package com.madeu.controller.mobile.mobilelogin;

import com.madeu.constants.Constants;
import com.madeu.init.ManagerDraftAction;
import com.madeu.service.mobile.mobilelogin.MobileLoginService;
import com.madeu.util.HttpUtil;
import com.madeu.util.RequestLogUtil;
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.servlet.ModelAndView;

import java.util.HashMap;


@Slf4j
@Controller
public class MobileLoginController extends ManagerDraftAction{

	@Autowired 
	private MobileLoginService mobileLoginService;
	/**
	 * 로그인 화면 이동
	 * 페이지 처리 없음
	 * request:
	 * response : msgCode, msgDesc
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/mobile")
	public String loginIntro(HttpSession session,HttpServletRequest request, HttpServletResponse response) {
		log.debug("WebLoginController loginIntro START");

		try{
			if(!mobileCheckLogin(session)){

			}
			else{
				return "/mobile/home/home";
			}
		}
		catch(Exception e){
			e.printStackTrace();
		}
		log.debug("MobileLoginController loginIntro END");
		return "/mobile/login/login";
	}
	/**
	 * 로그인 화면 이동
	 * 페이지 처리 없음 
	 * request: 
	 * response : msgCode, msgDesc
	 * 
	 * @param request
	 * @param response 
	 * @return
	 */
	@RequestMapping(value="/mobilelogin/loginIntro.do")
	public String loginIntro2(HttpSession session,HttpServletRequest request, HttpServletResponse response) {
		log.debug("WebLoginController loginIntro START");
		
		try{
			if(!mobileCheckLogin(session)){
				
			}
			else{
				return "/mobile/home/home";
			}
		}
		catch(Exception e){
			e.printStackTrace();
		}
		log.debug("MobileLoginController loginIntro END");
		return "/mobile/login/login";
	}
	
