package com.dbnt.sims.faStatistics.internationalCrimeArrest;

import com.dbnt.sims.authMgt.service.AuthMgtService;
import com.dbnt.sims.codeMgt.model.CodeMgt;
import com.dbnt.sims.codeMgt.service.CodeMgtService;
import com.dbnt.sims.faStatistics.internationalCrimeArrest.model.InternationalCrimeArrest;
import com.dbnt.sims.faStatistics.internationalCrimeArrest.repository.SuspectPersonInfoRepository;
import com.dbnt.sims.faStatistics.internationalCrimeArrest.service.InternationalCrimeArrestService;
import com.dbnt.sims.organMgt.model.OrganConfig;
import com.dbnt.sims.organMgt.service.OrganConfigService;
import com.dbnt.sims.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 InternationalCrimeArrestController {

    private final AuthMgtService authMgtService;
    private final InternationalCrimeArrestService internationalCrimeArrestService;
    private final CodeMgtService codeMgtService;
    private final OrganConfigService organConfigService;
    private final SuspectPersonInfoRepository suspectPersonInfoRepository;

    @GetMapping("/internationalCrimeArrest")
    public ModelAndView internationalCrimeArrest(@AuthenticationPrincipal UserInfo loginUser,InternationalCrimeArrest internationalCrimeArrest) {
        ModelAndView mav = new ModelAndView("faStatistics/internationalCrimeArrest/internationalCrimeArrest");

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

        mav.addObject("accessAuth", accessAuth);
        mav.addObject("mgtOrganList", loginUser.getDownOrganCdList());

        internationalCrimeArrest.setQueryInfo();
        List<InternationalCrimeArrest> internationalCrimeArrestList = internationalCrimeArrestService.selectInternationalCrimeArrestList(internationalCrimeArrest);

        for (InternationalCrimeArrest ica:internationalCrimeArrestList) {
            ica.setSuspectPersonInfoList(suspectPersonInfoRepository.findByIcaKey(ica.getIcaKey()));
        }

        mav.addObject("internationalCrimeArrestList", internationalCrimeArrestList);
        internationalCrimeArrest.setContentCnt(internationalCrimeArrestService.selectInternationalCrimeArrestListCnt(internationalCrimeArrest));
        internationalCrimeArrest.setPaginationInfo();
        mav.addObject("organConfigList", selectOrganList());
        mav.addObject("searchParams", internationalCrimeArrest);
        return mav;
    }

    @GetMapping("/internationalCrimeArrest/internationalCrimeArrestEditModal")
    public ModelAndView internationalCrimeArrestEditModal(@AuthenticationPrincipal UserInfo loginUser, InternationalCrimeArrest internationalCrimeArrest){
        ModelAndView mav = new ModelAndView("faStatistics/internationalCrimeArrest/internationalCrimeArrestEditModal");
        if(internationalCrimeArrest.getIcaKey()!=null){
            internationalCrimeArrest = internationalCrimeArrestService.selectInternationalCrimeArrest(internationalCrimeArrest.getIcaKey());
        }else{
            internationalCrimeArrest.setWrtOrgan(loginUser.getOgCd());
            internationalCrimeArrest.setWrtNm(loginUser.getUserNm());
            internationalCrimeArrest.setWrtDt(LocalDateTime.now());
            internationalCrimeArrest.setSuspectPersonInfoList(suspectPersonInfoRepository.findByIcaKey(internationalCrimeArrest.getIcaKey()));
        }
        mav.addObject("organConfigList", selectOrganList());
        mav.addObject("internationalCrimeArrest", internationalCrimeArrest);
        return mav;
    }

    @GetMapping("/internationalCrimeArrest/internationalCrimeArrestViewModal")
    public ModelAndView internationalCrimeArrestViewModal(@AuthenticationPrincipal UserInfo loginUser, InternationalCrimeArrest internationalCrimeArrest){
        ModelAndView mav = new ModelAndView("faStatistics/internationalCrimeArrest/internationalCrimeArrestViewModal");
        internationalCrimeArrest = internationalCrimeArrestService.selectInternationalCrimeArrest(internationalCrimeArrest.getIcaKey());
        mav.addObject("internationalCrimeArrest", internationalCrimeArrest);
        mav.addObject("userSeq",loginUser.getUserSeq());
        mav.addObject("organConfigList", selectOrganList());
        //메뉴권한 확인
        mav.addObject("accessAuth", authMgtService.selectAccessConfigList(loginUser.getUserSeq(), "/faStatistics/internationalCrimeArrest").get(0).getAccessAuth());
        return mav;
    }

    @PostMapping("/internationalCrimeArrest/saveInternationalCrimeArrest")
    public Integer saveInternationalCrimeArrest(@AuthenticationPrincipal UserInfo loginUser,
                                                InternationalCrimeArrest internationalCrimeArrest){
        internationalCrimeArrest.setWrtUserSeq(loginUser.getUserSeq());
        return internationalCrimeArrestService.savedInternationalCrimeArrest(internationalCrimeArrest);
    }

    public List<OrganConfig> selectOrganList(){
        List<CodeMgt> organCdList = codeMgtService.selectCodeMgtList("OG");
        List<OrganConfig> organConfigList = organConfigService.selectSavedOrganList();
        for(CodeMgt organCode: organCdList){
            if(!organCode.getItemCd().equals("OG001")){
                boolean usedFlag = false;
                for(OrganConfig organConfig: organConfigList){
                    if(organCode.getItemCd().equals(organConfig.getOrganCd())){
                        usedFlag = true;
                        organConfig.setOrganNm(organCode.getItemValue());
                        organConfig.setUseState("T");
                        break;
                    }
                }
                if(!usedFlag){
                    OrganConfig organConfig = new OrganConfig();
                    organConfig.setOrganCd(organCode.getItemCd());
                    organConfig.setOrganNm(organCode.getItemValue());
                    organConfig.setUseState("F");
                    organConfigList.add(organConfig);
                }
            }
        }
        return organConfigList;
    }
}
