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

import com.dbnt.faisp.main.authMgt.service.AuthMgtService;
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.crackdownStatus.CrackdownStatusVersion;
import com.dbnt.faisp.main.faStatistics.crackdownsStatus.model.processResult.ProcessResult;
import com.dbnt.faisp.main.faStatistics.crackdownsStatus.repository.*;
import com.dbnt.faisp.main.faStatistics.crackdownsStatus.service.CrackdownStatusService;
import com.dbnt.faisp.main.userInfo.model.UserInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

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

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

    private final AuthMgtService authMgtService;
    private final CrackdownStatusService crackdownStatusService;
    private final ViolationRepository violationRepository;
    private final ProcessResultRepository processResultRepository;
    private final FishingBoatRepository fishingBoatRepository;
    private final SailorRepository sailorRepository;

    @RequestMapping("/crackdownStatus")
    public ModelAndView crackdownStatus(@AuthenticationPrincipal UserInfo loginUser, CrackdownStatus crackdownStatus) {
        ModelAndView mav = new ModelAndView("faStatistics/crackdownStatus/crackdownStatus");

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

        mav.addObject("accessAuth", accessAuth);

        List<CrackdownStatus> crackdownStatusList = crackdownStatusService.selectCrackdownStatusList(crackdownStatus);

        for (CrackdownStatus cds:crackdownStatusList) {
            cds.setViolationList(violationRepository.findByFbKey(cds.getFbKey()));
            cds.setProcessResult(processResultRepository.findByCdsKey(cds.getCdsKey()).orElse(new ProcessResult()));
            cds.setFishingBoat(fishingBoatRepository.findByCdsKey(cds.getCdsKey()).orElse(new FishingBoat()));
            cds.setSailorList(sailorRepository.findByFbKey(cds.getFbKey()));
        }

        mav.addObject("crackdownStatusList", crackdownStatusList);
        mav.addObject("searchParams", crackdownStatus);
        return mav;
    }

    @GetMapping("/crackdownStatus/crackdownStatusViewModal")
    public ModelAndView crackdownStatusViewModal(@AuthenticationPrincipal UserInfo loginUser, CrackdownStatus crackdownStatus){
        ModelAndView mav = new ModelAndView("faStatistics/crackdownStatus/crackdownStatusViewModal");
        crackdownStatus = crackdownStatusService.selectCrackdownStatus(crackdownStatus.getCdsKey());
        crackdownStatus.setFishingBoat(fishingBoatRepository.findByCdsKey(crackdownStatus.getCdsKey()).orElse(null));
        crackdownStatus.setProcessResult(processResultRepository.findByCdsKey(crackdownStatus.getCdsKey()).orElse(null));
        crackdownStatus.setViolationList(violationRepository.findByFbKey(crackdownStatus.getFishingBoat().getFbKey()));
        crackdownStatus.setSailorList(sailorRepository.findByFbKey(crackdownStatus.getFishingBoat().getFbKey()));

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

    @GetMapping("/crackdownStatus/crackdownStatusEditModal")
    public ModelAndView crackdownStatusEditModal(@AuthenticationPrincipal UserInfo loginUser, CrackdownStatus crackdownStatus){
        ModelAndView mav = new ModelAndView("faStatistics/crackdownStatus/crackdownStatusEditModal");
        if(crackdownStatus.getCdsKey()!=null){
            crackdownStatus = crackdownStatusService.selectCrackdownStatus(crackdownStatus.getCdsKey());
            crackdownStatus.setFishingBoat(fishingBoatRepository.findByCdsKey(crackdownStatus.getCdsKey()).orElse(null));
            crackdownStatus.setProcessResult(processResultRepository.findByCdsKey(crackdownStatus.getCdsKey()).orElse(null));
            crackdownStatus.setViolationList(violationRepository.findByFbKey(crackdownStatus.getFishingBoat().getFbKey()));
            crackdownStatus.setSailorList(sailorRepository.findByFbKey(crackdownStatus.getFishingBoat().getFbKey()));
        }else{
            crackdownStatus.setWrtOrgan(loginUser.getOgCd());
            crackdownStatus.setWrtPart(loginUser.getOfcCd());
            crackdownStatus.setWrtUserGrd(loginUser.getGroupCd());
            crackdownStatus.setWrtOrgan(loginUser.getOgCd());
            crackdownStatus.setWrtUserNm(loginUser.getUserNm());
            crackdownStatus.setWrtDt(LocalDateTime.now());
            crackdownStatus.setFishingBoat(new FishingBoat());
            crackdownStatus.getFishingBoat().setWrtOrgan(loginUser.getOgCd());
            crackdownStatus.getFishingBoat().setWrtUserNm(loginUser.getUserNm());
            crackdownStatus.getFishingBoat().setWrtDt(LocalDateTime.now());
            crackdownStatus.setProcessResult(new ProcessResult());
            crackdownStatus.getProcessResult().setWrtOrgan(loginUser.getOgCd());
            crackdownStatus.getProcessResult().setWrtUserNm(loginUser.getUserNm());
            crackdownStatus.getProcessResult().setWrtDt(LocalDateTime.now());
        }
        mav.addObject("crackdownStatus", crackdownStatus);
        return mav;
    }

    @GetMapping("/crackdownStatus/crackdownStatusHistoryViewModal")
    public ModelAndView crackdownStatusHistoryViewModal(@AuthenticationPrincipal UserInfo loginUser, CrackdownStatus crackdownStatus){
        ModelAndView mav = new ModelAndView("faStatistics/crackdownStatus/crackdownStatusHistoryViewModal");
        List<CrackdownStatusVersion> crackdownStatusVersionList = crackdownStatusService.selectCrackdownStatusVersionList(crackdownStatus.getCdsKey());

        mav.addObject("crackdownStatusVersionList", crackdownStatusVersionList);
        mav.addObject("crackdownStatus", crackdownStatus);
        return mav;
    }

    @GetMapping("/crackdownStatus/crackdownStatusHistoryDetail")
    public ModelAndView crackdownStatusHistoryDetail(@AuthenticationPrincipal UserInfo loginUser, CrackdownStatusVersion crackdownStatusVersion){
        ModelAndView mav = new ModelAndView("faStatistics/crackdownStatus/crackdownStatusHistoryDetail");

        crackdownStatusVersion = crackdownStatusService.selectCrackdownStatusVersion(crackdownStatusVersion.getVersionNo(), crackdownStatusVersion.getCdsKey());

        mav.addObject("crackdownStatusVersion", crackdownStatusVersion);
        return mav;
    }

    @PostMapping("/crackdownStatus/saveCrackdownStatus")
    public Integer saveCrackdownStatus(@AuthenticationPrincipal UserInfo loginUser,
                                       CrackdownStatus crackdownStatus,
                                       FishingBoat fishingBoat,
                                       ProcessResult processResult){
        crackdownStatus.setWrtUserSeq(loginUser.getUserSeq());
        crackdownStatus.setFishingBoat(fishingBoat);
        crackdownStatus.setProcessResult(processResult);
        return crackdownStatusService.saveCrackdownStatus(crackdownStatus);
    }
}
