package com.madeu.controller.mobile.mobilereservation;

import com.madeu.constants.Constants;
import com.madeu.init.ManagerDraftAction;
import com.madeu.service.mobile.mobilelogin.MobileLoginService;
import com.madeu.service.mobile.mobilereservation.MobileReservationService;
import com.madeu.util.HttpUtil;
import com.madeu.util.RequestLogUtil;
import com.madeu.util.ValidationCheckUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;


@Slf4j
@Controller
public class MobileReservationController extends ManagerDraftAction{

	@Autowired
	private MobileReservationService mobileReservationService;

	/**
	 * 예약등록 정보 확인 페이지 이동
	 * 페이지 처리 없음
	 * request:
	 * response : msgCode, msgDesc
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/mobilereservation/selectReserveIntro.do")
	public String selectReserveIntro(HttpSession session, HttpServletRequest request, Model model) {
		log.debug("MobileReservationController  selectReserveIntro START");

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

		try {
			if (!mobileCheckLogin(session)) {
				return "/mobile/login/login";
			} else {
				paramMap.put("loginUserId", String.valueOf(session.getAttribute("loginUserId")));
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "/mobile/login/login";
		}
		log.debug("MobileReservationController  selectReserveIntro END");
		return "/mobile/reservation/selectReservation";
	}

	/**
	 * 예약내역 정보 리스트 조회
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/mobilereservation/selectListReservation.do")
	public ModelAndView selectListReservation(HttpSession session, HttpServletRequest request, HttpServletResponse response) {

		log.debug("MobileReservationController selectListReservation START");

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

		try{
			if(!mobileCheckLogin(session)){
				return null;
			}
			else{
				paramMap.put("muUserId", String.valueOf(session.getAttribute("loginUserId")));
				map = mobileReservationService.selectListReservation(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("MobileReservationController selectListReservation END");
		return HttpUtil.makeHashToJsonModelAndView(map);
	}

	/**
	 * 이전 예약내역 정보 리스트 조회
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/mobilereservation/selectListReservationHistory.do")
	public ModelAndView selectListReservationHistory(HttpSession session, HttpServletRequest request, HttpServletResponse response) {

		log.debug("MobileReservationController selectListReservationHistory START");

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

		try{
			if(!mobileCheckLogin(session)){
				return null;
			}
			else{
				paramMap.put("muUserId", String.valueOf(session.getAttribute("loginUserId")));
				map = mobileReservationService.selectListReservationHistory(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("MobileReservationController selectListReservationHistory END");
		return HttpUtil.makeHashToJsonModelAndView(map);
	}

	/**
	 * 홈 상단 예약 알림 조회
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/mobilereservation/selectReservationHeader.do")
	public ModelAndView selectReservationHeader(HttpSession session, HttpServletRequest request, HttpServletResponse response) {

		log.debug("MobileReservationController selectReservationHeader START");

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

		try{
			if(!mobileCheckLogin(session)){
				return null;
			}
			else{
				paramMap.put("muUserId", String.valueOf(session.getAttribute("loginUserId")));
				paramMap.put("deviceUuid", String.valueOf(session.getAttribute("mobileDeviceUuid")));
				map = mobileReservationService.selectReservationHeader(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("MobileReservationController selectReservationHeader END");
		return HttpUtil.makeHashToJsonModelAndView(map);
	}

	/**
	 * 예약등록 화면 이동
	 * 페이지 처리 없음
	 * request:
	 * response : msgCode, msgDesc
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/mobilereservation/insertReservationIntro.do")
	public String insertReservationIntro(HttpSession session, HttpServletRequest request, Model model) {
		log.debug("MobileReservationController  insertReservationIntro START");

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

		try {
			if (!mobileCheckLogin(session)) {
				return "/mobile/login/login";
			} else {
				paramMap.put("loginUserId", String.valueOf(session.getAttribute("loginUserId")));
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "/mobile/login/login";
		}
		log.debug("MobileReservationController  insertReservationIntro END");
		return "/mobile/reservation/insertReservation";
	}

	/**
	 * 예약현황 정보 등록 (예약 등록)
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/mobilereservation/insertReserve.do")
	public ModelAndView insertReserve(HttpSession session,HttpServletRequest request, HttpServletResponse response, MultipartHttpServletRequest file) {
		log.debug("MobileReservationController insertReserve START");

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

		StringBuffer errorMsg = new StringBuffer();

		try{
			if(!mobileCheckLogin(session)){
				return null;
			}
			else{
				paramMap.put("muUserId", String.valueOf(session.getAttribute("loginUserId")));
				paramMap.put("regId", String.valueOf(session.getAttribute("loginUserId")));
				paramMap.put("modId", String.valueOf(session.getAttribute("loginUserId")));
				paramMap.put("userName", String.valueOf(session.getAttribute("mobileLoginName")));
				map = mobileReservationService.insertReserve(paramMap);
			}
		}
		catch (Exception e) {
			e.printStackTrace();
			errorMsg.append(e);
		}
		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", "/mobilereservation/insertReserve.do");
				insertMap.put("func", "insertReserve");
				insertMap.put("funcName", "예약현황 등록");
				insertMap.put("service", "webReserveService");
				insertMap.put("serviceName", "예약현황 등록");
				insertMap.put("requestValue", String.valueOf(paramMap));
				insertMap.put("responseValue", String.valueOf(map));
				insertMap.put("tId", map.get("tId"));
				if((String.valueOf(errorMsg)).equals("") || (String.valueOf(errorMsg) == null) || String.valueOf(errorMsg).length() == 0){
					insertMap.put("resultCode", "SUCCESS");
				}
				else{
					insertMap.put("resultCode", "ERROR");
				}
				insertMap.put("resultMsg", String.valueOf(errorMsg));
				insertMap.put("muMemberId", paramMap.get("loginMemberId"));

//				webLogHistoryService.insertLogHistory(insertMap, visitLogParamMap);
			}
			catch (Exception e) {
				e.printStackTrace();
			}
		}
		log.debug("MobileReservationController insertReserve END");
		return HttpUtil.makeHashToJsonModelAndView(map);
	}

	/**
	 * 예약현황 정보 상태 수정 (진료상태)
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/mobilereservation/updateReserveStatus.do")
	public ModelAndView updateReserveStatus(HttpSession session,HttpServletRequest request, HttpServletResponse response, MultipartHttpServletRequest file) {
		log.debug("MobileReservationController updateReserveStatus START");

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

		StringBuffer errorMsg = new StringBuffer();

		try{
			if(!mobileCheckLogin(session)){
				return null;
			}
			else{
				paramMap.put("muUserId", String.valueOf(session.getAttribute("loginUserId")));
				paramMap.put("regId", String.valueOf(session.getAttribute("loginUserId")));
				paramMap.put("modId", String.valueOf(session.getAttribute("loginUserId")));
				map = mobileReservationService.updateReserveStatus(paramMap);
			}
		}
		catch (Exception e) {
			e.printStackTrace();
			errorMsg.append(e);
		}
		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", "/mobilereservation/updateReserveStatus.do");
				insertMap.put("func", "updateReserve");
				insertMap.put("funcName", "예약현황 상태수정(진료상태)");
				insertMap.put("service", "webReserveService");
				insertMap.put("serviceName", "예약현황 상태수정(진료상태");
				insertMap.put("requestValue", String.valueOf(paramMap));
				insertMap.put("responseValue", String.valueOf(map));
				insertMap.put("tId", map.get("tId"));
				if((String.valueOf(errorMsg)).equals("") || (String.valueOf(errorMsg) == null) || String.valueOf(errorMsg).length() == 0){
					insertMap.put("resultCode", "SUCCESS");
				}
				else{
					insertMap.put("resultCode", "ERROR");
				}
				insertMap.put("resultMsg", String.valueOf(errorMsg));
				insertMap.put("muMemberId", paramMap.get("loginMemberId"));

//				webLogHistoryService.insertLogHistory(insertMap, visitLogParamMap);
			}
			catch (Exception e) {
				e.printStackTrace();
			}
		}
		log.debug("MobileReservationController updateReserveStatus END");
		return HttpUtil.makeHashToJsonModelAndView(map);
	}
}