package idc;

import idc.main.command.IdcGearArrayCmd;
import idc.main.command.IdcGearDeleteCmd;
import idc.main.command.IdcGearModifyCmd;
import idc.main.command.IdcIndexCmd;
import idc.main.command.IdcJoinCmd;
import idc.main.command.IdcLoginCheckCmd;
import idc.main.command.IdcLogoutCmd;
import idc.main.command.IdcMemberDeleteCmd;
import idc.main.command.IdcMemberListFormCmd;
import idc.main.command.IdcMemberModifyCmd;
import idc.main.command.IdcMemberPowerModifyCmd;
import idc.main.command.IdcMemberQuickModifyCmd;
import idc.main.command.IdcNewServerInsertCmd;
import idc.main.command.IdcShareBoardDeleteCmd;
import idc.main.command.IdcShareBoardDownCmd;
import idc.main.command.IdcShareBoardListForm;
import idc.main.command.IdcShareBoardModifyCmd;
import idc.main.command.IdcShareBoardModifyFormCmd;
import idc.main.command.IdcShareBoardReadFormCmd;
import idc.main.command.IdcShareBoardWriteCmd;
import idc.main.command.IdcTotalSearchFormCmd;
import idc.model.IdcCmd;

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 IdcClientFrontController
 */
@WebServlet("*.main")
public class IdcMainFrontController extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private static final String MAIN_PATH="jsp/main/" ;  
    /**
     * @see HttpServlet#HttpServlet()
     */
    public IdcMainFrontController() {
        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 
	{
		
		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());
		IdcCmd cmd = null;
		String viewPage = "LayoutPage.jsp";
		//로그인 페이지.
		if(cmdURI.equals("/IdcLoginForm.main"))
		{
			request.setAttribute("TargetFile", MAIN_PATH+"IdcLoginForm.jsp");
		}
		//로그인 체크하기.
		if(cmdURI.equals("/loginCheck.main"))
		{
			//로그인 체크
			cmd = new IdcLoginCheckCmd();
			cmd.execute(request,response);
			//성공
			if(request.getAttribute("memberCheck")!=null && (boolean)request.getAttribute("memberCheck") )
			{
				request.setAttribute("TargetFile", "IDC/index.jsp");
			}
			else //실패
			{
				request.setAttribute("alert", "o");
				viewPage ="/IdcLoginForm.main";
			}
		}
		Object obj=request.getSession().getAttribute("member");
		if(obj!=null)
		{
			//회원가입 폼.
			if(cmdURI.equals("/IdcJoinForm.main"))
			{
				request.setAttribute("TargetFile", MAIN_PATH+"IdcJoinForm.jsp");
			}
			//회원가입 데이터 전송.
			if(cmdURI.equals("/IdcJoin.main"))
			{
				cmd = new IdcJoinCmd();
				cmd.execute(request,response);
			}
			//로그아웃.
			if(cmdURI.equals("/IdcLogout.main"))
			{
				cmd = new IdcLogoutCmd();
				cmd.execute(request,response);
				request.setAttribute("TargetFile", MAIN_PATH+"IdcLoginForm.jsp");
			}
			//수정하기 창.
			if(cmdURI.equals("/IdcMemberModifyForm.main"))
			{
				request.setAttribute("TargetFile", MAIN_PATH+"IdcMemberModifyForm.jsp");
			}
			//수정하기.
			if(cmdURI.equals("/IdcMemberModify.main"))
			{
				cmd = new IdcMemberModifyCmd();
				cmd.execute(request,response);
				request.setAttribute("TargetFile", "IDC/index.jsp");
			}
			//멤버 리스트 보기.
			if(cmdURI.equals("/IdcMemberListForm.main"))
			{
				cmd = new IdcMemberListFormCmd();
				cmd.execute(request,response);
				request.setAttribute("TargetFile", MAIN_PATH+"IdcMemberListForm.jsp");
			}
			//권한 수정창
			if(cmdURI.equals("/IdcMemberPowerModify.main"))
			{
				cmd = new IdcMemberPowerModifyCmd();
				cmd.execute(request, response);
				viewPage="IdcMemberListForm.main?curPage=" + request.getParameter("curPage");
			}
			//멤버 삭제
			if(cmdURI.equals("/IdcMemberDelete.main"))
			{
				cmd = new IdcMemberDeleteCmd();
				cmd.execute(request, response);
			}
			//공동자료실
			if(cmdURI.equals("/IdcShareBoardListForm.main"))
			{
				cmd = new IdcShareBoardListForm();
				cmd.execute(request, response);
				request.setAttribute("TargetFile", MAIN_PATH+"IdcShareBoardListForm.jsp");
			}
			//공동 자료실 글 작성 폼
			if(cmdURI.equals("/IdcShareBoardWriteForm.main"))
			{
				request.setAttribute("TargetFile", MAIN_PATH+"IdcShareBoardWriteForm.jsp");
			}
			//공동 자료실 글 작성 기능
			if(cmdURI.equals("/IdcShareBoardWrite.main"))
			{
				cmd = new IdcShareBoardWriteCmd();
				cmd.execute(request, response);
				if((boolean)request.getAttribute("check"))	
					viewPage="IdcShareBoardListForm.main";
				else
					viewPage="/mainPage.main";
			}
			//공동 자료실 글 열람
			if(cmdURI.equals("/IdcShareBoardReadForm.main"))
			{
				cmd = new IdcShareBoardReadFormCmd();
				cmd.execute(request, response);
				request.setAttribute("TargetFile", MAIN_PATH+"IdcShareBoardReadForm.jsp");
			}
			//공동 자료실 글 수정 창
			if(cmdURI.equals("/IdcShareBoardModifyForm.main"))
			{
				cmd = new IdcShareBoardModifyFormCmd();
				cmd.execute(request, response);
				request.setAttribute("TargetFile", MAIN_PATH+"IdcShareBoardModifyForm.jsp");
			}
			//공동 자료실 글 수정 기능
			if (cmdURI.equals("/IdcShareBoardModify.main"))
			{
				cmd = new IdcShareBoardModifyCmd();
				cmd.execute(request, response);
				viewPage = "/IdcShareBoardListForm.main";
			}
			//공동 자료실 글 삭제
			if (cmdURI.equals("/IdcShareBoardDelete.main"))
			{
				cmd = new IdcShareBoardDeleteCmd();
				cmd.execute(request, response);
				viewPage = "/IdcShareBoardListForm.main";
			}
			//공동 자료실 다운로드
			if (cmdURI.equals("/IdcShareBoardDown.main"))
			{
				cmd = new IdcShareBoardDownCmd();
				cmd.execute(request, response);
				viewPage = "/IdcShareBoardReadForm.main?num="+request.getAttribute("num");
			}
			//서버 장비 등록 페이지
			if(cmdURI.equals("/IdcNewServerInsertForm.main"))
			{
				request.setAttribute("TargetFile", MAIN_PATH+"IdcNewServerInsertForm.jsp");
			}
			//서버 장비 등록
			if(cmdURI.equals("/IdcNewServerInsert.main"))
			{
				cmd = new IdcNewServerInsertCmd();
				cmd.execute(request, response);
				request.setAttribute("TargetFile", MAIN_PATH+"IdcNewServerInsertForm.jsp");
			}
			//서버 장비 수정
			if(cmdURI.equals("/IdcGearModify.main"))
			{
				cmd = new IdcGearModifyCmd();
				cmd.execute(request, response);
				request.setAttribute("TargetFile", MAIN_PATH+"IdcNewServerInsertForm.jsp");
			}
			//서버 장비 지우기
			if(cmdURI.equals("/IdcGearDelete.main"))
			{
				cmd = new IdcGearDeleteCmd();
				cmd.execute(request, response);
				request.setAttribute("TargetFile", MAIN_PATH+"IdcNewServerInsertForm.jsp");
			}
			//서버 장비 순서 변경
			if(cmdURI.equals("/IdcGearArray.main"))
			{
				cmd = new IdcGearArrayCmd();
				cmd.execute(request, response);
				request.setAttribute("TargetFile", MAIN_PATH+"IdcNewServerInsertForm.jsp");
			}
			//통합 검색 결과
			if(cmdURI.equals("/IdcTotalSearchForm.main"))
			{
				cmd = new IdcTotalSearchFormCmd();
				cmd.execute(request, response);
				request.setAttribute("TargetFile", MAIN_PATH+"IdcTotalSearchForm.jsp");
			}
			//멤버 퀵버튼
			if(cmdURI.equals("/IdcMemberQuickModify.main"))
			{
				cmd = new IdcMemberQuickModifyCmd();
				cmd.execute(request, response);
				request.setAttribute("TargetFile", "IDC/index.jsp");
			}
			//메인페이지
			if(cmdURI.equals("/mainPage.main")){
				cmd = new IdcIndexCmd();
				cmd.execute(request, response);
				request.setAttribute("TargetFile", "IDC/index.jsp");
			}
		}else{
			request.setAttribute("TargetFile", MAIN_PATH+"IdcLoginForm.jsp");
		}
		//최종출력장소
		System.out.println(cmdURI);
		RequestDispatcher dis = request.getRequestDispatcher(viewPage);
		dis.forward(request, response);
	}

}
