package com.its.bis.service;

import com.its.app.utils.Elapsed;
import com.its.bis.config.ApplicationConfig;
import com.its.bis.dao.mapper.VmsCtlrMapper;
import com.its.bis.dto.TbVmsCtlrDto;
import com.its.bis.entity.TbVmsCtlr;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.io.File;
import java.util.List;

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

    private final ApplicationConfig config;
    private final AppRepositoryService repoService;
    private final VmsCtlrMapper mapper;
    private int historyMin;

    @PostConstruct
    private void init() {
        log.info("VmsCtlrService.init: Start.");
        this.historyMin = -1;
        log.info("VmsCtlrService.init: ..End.");
    }

    public void loadDb() {
        loadCtlrInfo();
    }

    public void loadCtlrInfo() {
        Elapsed elapsed = new Elapsed();
        this.repoService.initCtlrInfo();

        try {
            List<TbVmsCtlr> infoList  = this.mapper.selectAll();
            log.info("VmsCtlrService.loadCtlrInfo: {} EA", infoList.size());
            for (TbVmsCtlr dto : infoList) {
                log.info("VmsCtlrService.loadCtlrInfo: {}.", dto);
                TbVmsCtlrDto vmsObj = dto.toDto();
                if (vmsObj.getCtlrId().trim().equals("")) {
                    log.error("VmsCtlrService.loadCtlrInfo: Controller Ip Address Error: {}.", vmsObj);
                    continue;
                }

                vmsObj.setGroupNo(0L);

                this.repoService.putCtlrMap(vmsObj);
                this.repoService.putIpAddrMap(vmsObj);
                vmsObj.getStts().initUnknown();

                if (vmsObj.getMaxPhaseNum() > this.config.getMaxDownloadForms()) {
                    log.error("VmsCtlrService.loadCtlrInfo: VMS {} Download Form Size Error: {}/{}.", vmsObj.getVmsCtlrNmbr(), vmsObj.getMaxPhaseNum(), this.config.getMaxDownloadForms());
                    vmsObj.setMaxPhaseNum(this.config.getMaxDownloadForms());
                }
                vmsObj.setLocalFormDir(this.config.getFtpFormDir() + File.separator + vmsObj.getVmsCtlrNmbr() + File.separator);
                vmsObj.setFtpFormDir(ApplicationConfig.FTP_FORM + File.separator + vmsObj.getVmsCtlrNmbr() + File.separator);
                vmsObj.setFtpDownload(true);

                this.config.makeDirectory(vmsObj.getLocalFormDir(), "VMS " + vmsObj.getVmsCtlrNmbr() + " Directory.");
            }
        }
        catch (Exception e) {
            log.error("VmsCtlrService.loadCtlrInfo: {}.", e.toString());
        }

        this.repoService.getCtlrMap().forEach((key, obj) -> {
            log.info("VmsCtlrService.loadCtlrInfo:: {}.", obj.toString());
        });
        log.info("VmsCtlrService.loadCtlrInfo: {} ms.", elapsed.milliSeconds());
    }

}
