package com.its.vms.service;

import com.its.vms.dao.mapper.VmsIfscMapper;
import com.its.vms.dto.TbVmsIfscTrafDto;
import com.its.vms.dto.TbVmsRltnIfscDto;
import com.its.vms.entity.TbVmsCtlr;
import com.its.vms.entity.TbVmsIfscTraf;
import com.its.vms.entity.TbVmsRltnIfsc;
import com.its.vms.global.AppRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

@Slf4j
@Service
@RequiredArgsConstructor
public class VmsIfscService {

    private final VmsIfscMapper vmsIfscMapper;
    private final VmsPrcsService vmsPrcsService;

    public ConcurrentHashMap<Long, TbVmsIfscTraf> trafMap = new ConcurrentHashMap<>();

    public void loadVmsIfscInfo() {
        loadVmsRltnIfsc();
        loadVmsIfscTrafInfo();
    }

    public void loadVmsRltnIfsc() {
        AppRepository.getInstance().getCtlrMap().forEach((nmbr, obj) -> {
            obj.getRltnIfscMap().forEach((ifscId, ifsc) -> {
                ifsc.setUsed(false);
            });
        });

        try {
            List<TbVmsRltnIfscDto> infoList  = this.vmsIfscMapper.selectVmsRltnIfsc();
            log.info("VmsIfscService.loadVmsRltnIfsc(), {} EA", infoList.size());
            infoList.forEach(dto -> {
                TbVmsCtlr vmsObj = AppRepository.getInstance().getCtlrMap().get(dto.getVmsCtlrNmbr());
                if (vmsObj != null) {
                    TbVmsRltnIfsc rltnIfsc = vmsObj.getRltnIfscMap().get(dto.getVmsIfscId());
                    if (rltnIfsc == null) {
                        rltnIfsc = dto.toEntity();
                        vmsObj.getRltnIfscMap().put(dto.getVmsIfscId(), rltnIfsc);
                    }
                    rltnIfsc.setUsed(true);
                    if (this.vmsPrcsService.isStaticCycle()) {
                        boolean cngstCnfmYn = "Y".equals(dto.getCngstCnfmYn());
                        Integer cngstCnfmSped = dto.getCngstCnfmSped();
                        if (rltnIfsc.isCngstCnfmYn() != cngstCnfmYn || !Objects.equals(rltnIfsc.getCngstCnfmSped(), cngstCnfmSped)) {
                            rltnIfsc.setCngstCnt(0);
                        }
                    }
                    dto.rltnIfscCopy(rltnIfsc);
                }
            });
        }
        catch (Exception e) {
            log.error("VmsIfscService.loadVmsRltnIfsc: {}", e.toString());
        }
    }

    public void loadVmsIfscTrafInfo() {
        trafMap.forEach((ifscId, ifsc) -> {
            ifsc.setCmtrGradCd("0");
            ifsc.setSped(0);
            ifsc.setTrvlHh(0);
        });

        try {
            List<TbVmsIfscTrafDto> infoList  = this.vmsIfscMapper.selectVmsIfscTrafInfo();
            log.info("VmsIfscService.loadVmsIfscTrafInfo(), {} EA", infoList.size());
            infoList.forEach(dto -> {
                this.trafMap.put(dto.getVmsIfscId(), dto.toEntity());
            });
        }
        catch (Exception e) {
            log.error("VmsIfscService.loadVmsIfscTrafInfo: {}", e.toString());
        }
    }

}
