package kr.gmtc.gw.standstatus.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import kr.gmtc.gw.comp.thread.CustomThread;
import kr.gmtc.gw.standstatus.database.datasource1.CmSelectDao;
import kr.gmtc.gw.standstatus.database.datasource2.IcSelectDao;
import kr.gmtc.gw.standstatus.database.dto.DataLoadDTO;
import kr.gmtc.gw.standstatus.database.dto.DataLoadHashMap;


// @Service
public class DataLoadService {
    
    // @Resource(name = "dataLoadDTO")
    private DataLoadDTO dataLoadDTO;

    // @Resource(name = "cmSelectDao")
    private CmSelectDao cmSelectDao;

    // @Resource(name = "icSelectDao")
    private IcSelectDao icSelectDao;

    


    private CustomThread threadLoadCctvList;
    private CustomThread threadLoadFlt;

    private final Logger logger;


    public DataLoadService(DataLoadDTO dataLoadDTO, CmSelectDao cmSelectDao, IcSelectDao icSelectDao){
        logger = LoggerFactory.getLogger(this.getClass());
        this.dataLoadDTO = dataLoadDTO;
        this.cmSelectDao = cmSelectDao;
        this.icSelectDao = icSelectDao;
        
        initialize();
    }

    public void initialize() {


		try {
            
            threadLoadCctvList = new CustomThread("loadCctvList", this, 600000, this::loadCctvList, null, false);

            threadLoadFlt = new CustomThread("loadArcftFlt", this, 30000, this::loadArcftFltSchedule, null, false);

        } catch (Exception e) {
            //			logger.writeLevelLog("[MainController] Prepare Fail " + e.getMessage(), LogLevelType.LOG_ERROR, "AllLog");
                        // System.exit(1);
        }
    }

    public void start() {
        try {
			
            threadLoadCctvList.start();
            threadLoadFlt.start();
            
		} catch (Exception e) {
//			logger.writeLevelLog("[MainController] Start Fail " + e.getMessage(), LogLevelType.LOG_ERROR, "AllLog");
			// System.exit(1);
		}
    }


    //////////////////////////////////////////////////////////////////////////

     private void loadCctvList(){

        List<DataLoadHashMap> db_CctvList = cmSelectDao.selectCctv_List();
        
        if(db_CctvList == null){
            logger.error("cctv load fail");
        }else{
            dataLoadDTO.setDb_CctvList(db_CctvList);
            logger.info("cctv load Count:" + db_CctvList.size());
        }

    //    Thread.sleep(600000);
    }


    private void loadArcftFltSchedule() throws InterruptedException{

        dataLoadDTO.setDb_FltDep(icSelectDao.select_flt_dep());
        dataLoadDTO.setDb_FltArr(icSelectDao.select_flt_arr());

        //Thread.sleep(30000);
    }

    public IcSelectDao getIcSelectDao() {
        return icSelectDao;
    }

    /*
        년(yyyy)+월(mm)+일(dd)+시(hh24)+분(mi)+초(ss)+밀리초(잘자)+SEQ(000~999)
        예시)20230607153520123001 
    */
    public String MakeAnalsId() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		Date now = new Date();
		int seq = icSelectDao.getAnalsIdSeq();
		String nSeq = String.format("%03d", seq);
		return sdf.format(now) + nSeq;
	}

}
