package kr.gmtc.gw.standstatus.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import kr.gmt.so.state.StateManager;
import kr.gmt.so.state.callback.IState;
import kr.gmt.so.state.model.SystemType;

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

    @Autowired
    private StateManager sm;

    public Logger logger;

    public MainController(StateManager sm){

        this.logger = LoggerFactory.getLogger(MainController.class);

        this.sm = sm;

        sm.updateState();

        // sm.isActive();

        // sm.isActive("DT0201", SystemType.Primary);

        // sm.setCallback(new IState() {   
        //     @Override
        //     public void onState(String ID, String Type, String State, boolean isActive) {
        //             logger.info("[STATE] {},{},{},{}", ID, Type, State, isActive);
        //         }
        // });          
    }

    @Scheduled(fixedRate = 1000 * 10) // 10초에 1번
    public void testSchduler() {

        if(sm.isActive()){
            logger.info("************************** Active **************************");
        }

        sm.updateState();
    }

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

    

}
