package com.madeu.controller.web.webauth;

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

import com.madeu.constants.Constants;
import com.madeu.init.ManagerDraftAction;
import com.madeu.service.web.webauth.WebAuthService;
import com.madeu.service.web.webloghistory.WebLogHistoryService;
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 WebAuthController extends ManagerDraftAction{

	@Autowired
	private WebAuthService webAuthService;
	
	@Autowired
	private WebLogHistoryService webLogHistoryService;
	
	/**
	 * 권한 정보 리스트 조회 (option)
	 * 
	 * @param request
	 * @param response 
	 * @return
	 */
	@RequestMapping(value="/webauth/selectListAuthOption.do")
	public ModelAndView selectListAuthOption(HttpSession session,HttpServletRequest request, HttpServletResponse response) {
		log.debug("WebAuthController selectListAuthOption START");
		
		HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
		HashMap<String, Object> map = new HashMap<String, Object>();
		
		try{
			if(!webCheckLogin(session)){
				return null;
			}
			else{
				paramMap.put("loginMemberId", String.valueOf(session.getAttribute("loginMemberId")));
				map = webAuthService.selectListAuthOption(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("WebAuthController selectListAuthOption END");
		return HttpUtil.makeHashToJsonModelAndView(map);
	}
	
	/**
	 * 권한 정보 리스트 조회
	 * 
	 * @param request
	 * @param response 
	 * @return
	 */
	@RequestMapping(value="/webauth/selectListAuth.do")
	public ModelAndView selectListAuth(HttpSession session,HttpServletRequest request, HttpServletResponse response) {
		log.debug("WebAuthController selectListAuth START");
		
		HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
		HashMap<String, Object> map = new HashMap<String, Object>();
		
		try{
			if(!webCheckLogin(session)){
				return null;
			}
			else{
				paramMap.put("loginMemberId", String.valueOf(session.getAttribute("loginMemberId")));
				map = webAuthService.selectListAuth(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("WebAuthController selectListAuth END");
		return HttpUtil.makeHashToJsonModelAndView(map);
	}
	/**
	 * 권한 정보 상세 조회
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/webauth/selectAuth.do")
	public ModelAndView selectAuth(HttpSession session,HttpServletRequest request, HttpServletResponse response) {

		log.debug("WebAuthController selectAuth START");

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

		try{
			if(!webCheckLogin(session)){
				return null;
			}
			else{
				paramMap.put("loginMemberId", String.valueOf(session.getAttribute("loginMemberId")));
				map = webAuthService.selectAuth(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("WebAuthController selectAuth END");
		return HttpUtil.makeHashToJsonModelAndView(map);
	}
	/**
	 * 헤더 메뉴 권한 조회
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/webauth/selectHeaderRelation.do")
	public ModelAndView selectHeaderRelation(HttpSession session,HttpServletRequest request, HttpServletResponse response) {

		log.debug("WebAuthController selectHeaderRelation START");

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

		try{
			if(!webCheckLogin(session)){
				return null;
			}
			else{
				paramMap.put("loginMemberId", String.valueOf(session.getAttribute("loginMemberId")));
				map = webAuthService.selectHeaderRelation(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("WebAuthController selectHeaderRelation END");
		return HttpUtil.makeHashToJsonModelAndView(map);
	}
	/**
	 * 권한그룹 정보 수정
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/webauth/updateAuth.do")
	public ModelAndView updateDuty(HttpSession session,HttpServletRequest request, HttpServletResponse response, MultipartHttpServletRequest file) {

		log.debug("WebAuthController updateAuth START");

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

		StringBuffer errorMsg = new StringBuffer();

		try{
			if(!webCheckLogin(session)){
				return null;
			}
			else{
				paramMap.put("loginMemberId", String.valueOf(session.getAttribute("loginMemberId")));
				paramMap.put("regId", String.valueOf(session.getAttribute("loginMemberId")));
				paramMap.put("modId", String.valueOf(session.getAttribute("loginMemberId")));
				map = webAuthService.updateAuth(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", "/webauth/updateAuth.do");
				insertMap.put("func", "updateAuth");
				insertMap.put("funcName", "권한그룹 수정");
				insertMap.put("service", "webAuthService");
				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("WebAuthController updateAuth END");
		return HttpUtil.makeHashToJsonModelAndView(map);
	}
	/**	
	 * 권한 정보 등록
	 * 
	 * @param request
	 * @param response 
	 * @return
	 */
	@RequestMapping(value="/webauth/insertAuth.do")
	public ModelAndView insertAuth(HttpSession session,HttpServletRequest request, HttpServletResponse response, MultipartHttpServletRequest file) {
				
		log.debug("WebAuthController insertAuth START");
		
		HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
		HashMap<String, Object> map = new HashMap<String, Object>();

		StringBuffer errorMsg = new StringBuffer();

		try{
			if(!webCheckLogin(session)){
				return null;
			}
			else{
				paramMap.put("loginMemberId", String.valueOf(session.getAttribute("loginMemberId")));
				paramMap.put("regId", String.valueOf(session.getAttribute("loginMemberId")));
				paramMap.put("modId", String.valueOf(session.getAttribute("loginMemberId")));
				map = webAuthService.insertAuth(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", "/webauth/insertAuth.do");
				insertMap.put("func", "insertAuth");
				insertMap.put("funcName", "권한 등록");
				insertMap.put("service", "webAuthService");
				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("WebAuthController insertAuth END");
		return HttpUtil.makeHashToJsonModelAndView(map);
	}

	/**
	 * 권한 정보 삭제
	 * 
	 * @param request
	 * @param response 
	 * @return
	 */
	@RequestMapping(value="/webauth/deleteAuth.do")
	public ModelAndView deleteAuth(HttpSession session,HttpServletRequest request, HttpServletResponse response) {
				
		log.debug("WebAuthController deleteAuth START");
		
		HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
		HashMap<String, Object> map = new HashMap<String, Object>();

		StringBuffer errorMsg = new StringBuffer();
		
		try{
			if(!webCheckLogin(session)){
				return null;
			}
			else{
				paramMap.put("loginMemberId", String.valueOf(session.getAttribute("loginMemberId")));
				paramMap.put("regId", String.valueOf(session.getAttribute("loginMemberId")));
				paramMap.put("modId", String.valueOf(session.getAttribute("loginMemberId")));
				map = webAuthService.deleteAuth(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", "/webauth/deleteAuth.do");
				insertMap.put("func", "deleteAuth");
				insertMap.put("funcName", "권한 삭제");
				insertMap.put("service", "webAuthService");
				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("WebAuthController deleteAuth END");
		return HttpUtil.makeHashToJsonModelAndView(map);
	}
}
