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

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

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.core.JsonProcessingException;
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.config.QueueManageBean;
import kr.gmtc.gw.standstatus.kafka.vo.KafkaServiceData;
import kr.gmtc.gw.standstatus.kafka.vo.KafkaServiceVO;
import kr.gmtc.gw.standstatus.kafka.vo.ResponsHeader;

@Component
public class KafkaProduceStandSttus {
    
    protected Logger logger;

    @Autowired                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
	private KafkaTemplate<String, String> kafkaTemplateString;

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

	@Resource(name = "queueManageBean")
    private QueueManageBean queMB;


	private boolean sendRunnig = false;

	public boolean isSendRunnig() {
		return sendRunnig;
	}

	public void setSendRunnig(boolean sendRunnig) {
		this.sendRunnig = sendRunnig;
	}

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

    ObjectMapper mapper = new ObjectMapper();

	@Autowired
    private StateManager stateMgr;
    
    public KafkaProduceStandSttus(){
        logger = LoggerFactory.getLogger(this.getClass());
    }
 
    private KafkaServiceVO kafkaServiceVO;

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

		String sRetJsonData = "";

		stateMgr.updateState();

		if(stateMgr.isActive()){

			logger.info("************************** serviceList Count : " + queMB.getServiceList().size());

			if(queMB.getServiceList().size() > 0){
				sRetJsonData = makeServiceData(queMB.getServiceList());
			}

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

		}
		
	}
	
	private String makeServiceData(LinkedList<RestServiceData> procQue){

		String sendCode, sendMsg ;
		ResponsHeader jsonHeader = new ResponsHeader();
		List<KafkaServiceData> jsonData = new ArrayList<KafkaServiceData>();
		
		sendCode = "200";
		sendMsg = "";
		
		kafkaServiceVO = new KafkaServiceVO();
		 
		for( RestServiceData data : procQue ){

            KafkaServiceData cnvData = new KafkaServiceData();
            cnvData.setStand_no(data.getStand_no());
            cnvData.setClsgn(data.getClsgn());
			cnvData.setSchdul_id(data.getSchdul_id());
			cnvData.setAc_reg_no(data.getAc_reg_no());
            cnvData.setStand_sttus_ty(data.getAiStandSttus());
            cnvData.setAircraft_sttus_ty(data.getAircraft_sttus_ty());

            jsonData.add(cnvData);
        }

		jsonHeader.setResult_code(sendCode);
		jsonHeader.setResult_msg(sendMsg);
		
		kafkaServiceVO.setHeader(jsonHeader);
		
		if (sendCode.equals("200")) {
			
			kafkaServiceVO.setData(jsonData);

			//logger.info("Que["+(queIdx + 1)+"] service count :" + jsonData.size() + "/" + qTotalSize);
		}
		
		String sRetJsonData = "";

		try {
			sRetJsonData = mapper.writeValueAsString(kafkaServiceVO);
		} catch (JsonProcessingException e) {
			logger.error("makeServiceData-JsonProcessingException : " + e);
		}

		return sRetJsonData;

	}

}
