package com.madeu.crm.kiosk.ctrl;

import java.util.HashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.ModelAndView;

import com.madeu.constants.Constants;
import com.madeu.crm.kiosk.service.KioskService;
import com.madeu.init.ManagerDraftAction;
import com.madeu.service.web.webloghistory.WebLogHistoryService;
import com.madeu.util.HttpUtil;
import com.madeu.util.RequestLogUtil;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Controller
public class KioskController extends ManagerDraftAction {

	@Autowired
	private KioskService kioskService;
	
	@Autowired
    private WebLogHistoryService webLogHistoryService;

    /**
     * 키오스크 메인화면 화면으로 이동.
     *
     * @param request
     * @param response
     * @return
     */
    @GetMapping("/kiosk/MainIntro.do")
    public String selectMainIntro(HttpServletRequest request, HttpServletResponse response, Model model) {
        log.debug("KioskController  selectMainIntro START");

        log.debug("KioskController  selectMainIntro END");
        return "/kiosk/consultation";
    }
    
    /**
     * 키오스크 신규 환자 화면 화면으로 이동.
     *
     * @param request
     * @param response
     * @return
     */
    @GetMapping("/kiosk/NewPatientIntro.do")
    public String NewPatientIntro(HttpServletRequest request, HttpServletResponse response, Model model) {
        log.debug("KioskController  NewPatientIntro START");

        log.debug("KioskController  NewPatientIntro END");
        return "/kiosk/new-patient";
    }
    
    /**
     * 키오스크 기존 환자 화면 화면으로 이동.
     *
     * @param request
     * @param response
     * @return
     */
    @GetMapping("/kiosk/ExistPatientIntro.do")
    public String ExistPatientIntro(HttpServletRequest request, HttpServletResponse response, Model model) {
        log.debug("KioskController  ExistPatientIntro START");

        log.debug("KioskController  ExistPatientIntro END");
        return "/kiosk/existing-patient";
    }
    
    /**
	 * 진료 정보 리스트 조회 (option)
	 * 
	 * @param request
	 * @param response 
	 * @return
	 */
    @PostMapping("/kiosk/getTreatmentOptionList.do")
	public ModelAndView getTreatmentOptionList(HttpServletRequest request, HttpServletResponse response) {
		log.debug("KioskController getTreatmentOptionList START");
		
		HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
		HashMap<String, Object> map = new HashMap<String, Object>();
		
		try{
			map = kioskService.getTreatmentOptionList(paramMap);
		}
		catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		finally {
			if(Constants.OK == map.get("msgCode")) {

			}
			else{
				map.put("msgCode", Constants.FAIL);
				map.put("success", false);
				if(null == map.get("msgDesc") || ("").equals(map.get("msgDesc"))) {
					map.put("msgDesc","정상적으로 수행되지 않았습니다. 관리자에게 문의하시기 바랍니다.");
				}
			}
		}
		log.debug("KioskController getTreatmentOptionList END");
		return HttpUtil.makeHashToJsonModelAndView(map);
	}
    
    /**
     * 병원 기타정보 카테고리 타입 조회
     *
     * @param request
     * @param response
     * @return
     */
    @PostMapping("/kiosk/getCategoryList.do")
    public ModelAndView getCategoryList(HttpServletRequest request, HttpServletResponse response) {

        log.debug("KioskController getCategoryList START");

        HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
        HashMap<String, Object> map = new HashMap<String, Object>();

        try{            
            map = kioskService.getCategoryList(paramMap);
        }
        catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        finally {
            if(Constants.OK == map.get("msgCode")) {

            }
            else{
                map.put("msgCode", Constants.FAIL);
                map.put("success", false);
                if(null == map.get("msgDesc") || ("").equals(map.get("msgDesc"))) {
                    map.put("msgDesc","정상적으로 수행되지 않았습니다. 관리자에게 문의하시기 바랍니다.");
                }
            }
        }
        log.debug("KioskController getCategoryList END");
        return HttpUtil.makeHashToJsonModelAndView(map);
    }       
    
    /**
     * 병원 기타정보 카테고리 타입 조회
     *
     * @param request
     * @param response
     * @return
     */
    @PostMapping("/kiosk/getCategoryItem.do")
    public ModelAndView getCategoryItem(HttpServletRequest request, HttpServletResponse response) {

        log.debug("KioskController getCategoryItem START");

        HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
        HashMap<String, Object> map = new HashMap<String, Object>();
        
        try{            
            map = kioskService.getCategoryItem(paramMap);
        }
        catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        finally {
            if(Constants.OK == map.get("msgCode")) {

            }
            else{
                map.put("msgCode", Constants.FAIL);
                map.put("success", false);
                if(null == map.get("msgDesc") || ("").equals(map.get("msgDesc"))) {
                    map.put("msgDesc","정상적으로 수행되지 않았습니다. 관리자에게 문의하시기 바랍니다.");
                }
            }
        }
        log.debug("KioskController getCategoryItem END");
        return HttpUtil.makeHashToJsonModelAndView(map);
    }
    
