package com.dbnt.faisp.faStatistics.crackdownStatus.service;


import com.dbnt.faisp.config.BaseService;
import com.dbnt.faisp.faStatistics.crackdownStatus.mapper.CrackdownStatusMapper;
import com.dbnt.faisp.faStatistics.crackdownStatus.model.CrackdownStatus;
import com.dbnt.faisp.faStatistics.crackdownStatus.model.ProcessResult;
import com.dbnt.faisp.faStatistics.crackdownStatus.model.Sailor;
import com.dbnt.faisp.faStatistics.crackdownStatus.model.Violation;
import com.dbnt.faisp.faStatistics.crackdownStatus.repository.*;
import com.dbnt.faisp.faStatistics.internationalCrimeArrest.model.InternationalCrimeArrest;
import com.dbnt.faisp.faStatistics.internationalCrimeArrest.model.SuspectPersonInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
public class CrackdownStatusService extends BaseService {

    private final CrackdownStatusMapper crackdownStatusMapper;
    private final CrackdownStatusRepository crackdownStatusRepository;
    private final FishingBoatRepository fishingBoatRepository;
    private final ViolationRepository violationRepository;
    private final ProcessResultRepository processResultRepository;
    private final SailorRepository sailorRepository;

    public List<CrackdownStatus> selectCrackdownStatusList(CrackdownStatus crackdownStatus) {
        return crackdownStatusMapper.selectCrackdownStatusList(crackdownStatus);
    };

    public Integer selectCrackdownStatusListCnt(CrackdownStatus crackdownStatus) {
        return crackdownStatusMapper.selectCrackdownStatusListCnt(crackdownStatus);
    }

    public CrackdownStatus selectCrackdownStatus(Integer cdsKey) {
        CrackdownStatus savedCrackdownStatus = crackdownStatusRepository.findById(cdsKey).orElse(null);
        if (savedCrackdownStatus != null) {

        }
        return savedCrackdownStatus;
    }

    @Transactional
    public Integer saveCrackdownStatus(CrackdownStatus crackdownStatus) {
        if (crackdownStatus.getCrackdownBoatEtc() != null) {
            crackdownStatus.setCrackdownBoat(crackdownStatus.getCrackdownBoatEtc());
        }
        if (crackdownStatus.getCrackdownPoliceEtc() != null) {
            crackdownStatus.setCrackdownPolice(crackdownStatus.getCrackdownPoliceEtc());
        }

        Integer cdsKey = crackdownStatusRepository.save(crackdownStatus).getCdsKey();
        if (crackdownStatus.getFishingBoat() != null) {
            if (crackdownStatus.getFishingBoat().getBoatMaterialEtc() != null) {
                crackdownStatus.getFishingBoat().setBoatMaterial(crackdownStatus.getFishingBoat().getBoatMaterialEtc());
            }
            crackdownStatus.getFishingBoat().setCdsKey(cdsKey);
            Integer fbKey = fishingBoatRepository.save(crackdownStatus.getFishingBoat()).getFbKey();
            crackdownStatus.getFishingBoat().setFbKey(fbKey);
        }

        if (crackdownStatus.getProcessResult() != null) {
            if (crackdownStatus.getProcessResult().getProcessStatusEtc() != null) {
                crackdownStatus.getProcessResult().setProcessStatus(crackdownStatus.getProcessResult().getProcessStatusEtc());
            }
            crackdownStatus.getProcessResult().setCdsKey(cdsKey);
            Integer prKey = processResultRepository.save(crackdownStatus.getProcessResult()).getPrKey();
            crackdownStatus.getProcessResult().setPrKey(prKey);
        }

        /*if (internationalCrimeArrest.getDeleteSpiKeyList() != null) {
            suspectPersonInfoRepository.deleteAllByIdInQuery(internationalCrimeArrest.getDeleteSpiKeyList());
        }*/

        if (crackdownStatus.getViolationList() != null) {
            for(Violation violation: crackdownStatus.getViolationList()){
                if (violation.getViolationEtc() != null) {
                    violation.setViolation(violation.getViolationEtc());
                }
                violation.setFbKey(crackdownStatus.getFishingBoat().getFbKey());
            }
            violationRepository.saveAll(crackdownStatus.getViolationList());
        }

        if (crackdownStatus.getSailorList() != null) {
            for(Sailor sailor: crackdownStatus.getSailorList()){
                sailor.setFbKey(crackdownStatus.getFishingBoat().getFbKey());
            }
            sailorRepository.saveAll(crackdownStatus.getSailorList());
        }

        return cdsKey;
    }
}
