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

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.concurrent.LinkedBlockingQueue;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.databind.ObjectMapper;

import kr.gmt.so.state.StateManager;
import kr.gmtc.gw.standstatus.config.QueueManageBean;
import kr.gmtc.gw.standstatus.kafka.vo.AiVideoMetaData;
import kr.gmtc.gw.standstatus.kafka.vo.KafkaRequestVo;
import kr.gmtc.gw.standstatus.kafka.vo.KafkaRequestVoiceVo;
import kr.gmtc.gw.standstatus.vo.VoiceVO;

@Service
public class KafkaMsgVccsListener {
    
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
	private final ObjectMapper objMapper = new ObjectMapper();

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

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


	@Autowired
    private StateManager stateMgr;
	
    @KafkaListener(topics = "${kafka.message.topic.recv.vccs}", groupId = "${kafka.consumer.group-id}")
     public void listenAiAnalyzeVideo(@Payload  String message) throws Exception{

		if(stateMgr.isActive()){
			
			KafkaRequestVoiceVo vo = null;

			try {
				if (message != null) {
	
					vo = objMapper.readValue(message, KafkaRequestVoiceVo.class);
					//recvVideoQ.add(vo.getData());
					queMB.getQRecvVccs().offer((ArrayList<VoiceVO>) vo.getData());
	
				}
			} catch(Exception e) {
				logger.error(e.getMessage());
			}

			logger.info("AI음성메타정보(VCCS) 수신 1건" );
		}


     }

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