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.processResult.ProcessResult;
import com.dbnt.faisp.main.faStatistics.crackdownsStatus.model.processResult.ProcessResultVersion;
import com.dbnt.faisp.main.faStatistics.crackdownsStatus.repository.*;
import com.dbnt.faisp.main.faStatistics.crackdownsStatus.service.ProcessResultService;
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.servlet.ModelAndView;

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

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

    private final AuthMgtService authMgtService;
    private final ProcessResultService processResultService;
    private final ViolationRepository violationRepository;
    private final CrackdownStatusRepository crackdownStatusRepository;
    private final FishingBoatRepository fishingBoatRepository;
    private final ProcessResultRepository processResultRepository;

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

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

        mav.addObject("accessAuth", accessAuth);

        processResult.setQueryInfo();
        List<ProcessResult> processResultList = processResultService.selectProcessResultList(processResult);

        for (ProcessResult pr:processResultList) {
            pr.setCrackdownStatus(crackdownStatusRepository.findByCdsKey(pr.getCdsKey()).orElse(null));
            pr.setFishingBoat(fishingBoatRepository.findByCdsKey(pr.getCdsKey()).orElse(null));
            pr.setViolationList(violationRepository.findByFbKey(pr.getFishingBoat().getFbKey()));
        }

        mav.addObject("processResultList", processResultList);
        mav.addObject("searchParams", processResult);
        return mav;
    }

    @GetMapping("/processResult/processResultViewModal")
    public ModelAndView processResultViewModal(@AuthenticationPrincipal UserInfo loginUser, ProcessResult processResult){
        ModelAndView mav = new ModelAndView("faStatistics/processResult/processResultViewModal");
        processResult = processResultService.selectProcessResult(processResult.getPrKey());
        processResult.setCrackdownStatus(crackdownStatusRepository.findByCdsKey(processResult.getCdsKey()).orElse(null));
        processResult.setFbKey(fishingBoatRepository.findByCdsKey(processResult.getCdsKey()).orElse(null).getFbKey());
        processResult.setBoatNameKr(fishingBoatRepository.findByCdsKey(processResult.getCdsKey()).orElse(null).getBoatNameKr());
        processResult.setViolationList(violationRepository.findByFbKey(processResult.getFbKey()));

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

    @GetMapping("/processResult/processResultEditModal")
    public ModelAndView crackdownStatusEditModal(@AuthenticationPrincipal UserInfo loginUser, ProcessResult processResult){
        ModelAndView mav = new ModelAndView("faStatistics/processResult/processResultEditModal");
        if(processResult.getPrKey()!=null){
            processResult = processResultService.selectProcessResult(processResult.getPrKey());
            processResult.setCrackdownStatus(crackdownStatusRepository.findByCdsKey(processResult.getCdsKey()).orElse(null));
            processResult.setFbKey(fishingBoatRepository.findByCdsKey(processResult.getCdsKey()).orElse(null).getFbKey());
            processResult.setBoatNameKr(fishingBoatRepository.findByCdsKey(processResult.getCdsKey()).orElse(null).getBoatNameKr());
            processResult.setViolationList(violationRepository.findByFbKey(processResult.getFbKey()));
        }else{
            processResult.setWrtOrgan(loginUser.getOgCd());
            processResult.setWrtPart(loginUser.getOfcCd());
            processResult.setWrtUserGrd(loginUser.getGroupCd());
            processResult.setWrtUserNm(loginUser.getUserNm());
            processResult.setWrtDt(LocalDateTime.now());
        }
        mav.addObject("processResult", processResult);
        return mav;
    }

    @GetMapping("/processResult/processResultHistoryViewModal")
    public ModelAndView processResultHistoryViewModal(@AuthenticationPrincipal UserInfo loginUser, ProcessResult processResult){
        ModelAndView mav = new ModelAndView("faStatistics/processResult/processResultHistoryViewModal");
        List<ProcessResultVersion> processReulstVersionList = processResultService.selectProcessResultVersionList(processResult.getPrKey());

        mav.addObject("processReulstVersionList", processReulstVersionList);
        mav.addObject("processResult", processResult);
        return mav;
    }

    @GetMapping("/processResult/processResultHistoryDetail")
    public ModelAndView processResultHistoryDetail(@AuthenticationPrincipal UserInfo loginUser, ProcessResultVersion processResultVersion){
        ModelAndView mav = new ModelAndView("faStatistics/processResult/processResultHistoryDetail");

        Integer cdsKey = processResultVersion.getCdsKey();
        Integer fbKey = processResultVersion.getFbKey();

        processResultVersion = processResultService.selectProcessResultVersion(processResultVersion.getVersionNo(), processResultVersion.getPrKey());
        processResultVersion.setFishingBoat(fishingBoatRepository.findByFbKey(fbKey).orElse(null));
        processResultVersion.setCrackdownStatus(crackdownStatusRepository.findByCdsKey(cdsKey).orElse(null));
        processResultVersion.setViolationList(violationRepository.findByFbKey(fbKey));

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

    @GetMapping("/processResult/processResultAddModal")
    public ModelAndView sailorAddModal(@AuthenticationPrincipal UserInfo loginUser, ProcessResult processResult){
        ModelAndView mav = new ModelAndView("faStatistics/processResult/processResultAddModal");

        processResult.setCrackdownStatusList(crackdownStatusRepository.findAll());

        processResult.setWrtOrgan(loginUser.getOgCd());
        processResult.setWrtUserNm(loginUser.getUserNm());
        processResult.setWrtDt(LocalDateTime.now());

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

    @PostMapping("/processResult/getProcessResult")
    public ProcessResult getProcessResult(@AuthenticationPrincipal UserInfo loginUser, ProcessResult processResult){
        processResult = processResultRepository.findByCdsKey(processResult.getCdsKey()).orElse(null);
        processResult.setCrackdownStatus(crackdownStatusRepository.findByCdsKey(processResult.getCdsKey()).orElse(null));
        processResult.setFishingBoat(fishingBoatRepository.findByCdsKey(processResult.getCdsKey()).orElse(null));

        processResult.setWrtOrgan(loginUser.getOgCd());
        processResult.setWrtUserNm(loginUser.getUserNm());
        processResult.setWrtDt(LocalDateTime.now());
        return processResult;
    }

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