	/**
	 * <b>로그인</b>
	 * 페이지 처리 없음 
	 * request: 
	 * response : msgCode, msgDesc
	 * 
	 * @param request
	 * @param response 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value="/mobilelogin/login.do")
	public ModelAndView login(HttpSession session,HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {
		log.debug("WebLoginController login START");
		
		HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
		HashMap<String, Object> map = new HashMap<String, Object>();


		try{
			HashMap<String, Object> visitLogParamMap = RequestLogUtil.getVisitLogParameterMap(request);
			HashMap<String, Object> loginMap = mobileLoginService.userLogin(paramMap,visitLogParamMap);

			if((Constants.FAIL).equals(String.valueOf(loginMap.get("msgCode")))){
				map.put("msgCode", loginMap.get("msgCode"));
				map.put("msgDesc", loginMap.get("msgDesc"));
			}
			else{
				log.debug("======================== USER INFO START ==========================");
				log.debug("LoginUserId    	    : "+loginMap.get("muUserId"));
				log.debug("mobileLoginId        : "+loginMap.get("loginId"));
				log.debug("mobileLoginName      : "+loginMap.get("userName"));
				log.debug("userType             : "+loginMap.get("userType"));
				log.debug("deviceUuid           : "+loginMap.get("deviceUuid"));
				log.debug("======================== USER INFO END ============================");
				
				// 회원
				session.setAttribute("loginUserId",loginMap.get("muUserId"));
				session.setAttribute("mobileLoginId",loginMap.get("loginId"));
				session.setAttribute("mobileLoginName",loginMap.get("userName"));
				session.setAttribute("mobileDeviceUuid",loginMap.get("deviceUuid"));

				log.debug("session     : "+session);
				
				map.put("msgCode", loginMap.get("msgCode"));
				map.put("msgDesc", loginMap.get("msgDesc"));
			}

		}
		catch(Exception e){
			e.printStackTrace();
		}
		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("WebLoginController login END");
		return HttpUtil.makeHashToJsonModelAndView(map);
	}

	/**
	 * <b>로그인 SNS</b>
	 * 페이지 처리 없음
	 * request:
	 * response : msgCode, msgDesc
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value="/mobilelogin/snsLogin.do")
	public ModelAndView snsLogin(HttpSession session,HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {
		log.debug("WebLoginController snsLogin START");

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


		try{
			HashMap<String, Object> visitLogParamMap = RequestLogUtil.getVisitLogParameterMap(request);
			HashMap<String, Object> loginMap = mobileLoginService.snsLogin(paramMap,visitLogParamMap);

			if((Constants.FAIL).equals(String.valueOf(loginMap.get("msgCode")))){
				map.put("msgCode", loginMap.get("msgCode"));
				map.put("msgDesc", loginMap.get("msgDesc"));
			}
			else{
				log.debug("======================== USER INFO START ==========================");
				log.debug("loginUserId    	    : "+loginMap.get("muUserId"));
				log.debug("mobileLoginId        : "+loginMap.get("loginId"));
				log.debug("mobileLoginName      : "+loginMap.get("userName"));
				log.debug("userType             : "+loginMap.get("userType"));
				log.debug("deviceUuid           : "+loginMap.get("deviceUuid"));
				log.debug("======================== USER INFO END ============================");

				// 회원
				session.setAttribute("loginUserId",loginMap.get("muUserId"));
				session.setAttribute("mobileLoginId",loginMap.get("loginId"));
				session.setAttribute("mobileLoginName",loginMap.get("userName"));
				session.setAttribute("mobileDeviceUuid",loginMap.get("deviceUuid"));

				log.debug("session     : "+session);

				map.put("msgCode", loginMap.get("msgCode"));
				map.put("msgDesc", loginMap.get("msgDesc"));
			}

		}
		catch(Exception e){
			e.printStackTrace();
		}
		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("WebLoginController snsLogin END");
		return HttpUtil.makeHashToJsonModelAndView(map);
	}

	/**
	 * 로그아웃
	 * 페이지 처리 없음 
	 * request: 
	 * response : msgCode, msgDesc
	 * 
	 * @param request
	 * @param response 
	 * @return
	 */
	@RequestMapping(value="/mobilelogin/logout.do")
	public String logout(HttpSession session,HttpServletRequest request, HttpServletResponse response) {
		log.debug("WebLoginController logout START");

		HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
		HashMap<String, Object> map = new HashMap<String, Object>();
		
		try{
			log.debug("Session Clear1");

			// 로그아웃시 디바이스 status Y -> N 업데이트
			paramMap.put("regId", String.valueOf(session.getAttribute("loginUserId")));
			paramMap.put("modId", String.valueOf(session.getAttribute("loginUserId")));
			paramMap.put("muUserId", String.valueOf(session.getAttribute("loginUserId")));
			paramMap.put("deviceUuid", String.valueOf(session.getAttribute("mobileDeviceUuid")));
			map = mobileLoginService.userLogout(paramMap);



			session.invalidate();
			log.debug("Session Clear2");
		}
		catch(Exception e){
			e.printStackTrace();
		}
		log.debug("WebLoginController logout END");
		return "/mobile/login/login";
	}

	/**
	 * 로그아웃
	 * 페이지 처리 없음
	 * request:
	 * response : msgCode, msgDesc
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/mobilelogin/deleteUser.do")
	public ModelAndView deleteUser(HttpSession session,HttpServletRequest request, HttpServletResponse response) {
		log.debug("WebLoginController deleteUser START");
		HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
		HashMap<String, Object> map = new HashMap<String, Object>();
		try{
			paramMap.put("muUserId", String.valueOf(session.getAttribute("loginUserId")));
			paramMap.put("modId", String.valueOf(session.getAttribute("loginUserId")));

			HashMap<String, Object> loginMap = mobileLoginService.deleteUser(paramMap);
			map.put("msgCode", loginMap.get("msgCode"));
			map.put("msgDesc", loginMap.get("msgDesc"));
		}
		catch(Exception e){
			e.printStackTrace();
		}
		log.debug("WebLoginController deleteUser END");
		return HttpUtil.makeHashToJsonModelAndView(map);
	}
}