    /**
     * 고객 정보 리스트 조회 (option)
     * 예약등록 팝업
     * @param request
     * @param response
     * @return
     */
    @PostMapping("/kiosk/getUserOptionList.do")
    public ModelAndView getUserOptionList(HttpServletRequest request, HttpServletResponse response) {

        log.debug("KioskController getUserOptionList START");

        HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
        HashMap<String, Object> map = new HashMap<String, Object>();
        
        try{            
            map = kioskService.getUserOptionList(paramMap);
        }
        catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        finally {
            if(Constants.OK == map.get("msgCode")) {

            }
            else{
                map.put("msgCode", Constants.FAIL);
                map.put("success", false);
                if(null == map.get("msgDesc") || ("").equals(map.get("msgDesc"))) {
                    map.put("msgDesc","정상적으로 수행되지 않았습니다. 관리자에게 문의하시기 바랍니다.");
                }
            }
        }
        log.debug("KioskController getUserOptionList END");
        return HttpUtil.makeHashToJsonModelAndView(map);
    }
    
    /**
     * 고객 정보 리스트 조회 (option)
     * 예약등록 팝업
     * @param request
     * @param response
     * @return
     */
    @PostMapping("/kiosk/putUser.do")
    public ModelAndView putUser(HttpServletRequest request, HttpServletResponse response) {

        log.debug("KioskController putUser START");

        HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
        HashMap<String, Object> map = new HashMap<String, Object>();
        
        StringBuffer errorMsg = new StringBuffer();
        try{            
        	paramMap.put("loginMemberId", "customer");
            paramMap.put("regId", "customer");
            paramMap.put("modId", "customer");
            map = kioskService.putUser(paramMap);
        }
        catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        finally {
        	if (Constants.OK == map.get("msgCode")) {

            } else {
                map.put("msgCode", Constants.FAIL);
                map.put("success", false);
                if (null == map.get("msgDesc") || ("").equals(map.get("msgDesc"))) {
                    map.put("msgDesc", "정상적으로 수행되지 않았습니다. 관리자에게 문의하시기 바랍니다.");
                }
            }

            try {
                HashMap<String, Object> visitLogParamMap = RequestLogUtil.getVisitLogParameterMap(request);
                HashMap<String, Object> insertMap = new HashMap<String, Object>();

                insertMap.put("url", "/kiosk/putUser.do");
                insertMap.put("func", "putUser");
                insertMap.put("funcName", "고객 등록");
                insertMap.put("service", "kioskService");
                insertMap.put("serviceName", "고객 등록");
                insertMap.put("requestValue", String.valueOf(paramMap));
                insertMap.put("responseValue", String.valueOf(map));
                insertMap.put("tId", map.get("tId"));
                if (("").equals(String.valueOf(errorMsg)) || null == (String.valueOf(errorMsg)) || 0 == String.valueOf(errorMsg).length()) {
                    insertMap.put("resultCode", "SUCCESS");
                } else {
                    insertMap.put("resultCode", "ERROR");
                }
                insertMap.put("resultMsg", String.valueOf(errorMsg));
                insertMap.put("muMemberId", "customer");

                webLogHistoryService.insertLogHistory(insertMap, visitLogParamMap);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        log.debug("KioskController getUserOptionList END");
        return HttpUtil.makeHashToJsonModelAndView(map);
    }
    
    /**
     * 고객 정보 조회
     * 바로접수 팝업
     * @param request
     * @param response
     * @return
     */
    @PostMapping("/kiosk/getUser.do")
    public ModelAndView getUser(HttpServletRequest request, HttpServletResponse response) {

        log.debug("KioskController getUser START");

        HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
        HashMap<String, Object> map = new HashMap<String, Object>();
        
        try{            
            map = kioskService.getUser(paramMap);
        }
        catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        finally {
            if(Constants.OK == map.get("msgCode")) {

            }
            else{
                map.put("msgCode", Constants.FAIL);
                map.put("success", false);
                if(null == map.get("msgDesc") || ("").equals(map.get("msgDesc"))) {
                    map.put("msgDesc","정상적으로 수행되지 않았습니다. 관리자에게 문의하시기 바랍니다.");
                }
            }
        }
        log.debug("KioskController getUser END");
        return HttpUtil.makeHashToJsonModelAndView(map);
    }
    
    /**
     * 근무 시간 조회
     * 바로접수 팝업
     * @param request
     * @param response
     * @return
     */
    @PostMapping("/kiosk/getWorkTime.do")
    public ModelAndView getWorkTime(HttpServletRequest request, HttpServletResponse response) {

        log.debug("KioskController getWorkTime START");

        HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
        HashMap<String, Object> map = new HashMap<String, Object>();
        
        try{            
            map = kioskService.getWorkTime(paramMap);
        }
        catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        finally {
            if(Constants.OK == map.get("msgCode")) {

            }
            else{
                map.put("msgCode", Constants.FAIL);
                map.put("success", false);
                if(null == map.get("msgDesc") || ("").equals(map.get("msgDesc"))) {
                    map.put("msgDesc","정상적으로 수행되지 않았습니다. 관리자에게 문의하시기 바랍니다.");
                }
            }
        }
        log.debug("KioskController getWorkTime END");
        return HttpUtil.makeHashToJsonModelAndView(map);
    }
    
    /**
     * 진료 시술 조회
     * 바로접수 팝업
     * @param request
     * @param response
     * @return
     */
    @PostMapping("/kiosk/getTreatmentProcedureOptionList.do")
    public ModelAndView getTreatmentProcedureOptionList(HttpServletRequest request, HttpServletResponse response) {

        log.debug("KioskController getTreatmentProcedureOptionList START");

        HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
        HashMap<String, Object> map = new HashMap<String, Object>();
        
        try{            
            map = kioskService.getTreatmentProcedureOptionList(paramMap);
        }
        catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        finally {
            if(Constants.OK == map.get("msgCode")) {

            }
            else{
                map.put("msgCode", Constants.FAIL);
                map.put("success", false);
                if(null == map.get("msgDesc") || ("").equals(map.get("msgDesc"))) {
                    map.put("msgDesc","정상적으로 수행되지 않았습니다. 관리자에게 문의하시기 바랍니다.");
                }
            }
        }
        log.debug("KioskController getTreatmentProcedureOptionList END");
        return HttpUtil.makeHashToJsonModelAndView(map);
    }
    
    /**
	 * 해당 시술 닥터 및 해당과 상담자 조회 (List, option)
	 * 
	 * @param request
	 * @param response 
	 * @return
	 */
    @PostMapping("/kiosk/getMemberDoctorConsultingOptionList.do")
    public ModelAndView getMemberDoctorConsultingOptionList(HttpServletRequest request, HttpServletResponse response) {

        log.debug("KioskController getMemberDoctorConsultingOptionList START");

        HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
        HashMap<String, Object> map = new HashMap<String, Object>();
        
        try{            
            map = kioskService.getMemberDoctorConsultingOptionList(paramMap);
        }
        catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        finally {
            if(Constants.OK == map.get("msgCode")) {

            }
            else{
                map.put("msgCode", Constants.FAIL);
                map.put("success", false);
                if(null == map.get("msgDesc") || ("").equals(map.get("msgDesc"))) {
                    map.put("msgDesc","정상적으로 수행되지 않았습니다. 관리자에게 문의하시기 바랍니다.");
                }
            }
        }
        log.debug("KioskController getMemberDoctorConsultingOptionList END");
        return HttpUtil.makeHashToJsonModelAndView(map);
    }
    
    /**
	 * 직원 정보 리스트 조회
	 * 
	 * @param request
	 * @param response 
	 * @return
	 */
    @PostMapping("/kiosk/getMemberList.do")
    public ModelAndView getMemberList(HttpServletRequest request, HttpServletResponse response) {

        log.debug("KioskController getMemberList START");

        HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
        HashMap<String, Object> map = new HashMap<String, Object>();
        
        try{            
            map = kioskService.getMemberList(paramMap);
        }
        catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        finally {
            if(Constants.OK == map.get("msgCode")) {

            }
            else{
                map.put("msgCode", Constants.FAIL);
                map.put("success", false);
                if(null == map.get("msgDesc") || ("").equals(map.get("msgDesc"))) {
                    map.put("msgDesc","정상적으로 수행되지 않았습니다. 관리자에게 문의하시기 바랍니다.");
                }
            }
        }
        log.debug("KioskController getMemberList END");
        return HttpUtil.makeHashToJsonModelAndView(map);
    }
    
    /**
	 * 고객 - 1년간 진료내역 조회 (수납완료건) (List, option)
	 * 
	 * @param request
	 * @param response 
	 * @return
	 */
    @PostMapping("/kiosk/getReserveUserOptionList.do")
    public ModelAndView getReserveUserOptionList(HttpServletRequest request, HttpServletResponse response) {

        log.debug("KioskController getReserveUserOptionList START");

        HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
        HashMap<String, Object> map = new HashMap<String, Object>();
        
        try{            
            map = kioskService.getReserveUserOptionList(paramMap);
        }
        catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        finally {
            if(Constants.OK == map.get("msgCode")) {

            }
            else{
                map.put("msgCode", Constants.FAIL);
                map.put("success", false);
                if(null == map.get("msgDesc") || ("").equals(map.get("msgDesc"))) {
                    map.put("msgDesc","정상적으로 수행되지 않았습니다. 관리자에게 문의하시기 바랍니다.");
                }
            }
        }
        log.debug("KioskController getReserveUserOptionList END");
        return HttpUtil.makeHashToJsonModelAndView(map);
    }
    
    /**
     * 키오스크 동의서 화면으로 이동.
     * @param request
     * @param response
     * @param model
     * @return
     */
    @GetMapping("/kiosk/agreement.do")
    public String agreementPage(HttpServletRequest request, HttpServletResponse response, Model model) {
        log.debug("KioskController agreementPage START");
        log.debug("KioskController agreementPage END");
        return "/kiosk/agreement";
    }
}