package idc;

import java.io.IOException;

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

import idc.client.command.IdcClientDBListCmd;
import idc.model.*;
import idc.server.command.*;


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

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		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";
		
		Object obj=request.getSession().getAttribute("member");
		if(obj!=null)
		{
			//서버 목록 조회		
			if(cmdURI.equals("/serverList.ser")){			
				cmd = new IdcServerListCmd();
				cmd.execute(request, response);
				request.setAttribute("TargetFile",SERVER_PATH+"serverList.jsp");
			}
					
			//서버 작성 화면 제공		
			if(cmdURI.equals("/serverWriteForm.ser")){						
				cmd = new IdcServerNumGenCmd();
				cmd.execute(request, response);
				request.setAttribute("TargetFile",SERVER_PATH+"serverWrite.jsp");
			}
			
			//서버 작성 처리
			if(cmdURI.equals("/serverWrite.ser")){
				cmd = new IdcServerWriteCmd();
				cmd.execute(request, response);
				viewPage="serverList.ser";
			}
			
			//서버 열람 처리
			if(cmdURI.equals("/serverRead.ser")){
				cmd = new IdcServerReadCmd();
				cmd.execute(request, response);
				request.setAttribute("TargetFile",SERVER_PATH+"serverRead.jsp");
			}
			
			//서버 수정 처리
			if(cmdURI.equals("/serverUpdate.ser")){
				cmd = new IdcServerUpdateCmd();			
				cmd.execute(request, response);			
				viewPage="serverList.ser";
			}
			
			//서버 삭제 처리
			if(cmdURI.equals("/serverDelete.ser")){
				cmd = new IdcServerDeleteCmd();
				cmd.execute(request, response);
				viewPage="serverList.ser";
			}
			
			//서버 가용 장비 확인
			if(cmdURI.equals("/usableServerList.ser")){			
				cmd = new IdcServerUsableCmd();
				cmd.execute(request, response);
				request.setAttribute("TargetFile",SERVER_PATH+"usableServerList.jsp");
			}
			//테스트용 기본페이지.
			if(cmdURI.equals("/DefaultPage.main"))
			{
				cmd = new IdcClientDBListCmd(true);
				cmd.execute(request,response);
				request.setAttribute("TargetFile",SERVER_PATH+"IdcCuponList.jsp");
			}
			
			//서버 목록 조회		
			if(cmdURI.equals("/serverListExcel.ser")){			
				cmd = new IdcServerListExcelCmd();
				cmd.execute(request, response);
				request.setAttribute("TargetFile",SERVER_PATH+"serverListExcel.jsp");
			}
		}
		else
		{
			request.setAttribute("TargetFile", "jsp/main/IdcLoginForm.jsp");
		}
		System.out.println(cmdURI);
		RequestDispatcher dis = request.getRequestDispatcher(viewPage);
		dis.forward(request, response);
	}

}
