package com.its.safe.service;

import com.its.app.utils.Elapsed;
import com.its.safe.config.ApplicationConfig;
import com.its.safe.dao.mapper.safe.VmsSafeMapper;
import com.its.safe.dto.safe.TbMessageDto;
import com.its.safe.entity.safe.TbArea;
import com.its.safe.entity.safe.TbMessage;
import com.its.safe.entity.safe.TbReadCheck;
import com.its.safe.process.DbmsDataProcess;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.StringTokenizer;

@Slf4j
@Getter
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = {Exception.class})
public class VmsSafeAdapterService {

    private final ApplicationConfig appConfig;
    private final DbmsDataProcess dbmsDataProcess;
    private final VmsSafeMapper vmsSafeMapper;

    public void run() {
        Elapsed elapsed = new Elapsed();
        selectArea();

        selectNewSafeMessage();
        updateConnCheck();
        log.info("VmsSafeAdapterService.run:: ..end. {} ms. {}", elapsed.milliSeconds(), Thread.currentThread().getName());
    }

    public void selectNewSafeMessage() {
        Elapsed elapsed = new Elapsed();
        try {
            List<TbReadCheck> checkList = this.vmsSafeMapper.selectNewVmsSafeMessage(this.appConfig.getSafeSystemId());
            log.info("VmsSafeAdapterService.selectNewSafeMessage(), {} EA", checkList.size());
            for (TbReadCheck chkMessage : checkList) {
                log.info("VmsSafeAdapterService.selectNewSafeMessage: {}.", chkMessage.toDto());

                List<TbMessage> messageList = this.vmsSafeMapper.selectVmsSafeMessage(chkMessage.getMessageSeq());
                for (TbMessage message : messageList) {
                    TbMessageDto msgDto = message.toDto();
                    log.info("VmsSafeAdapterService.selectVmsSafeMessage: {}.", msgDto);

//                    String[] codes = msgDto.getAreaCode().split(",");   // 빈 문자열 분리.
//                    for (int ii = 0; ii < codes.length; ii++) {
//                        log.info("VmsSafeAdapterService.selectVmsSafeMessage: SEQ {}, {} EA, AREA_CODE [{}]", msgDto.getSeq(), codes.length, codes[ii]);
//                    }

                    StringTokenizer areaCodes = new StringTokenizer(msgDto.getAreaCode(), ",");
                    int areaCount = areaCodes.countTokens();
                    while(areaCodes.hasMoreTokens()){
                        String areaCode = areaCodes.nextToken().trim();
                        log.info("VmsSafeAdapterService.selectVmsSafeMessage: SEQ {}, {} EA, AREA_CODE [{}].", msgDto.getSeq(), areaCount, areaCode);
                    }
                }
            }
        }
        catch (Exception e) {
            log.error("VmsSafeAdapterService.selectNewSafeMessage: Exception: {}.", e.toString());
        }
        log.info("VmsSafeAdapterService.selectNewSafeMessage:: ..end. {} ms. {}", elapsed.milliSeconds(), Thread.currentThread().getName());
    }

    public int updateConfirmNewVmsSafeMessage(Long confirmSeq) {
        Elapsed elapsed = new Elapsed();
        int result = 0;
        try {
            result = this.vmsSafeMapper.updateConfirmNewVmsSafeMessage(confirmSeq);
            log.info("VmsSafeAdapterService.updateConfirmNewSafeMessage(), {}, {} EA", confirmSeq, result);
        }
        catch (Exception e) {
            log.error("VmsSafeAdapterService.updateConfirmNewSafeMessage: Exception: {}.", e.toString());
        }
        log.info("VmsSafeAdapterService.updateConfirmNewVmsSafeMessage:: ..end. {} ms. {}", elapsed.milliSeconds(), Thread.currentThread().getName());
        return result;
    }

    public void selectArea() {
        Elapsed elapsed = new Elapsed();
        try {
            List<TbArea> resultList = this.vmsSafeMapper.selectArea();
            log.info("VmsSafeAdapterService.selectArea(), {} EA", resultList.size());
            for (TbArea area : resultList) {
                log.info("VmsSafeAdapterService.selectArea: {}.", area.toDto());
            }
        }
        catch (Exception e) {
            log.error("VmsSafeAdapterService.selectArea: Exception: {}.", e.toString());
        }
        log.info("VmsSafeAdapterService.selectArea:: ..end. {} ms. {}", elapsed.milliSeconds(), Thread.currentThread().getName());
    }

    public void updateConnCheck() {
        Elapsed elapsed = new Elapsed();
        String systemId = this.appConfig.getSafeSystemId();
        int result = this.vmsSafeMapper.insertConnCheck(systemId);
        log.info("VmsSafeAdapterService.updateConnCheck(), {}, {} EA", systemId, result);
        log.info("VmsSafeAdapterService.updateConnCheck:: ..end. {} ms. {}", elapsed.milliSeconds(), Thread.currentThread().getName());
    }

}
