package idc;

import idc.client.command.IdcClientDBListCmd;
import idc.cupon.command.*;
import idc.model.IdcCmd;
import idc.server.command.IdcServerListCmd;

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 IdcServerFrontController
 */
@WebServlet("*.cup")
public class IdcCuponFrontController extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private static final String CUPON_PATH="/jsp/cupon/";
    /**
     * @see HttpServlet#HttpServlet()
     */
    public IdcCuponFrontController() {
        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";
		
		//쿠폰 목록 조회		
		if(cmdURI.equals("/IdcCuponList.cup"))
		{			
			cmd = new IdcCuponListCmd();
			cmd.execute(request, response);
			request.setAttribute("TargetFile",CUPON_PATH+"IdcCuponList.jsp");
		}
		//NEW 쿠폰 목록 조회 - MK		
		if(cmdURI.equals("/IdcCouponListMK.cup"))
		{			
			cmd = new IdcCuponListCmd();
			cmd.execute(request, response);
			request.setAttribute("TargetFile",CUPON_PATH+"IdcCouponListMK.jsp");
		}		
		//NEW 쿠폰 확인 - 고객별 - MK		
		if(cmdURI.equals("/IdcCouponUseMK.cup"))
		{			
			cmd = new IdcCuponListCmd();
			//String mk_client_code = request.getParameter("client_code");
			cmd.execute(request, response);
			request.setAttribute("TargetFile",CUPON_PATH+"IdcCouponUseMK.jsp");

		}				
		//NEW 쿠폰 사용 - 서버별 - MK		
		if(cmdURI.equals("/IdcCouponBuyMK.cup"))
		{			
			cmd = new IdcCuponListCmd();
			//String mk_client_code = request.getParameter("client_code");
			cmd.execute(request, response);
			request.setAttribute("TargetFile",CUPON_PATH+"IdcCouponBuyMK.jsp");

		}
		//NEW 도메인 구매 목록 조회 - MK		
		if(cmdURI.equals("/IdcDomainBuyListMK.cup"))
		{			
			cmd = new IdcCuponListCmd();
			cmd.execute(request, response);
			request.setAttribute("TargetFile",CUPON_PATH+"IdcDomainBuyListMK.jsp");

		}			
		//쿠폰등록창
		if(cmdURI.equals("/IdcCuponWriteForm.cup")){			
			cmd = new IdcCuponWriteFormCmd();
			cmd.execute(request, response);
			request.setAttribute("TargetFile",CUPON_PATH+"IdcCuponWriteForm.jsp");

		}
		//쿠폰등록창2
		if(cmdURI.equals("/IdcCuponWriteForm2.cup"))
		{
			String str="IdcCuponList.cup";
			if(request.getParameterValues("checkNum")!=null)
			{
				str="DefaultPage.jsp";
				cmd = new IdcCuponWriteForm2Cmd();
				cmd.execute(request, response);
			}
			request.setAttribute("TargetFile",CUPON_PATH+"IdcCuponWriteForm2.jsp");
			viewPage=str;
		}
		//쿠폰등록 기능부
		if(cmdURI.equals("/IdcCuponWrite.cup"))
		{
			cmd = new IdcCuponWriteCmd();
			cmd.execute(request, response);
			viewPage="IdcCuponList.cup";
		}
		
//		쿠폰 사용부
		if(cmdURI.equals("/IdcUseCupon.cup"))
		{
			cmd = new IdcUseCuponCmd();
			cmd.execute(request, response);
			viewPage="IdcCuponList.cup";
		}
//		쿠폰 삭제부
		if(cmdURI.equals("/IdcCuponDelete.cup"))
		{
			cmd = new IdcCuponDeleteCmd();
			cmd.execute(request, response);
			viewPage="IdcCuponList.cup";
		}
//		쿠폰 수정 창
		if(cmdURI.equals("/IdcCuponModifyForm.cup"))
		{
			cmd = new IdcCuponModifyFormCmd();
			cmd.execute(request, response);
			viewPage="IdcCuponList.cup";
		}
//		쿠폰 수정
		if(cmdURI.equals("/IdcCuponModify.cup"))
		{
			cmd.execute(request, response);
			viewPage="IdcCuponList.cup";
		}
		//테스트용 기본페이지.
		if(cmdURI.equals("/DefaultPage.main"))
		{
			cmd = new IdcClientDBListCmd(true);
			cmd.execute(request,response);
			request.setAttribute("TargetFile",CUPON_PATH+"IdcCuponList.jsp");
		}
		
		System.out.println(cmdURI);
		RequestDispatcher dis = request.getRequestDispatcher(viewPage);
		dis.forward(request, response);
	}

}
