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;

@Service
public class KafkaMessageListener {
    
    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.video}", groupId = "${kafka.consumer.group-id}")
     public void listenAiAnalyzeVideo(@Payload  String message) throws Exception{

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


     }

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