package com.dbnt.faisp.config;

import com.dbnt.faisp.main.codeMgt.model.CodeMgt;
import com.dbnt.faisp.main.menuMgt.service.MenuMgtService;
import com.dbnt.faisp.main.organMgt.service.OrganConfigService;
import com.dbnt.faisp.main.userInfo.model.UserInfo;
import com.dbnt.faisp.main.codeMgt.service.CodeMgtService;

import com.dbnt.faisp.main.userInfo.service.UserInfoService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@RestController
@RequiredArgsConstructor
public class BaseController {

  private final CodeMgtService codeMgtService;
  private final OrganConfigService organConfigService;
  private final MenuMgtService menuMgtService;
  private final UserInfoService userInfoService;

  @GetMapping("/")
  public ModelAndView loginCheck(@AuthenticationPrincipal UserInfo loginUser) {
    ModelAndView mav = null;
    if(loginUser == null){
      mav = new ModelAndView("redirect:/login");
    }else{
      mav = new ModelAndView("redirect:/dashboard");
    }
    return mav;
  }

  @GetMapping("/login")
  public ModelAndView goLogin() { 
    ModelAndView mav = new ModelAndView("login/login");
    return mav;
  }

  @GetMapping("/login-error")
  public ModelAndView loginError() {
    ModelAndView mav = new ModelAndView("login/login");
    mav.addObject("loginError", true);
    return mav;
  }

  @GetMapping("/denied")
  public ModelAndView doDenied() {
    ModelAndView mav = new ModelAndView("login/denied");
    return mav;
  }

  @GetMapping("/dashboard")
  public ModelAndView dashboard() {
    ModelAndView mav = new ModelAndView("login/dashboard");
    return mav;
  }

  @GetMapping("/selectBoxOptions")
  public ModelAndView codeMgtPage(String categoryCd, String selectedCd) {
    ModelAndView mav = new ModelAndView("common/selectBox/options");
    mav.addObject("codeList", codeMgtService.selectCodeMgtList(categoryCd, ""));
    mav.addObject("selectedCd", selectedCd);
    return mav;
  }

  public void setSession(@AuthenticationPrincipal UserInfo loginUser, HttpSession session){
    loginUser.setDownOrganCdList(organConfigService.selectDownOrganListWhereUserOgCd(loginUser.getOgCd()));
    loginUser.setUpOrganCdList(organConfigService.selectUpOrganListWhereUserOgCd(loginUser.getOgCd()));
    session.setAttribute("menuList", menuMgtService.selectAccessMenuListWhereUserSeq(loginUser.getUserSeq()));
    Map<String, List<CodeMgt>> codeMap = codeMgtService.getCommonCode();
    session.setAttribute("commonCode", codeMap);
    session.setAttribute("userSeq", loginUser.getUserSeq());
    session.setAttribute("userOrgan", loginUser.getOgCd());
    String belongValue = "";
    belongValue += searchCodeValue(loginUser.getOgCd(), codeMap.get("OG"));
    belongValue += searchCodeValue(loginUser.getOfcCd(), codeMap.get("OFC"));
    belongValue += searchCodeValue(loginUser.getTitleCd(), codeMap.get("JT"));
    belongValue += loginUser.getUserNm()+"("+loginUser.getUserId()+")";
    session.setAttribute("belongValue", belongValue);
  }
  private String searchCodeValue(String itemCd, List<CodeMgt> codeList){
    if(itemCd==null){
      return "";
    }else{
      for(CodeMgt code: codeList){
        if(itemCd.equals(code.getItemCd())){
          return code.getItemValue()+" ";
        }
      }
    }
    return "";
  }
}
