package com.dbnt.faisp.main.userInfo;

import com.dbnt.faisp.kwms.service.KwmsService;
import com.dbnt.faisp.main.codeMgt.service.CodeMgtService;
import com.dbnt.faisp.main.userInfo.model.DashboardConfig;
import com.dbnt.faisp.main.userInfo.model.UserInfo;
import com.dbnt.faisp.main.userInfo.service.UserInfoService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/myInfo")
public class MyInfoController {

  private final UserInfoService userInfoService;
  private final CodeMgtService codeMgtService;
  private final KwmsService kwmsService;

  @GetMapping("/myInfoPage")
  public ModelAndView myInfoPage(@AuthenticationPrincipal UserInfo loginUser){
    ModelAndView mav = new ModelAndView("user/myInfo");
    mav.addObject("userInfo", userInfoService.selectUserInfo(loginUser.getUserSeq()));
    mav.addObject("ogList", codeMgtService.selectCodeMgtList("OG", ""));
    mav.addObject("ofcList", codeMgtService.selectCodeMgtList("OFC", ""));
    mav.addObject("titleList", codeMgtService.selectCodeMgtList("JT", ""));
    mav.addObject("outturnList", codeMgtService.selectCodeMgtList("OTC", ""));
    mav.addObject("seriesList", codeMgtService.selectCodeMgtList("SRC", ""));
    mav.addObject("languageList", codeMgtService.selectCodeMgtList("LNG", ""));
    mav.addObject("statusList", codeMgtService.selectCodeMgtList("USC", ""));

    mav.addObject("dashboardConfigList", userInfoService.getDashboardConfigList(loginUser.getUserSeq()));
    return mav;
  }

  @GetMapping("/getDashBoardConfig")
  public List<DashboardConfig> getDashBoardConfig(@AuthenticationPrincipal UserInfo loginUser){
    return userInfoService.getDashboardConfigList(loginUser.getUserSeq());
  }

  @PostMapping("/selectedMenuTable")
  @ResponseBody
  public ModelAndView dashboardConfigTable(@RequestBody List<DashboardConfig> selectMenuList){
    ModelAndView mav = new ModelAndView("user/dashboardConfigTable");
    mav.addObject("dashboardConfigList", selectMenuList);
    return mav;
  }

  @PostMapping("saveDashboardConfig")
  @ResponseBody
  public void saveDashboardConfig(@AuthenticationPrincipal UserInfo loginUser, @RequestBody List<DashboardConfig> configList){
    userInfoService.saveDashboardConfigList(loginUser.getUserSeq(), configList);
  }

  @PostMapping("/updateSelf")
  public void updateSelf(@AuthenticationPrincipal UserInfo loginUser, UserInfo userInfo) {
    userInfo.setUserSeq(loginUser.getUserSeq());
    userInfoService.updateUserInfo(loginUser,userInfo);
  }

  @PostMapping("/syncSelfToKwms")
  public void syncSelfToKwms(@AuthenticationPrincipal UserInfo loginUser, UserInfo userInfo) {
    UserInfo kwmsInfo = kwmsService.selectEmpInfo(userInfo.getDicCode());
    if(kwmsInfo!= null){
      kwmsInfo.setUserSeq(loginUser.getUserSeq());
      userInfoService.updateUserInfo(loginUser,kwmsInfo);
    }
  }
}
