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.sailor.Sailor;
import com.dbnt.faisp.main.faStatistics.crackdownsStatus.model.sailor.SailorVersion;
import com.dbnt.faisp.main.faStatistics.crackdownsStatus.repository.*;
import com.dbnt.faisp.main.faStatistics.crackdownsStatus.service.SailorService;
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 SailorController {

    private final AuthMgtService authMgtService;
    private final SailorService sailorService;
    private final ViolationRepository violationRepository;
    private final CrackdownStatusRepository crackdownStatusRepository;
    private final FishingBoatRepository fishingBoatRepository;
    private final SailorRepository sailorRepository;

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

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

        mav.addObject("accessAuth", accessAuth);
        List<Sailor> sailorList = sailorService.selectSailorList(sailor);

        for (Sailor s:sailorList) {
            s.setFishingBoat(fishingBoatRepository.findByFbKey(s.getFbKey()).orElse(null));
            s.setCrackdownStatus(crackdownStatusRepository.findByCdsKey(s.getFishingBoat().getCdsKey()).orElse(null));
            s.setViolationList(violationRepository.findByFbKey(s.getFishingBoat().getFbKey()));
        }

        mav.addObject("sailorList", sailorList);
        mav.addObject("searchParams", sailor);
        return mav;
    }

    @GetMapping("/sailor/sailorViewModal")
    public ModelAndView sailorViewModal(@AuthenticationPrincipal UserInfo loginUser, Sailor sailor){
        ModelAndView mav = new ModelAndView("faStatistics/sailor/sailorViewModal");
        sailor = sailorService.selectSailor(sailor.getSailorKey());
        sailor.setFishingBoat(fishingBoatRepository.findByFbKey(sailor.getFbKey()).orElse(null));
        sailor.setCrackdownStatus(crackdownStatusRepository.findByCdsKey(sailor.getFishingBoat().getCdsKey()).orElse(null));
        sailor.setViolationList(violationRepository.findByFbKey(sailor.getFishingBoat().getFbKey()));

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

    @GetMapping("/sailor/sailorHistoryViewModal")
    public ModelAndView sailorHistoryViewModal(@AuthenticationPrincipal UserInfo loginUser, Sailor sailor){
        ModelAndView mav = new ModelAndView("faStatistics/sailor/sailorHistoryViewModal");
        List<SailorVersion> sailorVersionList = sailorService.selectSailorVersionList(sailor.getSailorKey());

        mav.addObject("sailorVersionList", sailorVersionList);
        mav.addObject("sailor", sailor);
        return mav;
    }

    @GetMapping("/sailor/sailorHistoryDetail")
    public ModelAndView sailorHistoryDetail(@AuthenticationPrincipal UserInfo loginUser, SailorVersion sailorVersion){
        ModelAndView mav = new ModelAndView("faStatistics/sailor/sailorHistoryDetail");

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

        sailorVersion = sailorService.selectSailorVersion(sailorVersion.getVersionNo(), sailorVersion.getSailorKey());
        sailorVersion.setFishingBoat(fishingBoatRepository.findByFbKey(fbKey).orElse(null));
        sailorVersion.setCrackdownStatus(crackdownStatusRepository.findByCdsKey(cdsKey).orElse(null));

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

    @GetMapping("/sailor/sailorEditModal")
    public ModelAndView sailorEditModal(@AuthenticationPrincipal UserInfo loginUser, Sailor sailor){
        ModelAndView mav = new ModelAndView("faStatistics/sailor/sailorEditModal");
        sailor = sailorService.selectSailor(sailor.getSailorKey());
        sailor.setFishingBoat(fishingBoatRepository.findByFbKey(sailor.getFbKey()).orElse(null));
        sailor.setCrackdownStatus(crackdownStatusRepository.findByCdsKey(sailor.getFishingBoat().getCdsKey()).orElse(null));
        sailor.setViolationList(violationRepository.findByFbKey(sailor.getFishingBoat().getFbKey()));

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

    @GetMapping("/sailor/sailorAddModal")
    public ModelAndView sailorAddModal(@AuthenticationPrincipal UserInfo loginUser, Sailor sailor){
        ModelAndView mav = new ModelAndView("faStatistics/sailor/sailorAddModal");

        sailor.setFishingBoatList(fishingBoatRepository.findAll());

        sailor.setWrtOrgan(loginUser.getOgCd());
        sailor.setWrtPart(loginUser.getOfcCd());
        sailor.setWrtUserGrd(loginUser.getGroupCd());
        sailor.setWrtUserNm(loginUser.getUserNm());
        sailor.setWrtDt(LocalDateTime.now());

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

    @PostMapping("/sailor/saveSailor")
    public Integer saveSailor(@AuthenticationPrincipal UserInfo loginUser,
                                     CrackdownStatus crackdownStatus,
                                     FishingBoat fishingBoat,
                                     Sailor sailor){
        sailor.setWrtUserSeq(loginUser.getUserSeq());
        sailor.setFishingBoat(fishingBoat);
        sailor.setCrackdownStatus(crackdownStatus);
        return sailorService.saveSailor(sailor);
    }
}
