package kr.gmtc.gw.standstatus.controller;

import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import kr.gmtc.gw.comp.rest.ServiceQueManager;
import kr.gmtc.gw.comp.thread.CustomThread;
import kr.gmtc.gw.standstatus.component.AnalysisAlarmStatus;
import kr.gmtc.gw.standstatus.component.AnalysisStandStatus;
import kr.gmtc.gw.standstatus.config.QueueManageBean;
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.service.DataLoadService;

@Component("controller")
public class MainController implements ApplicationListener<ContextClosedEvent> {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private ServiceQueManager serviceQueManager;

    private List<CustomThread> listCustomThreads; 

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

    

    @Autowired
    private IcSelectDao icSelectDao;

    @Autowired
    private DataLoadDTO dataLoadDTO;


    @Autowired
    private QueueManageBean queMB;


    // @Resource(name = "recvVideoQ")
	// LinkedBlockingQueue<AiVideoMetaData> recvVideoQ;

    // @Resource(name = "serviceList")
    // Map<Integer, LinkedList<RestServiceData>> serviceList;

    // @Resource(name = "alamMsgQ")
    // Queue<AlarmMsgVO> alamMsgQ;

    // @Resource(name = "serviceRunnig")
    // private boolean serviceRunnig = false;

    // @Resource(name = "alamEndChkList")
    // LinkedList<AlarmMsgVO> alamEndChkList;

    // @Value("${rest.service.queueCount}")
    // private Integer scvQcount;

    // @Value("${rest.service.clearQ.maxCount}")
    // private Integer scvQmaxCount;

    // @Value("${rest.service.clearQ.diffTime}")
    // private Integer scvQdiffTime;

    // @Value("${rest.service.clearQ.clearTime}")
    // private Integer scvQclearTime;

    // @Resource(name = "testStandStsMap")
	// HashMap<String, String> testStandStsMap;

    // @Resource(name = "testStandStsMapAI")
	// HashMap<String, String[]> testStandStsMapAI;
    

    private DataLoadService dataLoadService;
    private AnalysisStandStatus analysisStatusService;
    private AnalysisAlarmStatus analysisAlarmStatus;

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

    
    @Order(3)
	@EventListener(ApplicationReadyEvent.class)
	public void initialize() {
		try {

            Marker interesting = MarkerFactory.getMarker("INTERESTING");
            
            logger.info(interesting, "MarkerTest");
            
            // serviceQueManager = new ServiceQueManager(serviceList, serviceRunnig);
            // serviceQueManager.setScvQclearTime(scvQclearTime);
            // serviceQueManager.setScvQcount(scvQcount);
            // serviceQueManager.setScvQdiffTime(scvQdiffTime);
            // serviceQueManager.setScvQmaxCount(scvQmaxCount);

            dataLoadService = new DataLoadService(dataLoadDTO, cmSelectDao, icSelectDao);
            dataLoadService.start();

            analysisStatusService = new AnalysisStandStatus(dataLoadDTO, queMB);

            analysisAlarmStatus = new AnalysisAlarmStatus(dataLoadDTO, queMB, dataLoadService);    


        } catch (Exception e) {
            logger.error("Start Fail: "+ e.toString());
        }
    }

    

    @Order(4)
	@EventListener(ApplicationReadyEvent.class)
	public void start() {
		try {
		
            // serviceQueManager.start();
            // serviceQueManager.join();
            
//            Thread.sleep(2000);
            analysisStatusService.start();

//            Thread.sleep(2000);
            analysisAlarmStatus.start();

            
            
		} catch (Exception e) {
            logger.error("Start Fail: "+ e);
		}

	}

    @Override
    public void onApplicationEvent(ContextClosedEvent event) {
        // TODO Auto-generated method stub
        throw new UnsupportedOperationException("Unimplemented method 'onApplicationEvent'");
    }
    

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

    public void setDataLoadDTO(DataLoadDTO dataLoadDTO) {
        this.dataLoadDTO = dataLoadDTO;
    }

    public void setCmSelectDao(CmSelectDao cmSelectDao) {
        this.cmSelectDao = cmSelectDao;
    }

    public void setIcSelectDao(IcSelectDao icSelectDao) {
        this.icSelectDao = icSelectDao;
    }

    public void setQueMB(QueueManageBean queMB) {
        this.queMB = queMB;
    }
}
