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


import com.dbnt.faisp.config.BaseService;
import com.dbnt.faisp.main.faStatistics.crackdownsStatus.mapper.ProcessResultMapper;
import com.dbnt.faisp.main.faStatistics.crackdownsStatus.model.*;
import com.dbnt.faisp.main.faStatistics.crackdownsStatus.model.crackdownStatus.CrackdownStatus;
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.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.util.Utils;
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 ProcessResultService extends BaseService {

    private final ProcessResultMapper processResultMapper;
    private final CrackdownStatusRepository crackdownStatusRepository;
    private final FishingBoatRepository fishingBoatRepository;
    private final ViolationRepository violationRepository;
    private final ProcessResultRepository processResultRepository;
    private final ProcessResultVersionRepository processResultVersionRepository;

    public List<ProcessResult> selectProcessResultList(ProcessResult processResult) {
        return processResultMapper.selectProcessResultList(processResult);
    }

    public Integer selectProcessResultListCnt(ProcessResult processResult) {
        return processResultMapper.selectProcessResultListCnt(processResult);
    }

    public ProcessResult selectProcessResult(Integer prKey) {
        return processResultRepository.findById(prKey).orElse(null);
    }

    public List<ProcessResultVersion> selectProcessResultVersionList(Integer prKey) {
        return processResultVersionRepository.findByPrKey(prKey);
    }

    public ProcessResultVersion selectProcessResultVersion(Integer versionNo, Integer prKey) {
        return processResultVersionRepository.findByVersionNoAndPrKey(versionNo, prKey);
    }

    @Transactional
    public Integer saveProcessResult(ProcessResult processResult) {
        if (processResult.getCrackdownStatus().getViolationDeleteKeyList() != null) {
            violationRepository.deleteAllByIdInQuery(processResult.getCrackdownStatus().getViolationDeleteKeyList());
        }

        if (processResult.getExecutionDetailEtc() != null) {
            processResult.setExecutionDetail(processResult.getExecutionDetailEtc());
        }

        Integer prKey = 0;
        ProcessResult existingProcessResult = processResultRepository.findByPrKey(processResult.getPrKey()).orElse(null);

        if (existingProcessResult != null) {
            Utils.copyNonNullProperties(processResult, existingProcessResult);
            existingProcessResult.setCrackdownStatus(null);
            existingProcessResult.setFishingBoat(null);
            existingProcessResult.setViolationList(null);
            prKey = processResultRepository.save(existingProcessResult).getPrKey();
            saveProcessResultVersion(existingProcessResult);
        } else {
            prKey = processResultRepository.save(processResult).getPrKey();
            saveProcessResultVersion(processResult);
        }

        /*if (processResult.getCrackdownStatus() != null) {
            if (processResult.getCrackdownStatus().getCrackdownPoliceEtc() != null) {
                processResult.getCrackdownStatus().setCrackdownPolice(processResult.getCrackdownStatus().getCrackdownPoliceEtc());
            }

            CrackdownStatus existingCrackdownStatus = crackdownStatusRepository.findByCdsKey(processResult.getCdsKey()).orElse(null);
            Utils.copyNonNullProperties(processResult, existingCrackdownStatus);
            processResult.setCrackdownStatus(crackdownStatusRepository.save(existingCrackdownStatus));
        }*/

        /*if (processResult.getFishingBoat() != null) {
            FishingBoat existingFishingBoat = fishingBoatRepository.findByCdsKey(processResult.getCdsKey()).orElse(null);
            Utils.copyNonNullProperties(processResult, existingFishingBoat);
            fishingBoatRepository.save(existingFishingBoat);
        }*/

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

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

        return prKey;
    }

    public void saveProcessResultVersion(ProcessResult processResult) {
        ProcessResultVersion lastVersion = processResultVersionRepository.findTopByPrKeyOrderByVersionNoDesc(processResult.getPrKey()).orElse(null);
        int versionNo = lastVersion==null?1:(lastVersion.getVersionNo()+1);

        ProcessResultVersion processResultVersion = new ProcessResultVersion();
        BeanUtils.copyProperties(processResult, processResultVersion);
        processResultVersion.setVersionNo(versionNo);

        processResultVersionRepository.save(processResultVersion);
    }
}
