package com.madeu.controller.web.weblogin;

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.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.madeu.constants.Constants;
import com.madeu.init.ManagerDraftAction;
import com.madeu.service.web.weblogin.WebLoginService;
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;


@Slf4j
@Controller
public class WebLoginController extends ManagerDraftAction{

	@Autowired 
	private WebLoginService webLoginService;
	
	/**
	 * 로그인 화면 이동
	 * 페이지 처리 없음 
	 * request: 
	 * response : msgCode, msgDesc
	 * 
	 * @param request
	 * @param response 
	 * @return
	 */
	@RequestMapping(value="/weblogin/loginIntro.do")
	public String loginIntro(HttpSession session,HttpServletRequest request, HttpServletResponse response) {
		log.debug("WebLoginController loginIntro START");
		
		try{
			if(!webCheckLogin(session)){
				
			}
			else{
				return "/web/home/home";
			}
		}
		catch(Exception e){
			e.printStackTrace();
		}
		log.debug("WebLoginController loginIntro END");
		return "/web/login/login";
	}
	
	/**
	 * <b>로그인</b>
	 * 페이지 처리 없음 
	 * request: 
	 * response : msgCode, msgDesc
	 * 
	 * @param request
	 * @param response 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value="/weblogin/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 = webLoginService.memberLogin(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("muMemberId    : "+loginMap.get("muMemberId"));
				log.debug("loginId       : "+loginMap.get("loginId"));
				log.debug("memberName    : "+loginMap.get("memberName"));
				log.debug("memberType    : "+loginMap.get("memberType"));
				log.debug("======================== USER INFO END ============================");
				
				// 회원
				session.setAttribute("loginMemberId",loginMap.get("muMemberId"));
				session.setAttribute("loginId",loginMap.get("loginId"));
				session.setAttribute("loginName",loginMap.get("memberName"));

				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);
	}
	
	/**
	 * 로그아웃
	 * 페이지 처리 없음 
	 * request: 
	 * response : msgCode, msgDesc
	 * 
	 * @param request
	 * @param response 
	 * @return
	 */
	@RequestMapping(value="/weblogin/logout.do")
	public String logout(HttpSession session,HttpServletRequest request, HttpServletResponse response) {
		log.debug("WebLoginController logout START");
		
		try{
			log.debug("Session Clear1");
			session.invalidate();
			log.debug("Session Clear2");
		}
		catch(Exception e){
			e.printStackTrace();
		}
		log.debug("WebLoginController logout END");
		return "/web/login/login";
	}
	
	/**
	 * 로그인 아이디 중복체크 (아이디 중복확인)
	 * 
	 * @param request
	 * @param response 
	 * @return
	 */
	@RequestMapping(value="/weblogin/checkloginId.do")
	public ModelAndView checkloginId(HttpSession session,HttpServletRequest request, HttpServletResponse response) {
			
		log.debug("WebLoginController checkloginId START");
		
		HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
		HashMap<String, Object> map = new HashMap<String, Object>();
		
		try{
			if(!webCheckLogin(session)){
				return null;
			}
			else{
				map = webLoginService.checkloginId(paramMap);
			}
		}
		catch(Exception e){
			e.printStackTrace();
			return null;
		}
		log.debug("WebLoginController checkloginId END");
		return HttpUtil.makeHashToJsonModelAndView(map);
	}

}