package com.madeu.controller.mobile.mobilejoin;

import com.madeu.constants.Constants;
import com.madeu.init.ManagerDraftAction;
import com.madeu.service.mobile.mobilejoin.MobileJoinService;
import com.madeu.util.HttpUtil;
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.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
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 MobileJoinController extends ManagerDraftAction {

	@Autowired
	private MobileJoinService mobileJoinService;

	/**
	 * 회원가입 페이지 화면 이동 (약관동의)
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping("/mobilejoin/joinIntro.do")
	public String joinIntro(HttpSession session,HttpServletRequest request, Model model) {
		log.debug("MobileJoinController  joinIntro START");

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

		log.debug("MobileJoinController  insertBoardIntro END");
		return "/mobile/join/joinAgreement";
	}

	/**
	 * 회원가입 페이지 화면 이동2 (정보입력)
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping("/mobilejoin/joinInputIntro.do")
	public String joinInputIntro(HttpSession session,HttpServletRequest request, Model model) {
		log.debug("MobileJoinController  joinInputIntro START");

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

		log.debug("MobileJoinController  joinInputIntro END");
		return "/mobile/join/joinInput";
	}

	/**
	 * <b>계정찾기 페이지</b>
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/mobilejoin/findIdIntro.do")
	public String findIdIntro(HttpSession session, HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {
		log.debug("MobileJoinController findIdIntro START");

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

		log.debug("MobileJoinController  insertBoardIntro END");
		return "/mobile/account/findId";
	}

	/**
	 * 비밀번호 찾기 페이지 이동
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/mobilejoin/findPasswordIntro.do")
	public String logout(HttpSession session,HttpServletRequest request, HttpServletResponse response) {
		log.debug("MobileJoinController findPasswordIntro START");

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

		log.debug("MobileJoinController  findPasswordIntro END");
		return "/mobile/account/findPassword";
	}

	/**
	 * 로그인 아이디 중복체크 (아이디 중복확인)
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/mobilejoin/checkMembershipId.do")
	public ModelAndView checkMembershipId(HttpSession session, HttpServletRequest request, HttpServletResponse response) {

		log.debug("MobileJoinController checkMembershipId START");

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

		try{
			map = mobileJoinService.checkMembershipId(paramMap);
		}
		catch(Exception e){
			e.printStackTrace();
			return null;
		}
		log.debug("MobileJoinController checkMembershipId END");
		return HttpUtil.makeHashToJsonModelAndView(map);
	}

	/**
	 * 고객 가입.
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/mobilejoin/insertUser.do")
	public ModelAndView insertUser(HttpSession session, HttpServletRequest request, HttpServletResponse response, MultipartHttpServletRequest file) {

		log.debug("MobileJoinController insertUser START");

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

		StringBuffer errorMsg = new StringBuffer();

		try {
				map = mobileJoinService.insertUser(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", "정상적으로 수행되지 않았습니다. 관리자에게 문의하시기 바랍니다.");
				}
			}
		}
		log.debug("WebUserController insertUser END");
		return HttpUtil.makeHashToJsonModelAndView(map);
	}
}