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


import com.dbnt.faisp.config.BaseService;
import com.dbnt.faisp.main.faStatistics.crackdownsStatus.mapper.CrackdownStatusMapper;
import com.dbnt.faisp.main.faStatistics.crackdownsStatus.model.crackdownStatus.CrackdownStatus;
import com.dbnt.faisp.main.faStatistics.crackdownsStatus.model.crackdownStatus.CrackdownStatusVersion;
import com.dbnt.faisp.main.faStatistics.crackdownsStatus.model.processResult.ProcessResultVersion;
import com.dbnt.faisp.main.faStatistics.crackdownsStatus.model.sailor.Sailor;
import com.dbnt.faisp.main.faStatistics.crackdownsStatus.model.fishingBoat.Violation;
import com.dbnt.faisp.main.faStatistics.crackdownsStatus.repository.*;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
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 CrackdownStatusVersionRepository crackdownStatusVersionRepository;
    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;
    }

    public List<CrackdownStatusVersion> selectCrackdownStatusVersionList(Integer cdsKey) {
        return crackdownStatusVersionRepository.findByCdsKey(cdsKey);
    }

    public CrackdownStatusVersion selectCrackdownStatusVersion(Integer versionNo, Integer cdsKey) {
        return crackdownStatusVersionRepository.findByVersionNoAndCdsKey(versionNo, cdsKey);
    }

    @Transactional
    public Integer saveCrackdownStatus(CrackdownStatus crackdownStatus) {
        if (crackdownStatus.getViolationDeleteKeyList() != null) {
            violationRepository.deleteAllByIdInQuery(crackdownStatus.getViolationDeleteKeyList());
        }
        if (crackdownStatus.getSailorDeleteKeyList() != null) {
            sailorRepository.deleteAllByIdInQuery(crackdownStatus.getSailorDeleteKeyList());
        }

        if (crackdownStatus.getCrackdownBoatEtc() != null) {
            crackdownStatus.setCrackdownBoat(crackdownStatus.getCrackdownBoatEtc());
        }
        if (crackdownStatus.getCrackdownPoliceEtc() != null) {
            crackdownStatus.setCrackdownPolice(crackdownStatus.getCrackdownPoliceEtc());
        }

        Integer cdsKey = crackdownStatusRepository.save(crackdownStatus).getCdsKey();
        saveCrackdownStatusVersion(crackdownStatus);

        if (crackdownStatus.getFishingBoat() != null) {
            if (crackdownStatus.getFishingBoat().getBoatMaterialEtc() != null) {
                crackdownStatus.getFishingBoat().setBoatMaterial(crackdownStatus.getFishingBoat().getBoatMaterialEtc());
            }
            if (crackdownStatus.getFishingBoat().getFisheryTypeEtc() != null) {
                crackdownStatus.getFishingBoat().setFisheryType(crackdownStatus.getFishingBoat().getFisheryTypeEtc());
            }

            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.getSailorList() != null) {
            for(Sailor sailor: crackdownStatus.getSailorList()){
                sailor.setFbKey(crackdownStatus.getFishingBoat().getFbKey());
            }
            sailorRepository.saveAll(crackdownStatus.getSailorList());
        }

        if (crackdownStatus.getViolationList().get(0).getViolation() != null) {
            Violation lastViolation = violationRepository.findTopByFbKeyOrderByViolationKeyDesc(crackdownStatus.getFbKey()).orElse(null);
            int violationKey = lastViolation==null?1:(lastViolation.getViolationKey()+1);

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

        return cdsKey;
    }

    public void saveCrackdownStatusVersion(CrackdownStatus crackdownStatus) {
        CrackdownStatusVersion lastVersion = crackdownStatusVersionRepository.findTopByCdsKeyOrderByVersionNoDesc(crackdownStatus.getCdsKey()).orElse(null);
        int versionNo = lastVersion==null?1:(lastVersion.getVersionNo()+1);

        CrackdownStatusVersion crackdownStatusVersion = new CrackdownStatusVersion();
        BeanUtils.copyProperties(crackdownStatus, crackdownStatusVersion);
        crackdownStatusVersion.setVersionNo(versionNo);

        crackdownStatusVersionRepository.save(crackdownStatusVersion);
    }
}
