package com.its.app.service;

import com.its.app.config.AppUtils;
import com.its.app.mapper.WcamMapper;
import com.its.app.process.DbmsJobType;
import com.its.app.repository.AppRepository;
import com.its.app.thread.IcmpPingThread;
import com.its.app.vo.voIcmpCtlr;
import com.its.app.vo.voIcmpCtlrStts;
import com.its.utils.SysUtils;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Slf4j
@Service("wcamService")
@Transactional(rollbackFor = {Exception.class})
public class WcamService {

    private WcamMapper ctlrMapper;
    private ConcurrentHashMap<String, voIcmpCtlr> icmpCtlrMap = null;

    @PostConstruct
    private void init() {
        log.info("WcamService.init");
        this.ctlrMapper = (WcamMapper) AppUtils.getBean(WcamMapper.class);
        this.icmpCtlrMap = new ConcurrentHashMap<>();
        //loadDb();
        //doJob();
    }

    public void loadDb() {
        try {
            List<voIcmpCtlr> infoList = this.ctlrMapper.selectAll();
            for (voIcmpCtlr obj: infoList) {
                this.icmpCtlrMap.put(obj.getCTLR_NMBR(), obj);
            }
            AppRepository.getInstance().setWcamCtlrMap(this.icmpCtlrMap);
        }
        catch (Exception e) {
            log.error("WcamService.loadDb: Exception: {}", e.toString());
        }
    }

    public void initJob() {
        try {
            for (Map.Entry<String, voIcmpCtlr> obj : this.icmpCtlrMap.entrySet()) {
                obj.getValue().setDEL_YN("Y");
            }
            loadDb();
        }
        catch (Exception e) {
            log.error("WcamService.initJob: Exception: {}", e.toString());
        }
    }

    public void doJob() {
        String keyData = "wcam";
        MDC.put("id", keyData);
        log.info("WcamService.doJob: Start...");

        boolean insHs = false;
        Calendar cal = Calendar.getInstance();
        int min = cal.get(Calendar.MINUTE);
        if ((min % 5) == 0) {
            insHs = true;
            initJob();
        }
        String UPDT_DT = SysUtils.getSysTime();

        for (Map.Entry<String, voIcmpCtlr> obj : this.icmpCtlrMap.entrySet()) {
            if (obj.getValue().getDEL_YN().equals("Y")) {
                log.info("WcamService.doJob: app WCAM: {}, {}, {}, DELETED.", obj.getValue().getCTLR_NMBR(), obj.getValue().getISTL_LCTN_NM(), obj.getValue().getCTLR_IP());
                continue;
            }
            //log.info("WcamService.doJob: app WCAM: {}, {}, {}", obj.getValue().getCTLR_NMBR(), obj.getValue().getISTL_LCTN_NM(), obj.getValue().getCTLR_IP());
            IcmpPingThread handler = (IcmpPingThread) AppUtils.getBean(IcmpPingThread.class);
            handler.run(DbmsJobType.DATA_TYPE_WCAM_STTS, insHs, UPDT_DT, obj.getValue());
        }
        log.info("WcamService.doJob: ..End...");
        MDC.remove(keyData);
        MDC.clear();
    }

    public void updateCtlrStts(boolean isRun) {
        String keyData = "wcam";
        MDC.put("id", keyData);

        boolean insHs = false;
        Calendar cal = Calendar.getInstance();
        int min = cal.get(Calendar.MINUTE);
        if ((min % 5) == 0) {
            insHs = true;
            initJob();
        }
        String UPDT_DT = SysUtils.getSysTime();

        for (Map.Entry<String, voIcmpCtlr> obj : this.icmpCtlrMap.entrySet()) {
            if (obj.getValue().getDEL_YN().equals("Y")) {
                log.info("WcamService.doJob: app WCAM: {}, {}, {}, DELETED.", obj.getValue().getCTLR_NMBR(), obj.getValue().getISTL_LCTN_NM(), obj.getValue().getCTLR_IP());
                continue;
            }
            voIcmpCtlrStts stts = new voIcmpCtlrStts();
            stts.setCTLR_NMBR(obj.getValue().getCTLR_NMBR());
            stts.setUPDT_DT(UPDT_DT);
            stts.setCMNC_STTS_CD("CMS1");   // 비정상으로 기본 설정
            this.ctlrMapper.updateStts(stts);
            if (insHs) {
                this.ctlrMapper.insertSttsHs(stts);
            }
        }
        MDC.remove(keyData);
        MDC.clear();
    }
}
