package idc;

import idc.board.command.*;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class IdcBoardFrontController
 */
@WebServlet("*.bor")
public class IdcBoardFrontController extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private String BOARD_PATH = "/jsp/board/";
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public IdcBoardFrontController() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control","no-Cache");
		response.setHeader("Cache-Control","no-store");
		response.setContentType("charset='utf-8'");
		
		request.setCharacterEncoding("UTF-8");
		String requestURI = request.getRequestURI();
		String contextPath = request.getContextPath();
		String cmdURI = requestURI.substring(contextPath.length());
		
		BoardCmd cmd = null;
		String viewPage = "LayoutPage.jsp";
		
		Object obj=request.getSession().getAttribute("member");
		if(obj!=null)
		{
			//글 목록 조회 처리
			if(cmdURI.equals("/boardList.bor")){
				cmd = new BoardListCmd();
				cmd.execute(request, response);
				request.setAttribute("TargetFile",BOARD_PATH+"boardList.jsp");
			}
			
			//글 작성 화면 제공
			if(cmdURI.equals("/boardWriteForm.bor")){
				request.setAttribute("TargetFile",BOARD_PATH+"boardWrite.jsp");
			}
			
			//글 작성 처리
			if(cmdURI.equals("/boardWrite.bor")){
				cmd = new BoardWriteCmd();
				cmd.execute(request, response);
				viewPage = "boardList.bor";
			}
			
			//글 열람 처리
			if(cmdURI.equals("/boardRead.bor")){
				cmd = new BoardReadCmd();
				cmd.execute(request, response);					
				request.setAttribute("TargetFile",BOARD_PATH+"boardRead.jsp");
			}
			
			//글 수정 비밀번호 확인 화면 제공
			if(cmdURI.equals("/boardUpdatePassword.bor")){
				cmd = new BoardUpdatePasswordCmd();
				cmd.execute(request, response);
				request.setAttribute("TargetFile",BOARD_PATH+"boardUpdatePassword.jsp");
			}
			
			//글 수정 비밀번호 확인 처리
			if(cmdURI.equals("/boardUpdateCheck.bor")){
				cmd = new BoardUpdateCheckCmd();
				cmd.execute(request, response);
				
				BoardUpdateCheckCmd checkCmd = (BoardUpdateCheckCmd) cmd;
				
				if(checkCmd.password_check){				
					viewPage = "boardUpdateForm.bor";
				}else{				
					viewPage = "boardUpdateError.bor";
				}
			}
			
			//글 수정 비밀번호 오류 화면 제공
			if(cmdURI.equals("/boardUpdateError.bor")){			
				request.setAttribute("TargetFile",BOARD_PATH+"boardUpdateError.jsp");
			}
			
			//글 수정 화면 제공
			if(cmdURI.equals("/boardUpdateForm.bor")){
				cmd = new BoardUpdateFormCmd();
				cmd.execute(request, response);			
				request.setAttribute("TargetFile",BOARD_PATH+"boardUpdateForm.jsp");
			}
			
			//글 수정 처리
			if(cmdURI.equals("/boardUpdate.bor")){
				cmd = new BoardUpdateCmd();
				cmd.execute(request, response);
				viewPage = "boardList.bor";
			}
			
			//글 삭제 비밀번호 확인 화면 제공
			if(cmdURI.equals("/boardDeletePassword.bor")){
				cmd = new BoardDeletePasswordCmd();
				cmd.execute(request, response);
				request.setAttribute("TargetFile",BOARD_PATH+"boardDeletePassword.jsp");
			}
			
			//글 삭제 비밀번호 확인 처리
			if(cmdURI.equals("/boardDeleteCheck.bor")){
				cmd = new BoardDeleteCheckCmd();
				cmd.execute(request, response);
				
				BoardDeleteCheckCmd checkCmd = (BoardDeleteCheckCmd)cmd;
				if(checkCmd.password_check && checkCmd.reply_check){
					viewPage = "boardDelete.bor";
				}else{
					viewPage = "boardDeleteError.bor";
				}			
			}
			
			//글 삭제 비밀번호 오류 화면 제공
			if(cmdURI.equals("/boardDeleteError.bor")){
				request.setAttribute("TargetFile",BOARD_PATH+"boardDeleteError.jsp");
			}
			
			//글 삭제 처리
			if(cmdURI.equals("/boardDelete.bor")){
				cmd = new BoardDeleteCmd();
				cmd.execute(request, response);
				viewPage = "boardList.bor";			
			}
			
			//글 검색 처리
			if(cmdURI.equals("/boardSearch.bor")){
				cmd = new BoardSearchCmd();
				cmd.execute(request, response);
				//viewPage = "boardSearchList.jsp";
				request.setAttribute("TargetFile",BOARD_PATH+"boardSearchList.jsp");
			}
			
			//답글 작성 처리
			if(cmdURI.equals("/boardReplyForm.bor")){
				cmd = new BoardReplyFormCmd();
				cmd.execute(request, response);
				request.setAttribute("TargetFile",BOARD_PATH+"boardReply.jsp");
			}
			
			//답글 작성 처리
			if(cmdURI.equals("/boardReply.bor")){
				cmd = new BoardReplyCmd();
				cmd.execute(request, response);
				viewPage = "boardList.bor";
			}
			
			//개별게시판(메모) 조회처리
			if(cmdURI.equals("/notebook.bor")){
				cmd = new NoteBookCmd();
				cmd.execute(request, response);
				request.setAttribute("TargetFile",BOARD_PATH+"notebook.jsp");
			}
			
			//개별게시판(메모) 작성처리		
			if(cmdURI.equals("/writeNotebook.bor")){			
				cmd = new NoteBookWriteCmd();			
				cmd.execute(request, response);
				viewPage = "/notebook.bor";
			}
					
			//개별게시판(메모) 삭제처리		
			if(cmdURI.equals("/deleteNotebook.bor")){			
				cmd = new NoteBookDeleteCmd();			
				cmd.execute(request, response);
				viewPage = "/notebook.bor";
			}
		}
		else
		{
			request.setAttribute("TargetFile", "jsp/main/IdcLoginForm.jsp");
		}
		//최종출력장소
		System.out.println(cmdURI);
		RequestDispatcher dis = request.getRequestDispatcher(viewPage);
		dis.forward(request, response);
	}

}
