package com.dbnt.faisp.main.faStatistics.crackdownsStatus;

import com.dbnt.faisp.main.authMgt.service.AuthMgtService;
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.model.fishingBoat.FishingBoat;
import com.dbnt.faisp.main.faStatistics.crackdownsStatus.model.fishingBoat.FishingBoatVersion;
import com.dbnt.faisp.main.faStatistics.crackdownsStatus.model.processResult.ProcessResult;
import com.dbnt.faisp.main.faStatistics.crackdownsStatus.service.FishingBoatService;
import com.dbnt.faisp.main.userInfo.model.UserInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/faStatistics")
public class FishingBoatController {

  private final AuthMgtService authMgtService;
  private final FishingBoatService fishingBoatService;
  private final CodeMgtService codeMgtService;

  @RequestMapping("/fishingBoat")
  public ModelAndView fishingBoat(@AuthenticationPrincipal UserInfo loginUser, CrackdownStatus crackdownStatus) {
    ModelAndView mav = new ModelAndView("faStatistics/fishingBoat/fishingBoatMgt");
    if(crackdownStatus.getFishingBoat()==null){
      crackdownStatus.setFishingBoat(new FishingBoat());
      crackdownStatus.setProcessResult(new ProcessResult());
    }
    //메뉴권한 확인
    String accessAuth = authMgtService.selectAccessConfigList(loginUser.getUserSeq(), "/faStatistics/fishingBoat").get(0).getAccessAuth();
    mav.addObject("accessAuth", accessAuth);

    crackdownStatus.setQueryInfo();
    List<CrackdownStatus> crackdownStatusList = fishingBoatService.selectFishingBoatList(crackdownStatus);

    mav.addObject("crackdownStatusList", crackdownStatusList);
    crackdownStatus.setContentCnt(fishingBoatService.selectFishingBoatListCnt(crackdownStatus));
    crackdownStatus.setPaginationInfo();
    mav.addObject("searchParams", crackdownStatus);
    return mav;
  }

  @GetMapping("/fishingBoatViewModal")
  public ModelAndView fishingBoatViewModal(@AuthenticationPrincipal UserInfo loginUser, CrackdownStatus crackdownStatus){
    ModelAndView mav = new ModelAndView("faStatistics/fishingBoat/fishingBoatViewModal");
    crackdownStatus = fishingBoatService.selectCrackdownStatus(crackdownStatus.getCdsKey());

    mav.addObject("crackdownStatus", crackdownStatus);
    //메뉴권한 확인
    mav.addObject("accessAuth", authMgtService.selectAccessConfigList(loginUser.getUserSeq(), "/faStatistics/processResult").get(0).getAccessAuth());

    mav.addObject("ataList", codeMgtService.selectCodeMgtList("ATA", ""));
    mav.addObject("cpoList", codeMgtService.selectCodeMgtList("CPO", ""));
    mav.addObject("vtList", codeMgtService.selectCodeMgtList("VT", ""));
    mav.addObject("ftList", codeMgtService.selectCodeMgtList("FT", ""));
    mav.addObject("bmList", codeMgtService.selectCodeMgtList("BM", ""));
    mav.addObject("prList", codeMgtService.selectCodeMgtList("PR", ""));
    mav.addObject("bdtList", codeMgtService.selectCodeMgtList("BDT", ""));
    mav.addObject("boatList", codeMgtService.selectCodeMgtList(crackdownStatus.getCrackdownPolice(), ""));
    mav.addObject("userSeq", loginUser.getUserSeq());
    return mav;
  }

  @GetMapping("/fishingBoatEditModal")
  public ModelAndView fishingBoatEditModal(@AuthenticationPrincipal UserInfo loginUser, CrackdownStatus crackdownStatus){
    ModelAndView mav = new ModelAndView("faStatistics/fishingBoat/fishingBoatEditModal");
    if(crackdownStatus.getCdsKey()!=null){
      crackdownStatus = fishingBoatService.selectCrackdownStatus(crackdownStatus.getCdsKey());
    }else{
      crackdownStatus.setFishingBoat(new FishingBoat());
      crackdownStatus.setProcessResult(new ProcessResult());
      crackdownStatus.getFishingBoat().setWrtOrgan(loginUser.getOgCd());
      crackdownStatus.getFishingBoat().setWrtPart(loginUser.getOfcCd());
      crackdownStatus.getFishingBoat().setWrtUserSeq(loginUser.getUserSeq());
      crackdownStatus.getFishingBoat().setWrtUserNm(loginUser.getUserNm());
      crackdownStatus.getFishingBoat().setWrtUserGrd(loginUser.getTitleCd());
    }
    mav.addObject("ataList", codeMgtService.selectCodeMgtList("ATA", ""));
    mav.addObject("cpoList", codeMgtService.selectCodeMgtList("CPO", ""));
    mav.addObject("vtList", codeMgtService.selectCodeMgtList("VT", ""));
    mav.addObject("ftList", codeMgtService.selectCodeMgtList("FT", ""));
    mav.addObject("bmList", codeMgtService.selectCodeMgtList("BM", ""));
    mav.addObject("prList", codeMgtService.selectCodeMgtList("PR", ""));
    mav.addObject("bdtList", codeMgtService.selectCodeMgtList("BDT", ""));
    mav.addObject("crackdownStatus", crackdownStatus);
    return mav;
  }

  @PostMapping("/saveFishingBoat")
  public Integer saveFishingBoat(@AuthenticationPrincipal UserInfo loginUser,
                                 CrackdownStatus crackdownStatus,
                                 MultipartHttpServletRequest request){
    return fishingBoatService.saveCrackdownStatus(crackdownStatus, request.getMultiFileMap().get("uploadFiles"));
  }

  @GetMapping("/checkCaseNum")
  public Integer checkCaseNum(String caseNum){
    return fishingBoatService.checkCaseNum(caseNum);
  }

  @GetMapping("/fishingBoatVersionInfo")
  public ModelAndView fishingBoatVersionInfo(FishingBoatVersion version){
    ModelAndView mav = new ModelAndView("faStatistics/fishingBoat/fishingBoardVersionInfo");
    mav.addObject("fishingBoat", fishingBoatService.selectFishingBoatVersion(version.getFbKey(), version.getVersionNo()));
    mav.addObject("vtList", codeMgtService.selectCodeMgtList("VT", ""));
    mav.addObject("ftList", codeMgtService.selectCodeMgtList("FT", ""));
    mav.addObject("bmList", codeMgtService.selectCodeMgtList("BM", ""));
    return mav;
  }
}
