package idc;

import idc.client.command.*;
import idc.deposit.command.*;
import idc.line.command.IdcLineWriteCmd;
import idc.main.command.IdcIndexCmd;
import idc.model.IdcCmd;
import idc.model.IdcMemberDBDTO;

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("*.cli")
public class IdcClientFrontController extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private static final String CLIENT_PATH="jsp/client/" ;  
    /**
     * @see HttpServlet#HttpServlet()
     */
    public IdcClientFrontController() {
        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";
	
		IdcMemberDBDTO dto = (IdcMemberDBDTO)request.getSession().getAttribute("member");
		
		Object obj=request.getSession().getAttribute("member");
		if(obj!=null)
		{	
			if(cmdURI.equals("/IdcClientDBList.cli"))
			{
				cmd = new IdcClientDBListCmd(true);
				cmd = new IdcClientListFormCmd();
				cmd.execute(request, response);
				request.setAttribute("TargetFile", CLIENT_PATH+"IdcClientDBList.jsp");

			}
			//글 수정 처리
			if(cmdURI.equals("/IdcClientDirectUpdate.cli")){
				cmd = new IdcClientDBUpdateCmd();
				cmd.execute(request, response);
				viewPage = "IdcClientDBList.cli";				
			}
						
			//해지 고객 리스트
			if(cmdURI.equals("/IdcClient_DeleteDBList.cli"))
			{
				cmd = new IdcDeleteClientListFormCmd();
				cmd.execute(request, response);
				request.setAttribute("TargetFile", CLIENT_PATH+"IdcClientDBList.jsp");

			}
			// 글 작성 고객 등록 페이지
			if(cmdURI.equals("/IdcClientDBWriteForm.cli"))
			{
				cmd = new IdcClientDBWriteFormCmd();
				cmd.execute(request, response);
				request.setAttribute("TargetFile", CLIENT_PATH+"IdcClientDBWrite.jsp");

			}
			//고객 등록 기능
			if(cmdURI.equals("/IdcClientDBWrite.cli"))
			{
				cmd = new IdcClientDBWriteCmd();
				cmd.execute(request, response);
				viewPage = "IdcClientDBList.cli";
			}
			//고객정보 해지 || 복구
			if(cmdURI.equals("/IdcClientDBDelete.cli"))
			{
				boolean isClientDB=Boolean.parseBoolean(request.getParameter("isClientDB"));
				cmd = new IdcClientDBDeleteCmd(isClientDB);
				cmd.execute(request,response);
				viewPage="IdcClientDeleteDB.cli";
			}
			//고객정보 해지 || 복구 후 남은 데이터 지우기
			if(cmdURI.equals("/IdcClientDeleteDB.cli"))
			{
				cmd = new IdcClientDeleteDBCmd();
				cmd.execute(request,response);
				viewPage="IdcClientDBList.cli";
			}
			//현재 서버 보유현황 목록 보기
			if(cmdURI.equals("/IdcClientAllServerList.cli"))
			{
				cmd = new IdcClientAllServerListCmd();
				cmd.execute(request,response);
				request.setAttribute("TargetFile", CLIENT_PATH+"IdcClientAllServerList.jsp");
			}
			//2025-04-21 choi.jh 포인트 기능추가
			//사용자별 Point 목록 보기
			if(cmdURI.equals("/IdcClientPointList.cli"))
			{
				request.setAttribute("TargetFile", CLIENT_PATH+"IdcClientPointList.jsp");
			}
			//사용자별 Point 입력폼
			if(cmdURI.equals("/IdcClientPointInsert.cli"))
			{
				request.setAttribute("TargetFile", CLIENT_PATH+"IdcClientPointInsert.jsp");
			}
			//2025-04-21 choi.jh 포인트 기능추가
			//메인페이지
			if(cmdURI.equals("/DefaultPage.main"))
			{
				cmd = new IdcClientDBListCmd(true);
				cmd.execute(request,response);
				request.setAttribute("TargetFile", "/IDC/index.jsp");

			}
			//엑셀 출력 페이지
			if(cmdURI.equals("/IdcClientDBListExcel.cli"))
			{			
				cmd = new IdcClientListFormExcelCmd();
				cmd.execute(request, response);
				request.setAttribute("TargetFile", CLIENT_PATH+"IdcClientDBListExcel.jsp");
			}
		}
		else
		{
			request.setAttribute("TargetFile", "jsp/main/IdcLoginForm.jsp");
		}
		System.out.println(cmdURI);
		RequestDispatcher dis = request.getRequestDispatcher(viewPage);
		dis.forward(request, response);
	}

}
