package com.madeu.controller.mobile.mobilephoneauth;

import com.madeu.constants.Constants;
import com.madeu.init.ManagerDraftAction;
import com.madeu.service.mobile.mobilephoneauth.MobilePhoneAuthService;
import com.madeu.service.web.webhome.WebHomeService;
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.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;

@Slf4j
@Controller
public class mobilePhoneAuthController extends ManagerDraftAction {

    @Autowired
    private MobilePhoneAuthService mobilePhoneAuthService;

    /**
     * <b>휴대폰 인증 확인</b>
     * request:
     * response : msgCode, msgDesc
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value="/mobilephoneauth/selectPhoneAuthCheck.do")
    public ModelAndView selectPhoneAuthCheck(HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        log.debug("mobilePhoneAuthController selectPhoneAuthCheck START");

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

        StringBuffer errorMsg = new StringBuffer();

        try{
                paramMap.put("loginUserId", String.valueOf(session.getAttribute("loginUserId")));
                paramMap.put("muUserId", String.valueOf(session.getAttribute("loginUserId")));
                map = mobilePhoneAuthService.selectPhoneAuthCheck(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","정상적으로 수행되지 않았습니다. (E0042)");
                }

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

                    insertMap.put("url", "/mobilephoneauth/selectPhoneAuthCheck.do");
                    insertMap.put("func", "selectPhoneAuthCheck");
                    insertMap.put("funcName", "휴대폰 인증 확인");
                    insertMap.put("service", "webPhoneAuthService");
                    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("muUserId", paramMap.get("muUserId"));

//                    logHistoryService.insertLogHistory(insertMap, visitLogParamMap);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        log.debug("mobilePhoneAuthController selectPhoneAuthCheck END");
        return HttpUtil.makeHashToJsonModelAndView(map);
    }


    /**
     * <b>인증번호 등록 </b>
     * request:
     * response : msgCode, msgDesc
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value="/mobilephoneauth/insertPhoneAuth.do")
    public ModelAndView insertPhoneAuth(HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        log.debug("mobilePhoneAuthController insertPhoneAuth START");

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

        StringBuffer errorMsg = new StringBuffer();

        try{
            paramMap.put("regId", String.valueOf(session.getAttribute("loginUserId")));
            paramMap.put("modId", String.valueOf(session.getAttribute("loginUserId")));
            map = mobilePhoneAuthService.insertPhoneAuth(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","정상적으로 수행되지 않았습니다. (E0043)");
                }
            }

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

                insertMap.put("url", "/mobilephoneauth/insertPhoneAuth.do");
                insertMap.put("func", "insertPhoneAuth");
                insertMap.put("funcName", "인증번호 등록");
                insertMap.put("service", "webPhoneAuthService");
                insertMap.put("serviceName", "인증번호");
                insertMap.put("requestValue", String.valueOf(paramMap));
                insertMap.put("responseValue", String.valueOf(map));
                insertMap.put("tId", map.get("tId"));
                if(("false").equals(String.valueOf(map.get("success")))){
                    insertMap.put("resultCode", "ERROR");
                }else{
                    insertMap.put("resultCode", "SUCCESS");
                }
                insertMap.put("resultMsg", String.valueOf(errorMsg));
                insertMap.put("sbUserId", paramMap.get("sbUserId"));

//                logHistoryService.insertLogHistory(insertMap, visitLogParamMap);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        log.debug("mobilePhoneAuthController insertPhoneAuth END");
        return HttpUtil.makeHashToJsonModelAndView(map);
    }

}
