package idc;

import idc.client.command.IdcClientDBListCmd;
import idc.line.command.IdcIPDeleteHandleCmd;
import idc.line.command.IdcIPModifyCmd;
import idc.line.command.IdcIPModifyFormCmd;
import idc.line.command.IdcLineDeleteCmd;
import idc.line.command.IdcLineIPListFormCmd;
import idc.line.command.IdcLineIPListFormExcelCmd;
import idc.line.command.IdcLineIPWriteCmd;
import idc.line.command.IdcLineListFormCmd;
import idc.line.command.IdcLineListFormExcelCmd;
import idc.line.command.IdcLineModifyCmd;
import idc.line.command.IdcLineModifyFormCmd;
import idc.line.command.IdcLineWriteCmd;
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("*.lin")
public class IdcLineFrontController extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private static final String LINE_PATH="/jsp/line/";
    /**
     * @see HttpServlet#HttpServlet()
     */
    public IdcLineFrontController() {
        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";
		Object obj=request.getSession().getAttribute("member");
		if(obj!=null)
		{
			//라인 등록
			if(cmdURI.equals("/IdcLineIPFirstWrite.lin"))
			{
				cmd = new IdcLineIPWriteCmd();
				cmd.execute(request, response);
				request.setAttribute("TargetFile", LINE_PATH+"IdcIPWriteForm.jsp");
			}
			//라인 등록 창
			if(cmdURI.equals("/IdcLineWriteForm.lin"))
			{
				request.setAttribute("TargetFile", LINE_PATH+"IdcLineWriteForm.jsp");
			}
			
			//라인 등록하기
			if(cmdURI.equals("/IdcLineWrite.lin"))
			{
				cmd = new IdcLineWriteCmd();
				cmd.execute(request,response);
				viewPage="IdcLineListForm.lin";
			}
			//수정 창 열어주기.
			if(cmdURI.equals("/IdcLineModifyForm.lin"))
			{
				cmd = new IdcLineModifyFormCmd();
				cmd.execute(request,response);
				request.setAttribute("TargetFile", LINE_PATH+"IdcLineModifyForm.jsp");
			}
			//수정 후 ListForm으로 돌아가기.
			if(cmdURI.equals("/IdcLineModify.lin"))
			{
				cmd = new IdcLineModifyCmd();
				cmd.execute(request,response);
				viewPage="IdcLineListForm.lin?curPage=1";
			}
			//Line 지우기.
			if(cmdURI.equals("/IdcLineDelete.lin"))
			{
				cmd = new IdcLineDeleteCmd();
				cmd.execute(request,response);
				//viewPage="IdcLineListForm.lin";
				cmdURI = "/IdcLineListForm.lin";
			}
			//라인보기
			if(cmdURI.equals("/IdcLineListForm.lin"))
			{
				cmd = new IdcLineListFormCmd();
				cmd.execute(request, response);
				request.setAttribute("TargetFile", LINE_PATH+"IdcLineListForm.jsp");
			}
			//라인소속 IP보기
			if(cmdURI.equals("/IdcIPListForm.lin"))
			{
				cmd = new IdcLineIPListFormCmd();
				cmd.execute(request, response);
				request.setAttribute("TargetFile", LINE_PATH+"IdcIPListForm.jsp");
			}
	//		Ip 수정창
			if(cmdURI.equals("/IdcIPModifyForm.lin"))
			{
				cmd = new IdcIPModifyFormCmd();
				cmd.execute(request, response);
				request.setAttribute("TargetFile", LINE_PATH+"IdcIPModifyForm.jsp");
			}
	//		IP수정
			if(cmdURI.equals("/IdcIPModify.lin"))
			{
				cmd = new IdcIPModifyCmd();
				cmd.execute(request, response);
				viewPage="IdcIPListForm.lin";
			}
			//테스트용 기본페이지.
			if(cmdURI.equals("/DefaultPage.main"))
			{
				cmd = new IdcClientDBListCmd(true);
				cmd.execute(request,response);
				request.setAttribute("TargetFile", LINE_PATH+"IdcClientDBList.jsp");
			}
			
			//IP삭제
			if(cmdURI.equals("/IdcIPDeleteHandle.lin")){
				cmd = new IdcIPDeleteHandleCmd();
				cmd.execute(request, response);
				viewPage="IdcIPListForm.lin";
			}
			//라인소속 IP보기 엑셀
			if(cmdURI.equals("/IdcIPListFormExcel.lin"))
			{
				cmd = new IdcLineIPListFormExcelCmd();
				cmd.execute(request, response);
				request.setAttribute("TargetFile", LINE_PATH+"IdcIPListFormExcel.jsp");
			}
			//IDCJP회선 및 전용선 사용 현황 엑셀
			if(cmdURI.equals("/IdcLineListFormExcel.lin"))
			{				
				cmd = new IdcLineListFormExcelCmd();
				cmd.execute(request, response);
				request.setAttribute("TargetFile", LINE_PATH+"IdcLineListFormExcel.jsp");
			}
		}
		else
		{
			request.setAttribute("TargetFile", "jsp/main/IdcLoginForm.jsp");
		}
		System.out.println(cmdURI);
		RequestDispatcher dis = request.getRequestDispatcher(viewPage);
		dis.forward(request, response);
	}

}
