package com.dbnt.faisp.config;

import com.dbnt.faisp.main.codeMgt.service.CodeMgtService;
import com.dbnt.faisp.main.faStatistics.crackdownsStatus.model.crackdownStatus.CrackdownStatus;
import com.dbnt.faisp.main.faStatistics.crackdownsStatus.repository.FishingBoatRepository;
import com.dbnt.faisp.main.faStatistics.crackdownsStatus.repository.ProcessResultRepository;
import com.dbnt.faisp.main.faStatistics.crackdownsStatus.repository.SailorRepository;
import com.dbnt.faisp.main.faStatistics.crackdownsStatus.repository.ViolationRepository;
import com.dbnt.faisp.main.faStatistics.crackdownsStatus.service.CrackdownStatusService;
import com.dbnt.faisp.main.menuMgt.model.MenuMgt;
import com.dbnt.faisp.main.menuMgt.service.MenuMgtService;
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.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;


@RestController
@RequiredArgsConstructor
@RequestMapping("/modal")
public class ModalController {

  private final MenuMgtService menuMgtService;
  private final UserInfoService userInfoService;
  private final CodeMgtService codeMgtService;
  private final CrackdownStatusService crackdownStatusService;
  private final ViolationRepository violationRepository;
  private final ProcessResultRepository processResultRepository;
  private final FishingBoatRepository fishingBoatRepository;
  private final SailorRepository sailorRepository;

  @GetMapping("/menuModal")
  public ModelAndView menuModalPage(@AuthenticationPrincipal UserInfo loginUser, MenuMgt menuMgt){
    ModelAndView mav = new ModelAndView("common/modal/menuModal");
    menuMgt.setUserSeq(loginUser.getUserSeq());
    menuMgt.setQueryInfo();
    mav.addObject("menuMgtList", menuMgtService.selectMenuMgtListToAccessAuth(menuMgt));
    menuMgt.setContentCnt(menuMgtService.selectMenuMgtListToAccessAuthCnt(menuMgt));
    menuMgt.setPaginationInfo();
    mav.addObject("searchParams", menuMgt);
    return mav;
  }

  @GetMapping("/userModal")
  public ModelAndView userMoadlPage(@AuthenticationPrincipal UserInfo loginUser, UserInfo userInfo){
    ModelAndView mav = new ModelAndView("common/modal/userModal");
    userInfo.setQueryInfo();
    if(userInfo.getUserStatus() == null || userInfo.getUserStatus().equals("")) {
      userInfo.setUserStatus("USC003");
    }
    mav.addObject("OgList", codeMgtService.selectCodeMgtList("OG", ""));
    mav.addObject("OfcList", codeMgtService.selectCodeMgtList("OFC", ""));
    mav.addObject("userInfoList", userInfoService.selectUserInfoList(userInfo));
    userInfo.setContentCnt(userInfoService.selectUserInfoListCnt(userInfo));
    userInfo.setPaginationInfo();
    mav.addObject("searchParams", userInfo);
    return mav;
  }

  @GetMapping("/crackdownStatusModal")
  public ModelAndView crackdownStatusModal(@AuthenticationPrincipal UserInfo loginUser, CrackdownStatus crackdownStatus){
    ModelAndView mav = new ModelAndView("common/modal/crackdownStatusModal");
    crackdownStatus.setQueryInfo();

    List<CrackdownStatus> crackdownList = crackdownStatusService.selectCrackdownStatusList(crackdownStatus);
    for (CrackdownStatus cds: crackdownList) {
      cds.setViolationList(violationRepository.findByFbKey(cds.getFbKey()));
      cds.setProcessResult(processResultRepository.findByCdsKey(cds.getCdsKey()).orElse(null));
      cds.setFishingBoat(fishingBoatRepository.findByCdsKey(cds.getCdsKey()).orElse(null));
      cds.setSailorList(sailorRepository.findByFbKey(cds.getFbKey()));
    }
    mav.addObject("crackdownList", crackdownList);
    crackdownStatus.setContentCnt(crackdownStatusService.selectCrackdownStatusListCnt(crackdownStatus));
    crackdownStatus.setPaginationInfo();
    mav.addObject("searchParams", crackdownStatus);
    return mav;
  }
}
