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

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
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.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import kr.gmt.so.state.StateManager;
import kr.gmtc.gw.asderecv.rest.vo.SacpServiceHeader;
import kr.gmtc.gw.asderecv.rest.vo.SacpServiceVO;
import kr.gmtc.gw.asderecv.rest.vo.ServiceAsdeData;

// import gmt.common.type.LogLevelType;
// import gmt.logger.GmtLogManager;
// import kr.gmtc.eyegw.rest.vo.ServiceAsdeData;

@Component("kafkaProducer")
public class KafkaProducer {

	private Logger logger = LoggerFactory.getLogger(this.getClass());;
	
	@Value("${kafka.message.topic.recv.asde}")
	private String topicName;
	
	@Resource(name = "serviceQueue")
	HashMap<Integer, Queue<ServiceAsdeData>> sendQueue;
	
	
	@Autowired
	private KafkaTemplate<String, String> kafkaTemplateString;

	ObjectMapper mapper = new ObjectMapper();
	
	String sSendMsg = "";
	
	boolean loopYn = true;

	@Autowired
    private StateManager stateMgr;
	
	//@Scheduled(fixedDelay = 1000, initialDelay = 1000)
	public void sendASDEMessage(List<ServiceAsdeData> srcList) {
		
		String sendMsg = "";

		int qidx = 2;

		stateMgr.updateState();

		List<ServiceAsdeData> _srcList = new ArrayList<ServiceAsdeData>();
		_srcList.addAll(srcList);

		if(stateMgr.isActive()){
			//logger.info("************************** serviceList Count : " + sendQueue.get(qidx).size());
			
			long qSize = _srcList.size();
			String logMsg = qSize + "건";
			String sFirstDate = null, sEndDate = null;

			if(qSize > 0){
				loopYn = false;
				//sendMsg = makeServiceData(sendQueue.get(qidx));
				sendMsg = makeServiceData(_srcList);
				loopYn = true;

				sFirstDate = _srcList.get(0).getRecptn_dt();
				sEndDate = _srcList.get((int) (qSize -1)).getRecptn_dt();
			}

			
			if(sendMsg != null && !sendMsg.equals("")){
				kafkaTemplateString.send(topicName, sendMsg);
				logger.info("kafka Produce complate(" + logMsg + " | " + sFirstDate + " | " + sEndDate + ")" );
			}
		}
		
	}

	//private String makeServiceData(Queue<ServiceAsdeData> procQue){
	private String makeServiceData(List<ServiceAsdeData> srcList){

		String sendCode, sendMsg ;
		SacpServiceHeader jsonHeader = new SacpServiceHeader();
		List<ServiceAsdeData> jsonData = new ArrayList<ServiceAsdeData>();
		
		sendCode = "200";
		sendMsg = "";
		
		SacpServiceVO sacpServiceVO = new SacpServiceVO();

		for(int idx=0; idx<srcList.size(); idx++) {
			jsonData.add(srcList.get(idx));
		}

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

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

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

		return sRetJsonData;

	}

	public String getTime() {
		return LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"));
	}





}
