package kr.gmtc.gw.standstatus.kafka.producer;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.databind.ObjectMapper;

import kr.gmt.so.state.StateManager;
import kr.gmtc.gw.comp.rest.vo.RestServiceData;
import kr.gmtc.gw.standstatus.component.AnalysisAlarmStatus;
import kr.gmtc.gw.standstatus.vo.AlarmMsgVO;


//@Component
public class KafkaProduceSttusAlaram {
    protected Logger logger;

    @Autowired
	private KafkaTemplate<String, String> kafkaTemplateString;

    @Autowired
    private AnalysisAlarmStatus makeAlamMsgService;

	@Value("${kafka.message.topic.send.alarm}")
    private String topicStandSttus;

    ObjectMapper mapper = new ObjectMapper();

    @Autowired
    private StateManager stateMgr;
    
    public KafkaProduceSttusAlaram(){
        logger = LoggerFactory.getLogger(this.getClass());
    }
 
    //private KafkaServiceVO kafkaServiceVO;
    @Resource(name = "alamMsgQ")
    Queue<AlarmMsgVO> alamMsgQ;
    
    @Resource(name = "serviceRunnig")
	private boolean serviceRunnig = false;

    @Scheduled(fixedDelay = 1000, initialDelay = 1000)
	public void sendMessage() {

        if(stateMgr.isActive() && serviceRunnig){

            logger.info("************************** alamMsgQ Count : " + alamMsgQ.size());

            String sRetJsonData = "";

            if(alamMsgQ.size() > 0)
                sRetJsonData = makeAlamMsgService.makeAlamMsg();

            if(sRetJsonData != null && !sRetJsonData.equals("")){
                kafkaTemplateString.send(topicStandSttus, sRetJsonData);
            }
        }
		
	}

    
    public Queue<AlarmMsgVO> getAlamMsgQ() {
        return alamMsgQ;
    }

    public void setAlamMsgQ(Queue<AlarmMsgVO> alamMsgQ) {
        this.alamMsgQ = alamMsgQ;
    }

    public boolean isServiceRunnig() {
        return serviceRunnig;
    }

    public void setServiceRunnig(boolean serviceRunnig) {
        this.serviceRunnig = serviceRunnig;
    }
	
}
