package kr.gmtc.gw.standstatus.component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.LinkedBlockingQueue;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import kr.gmtc.gw.comp.rest.vo.RestServiceData;
import kr.gmtc.gw.comp.thread.CustomThread;
import kr.gmtc.gw.standstatus.config.QueueManageBean;
import kr.gmtc.gw.standstatus.database.dto.DataLoadDTO;
import kr.gmtc.gw.standstatus.database.dto.DataLoadHashMap;
import kr.gmtc.gw.standstatus.kafka.vo.AiVideoMetaData;
import kr.gmtc.gw.standstatus.kafka.vo.KafkaRequestVoiceVo;
import kr.gmtc.gw.standstatus.vo.AiAnalyzeStandSttus;
import kr.gmtc.gw.standstatus.vo.VoiceVO;
import kr.gmtc.gw.standstatus.vo.VoiceVO.VoiceKeyword;

public class AnalysisVccsAiMetaStatus{
    
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private List<CustomThread> customThreads;
    private CustomThread chkThreadStatus;

    DateTimeFormatter df_Pattern = DateTimeFormatter.ofPattern("yyyyMMddHHmmss.SSS");

    LocalDateTime dt;

    private DataLoadDTO dto;
	private QueueManageBean queMB;
    
    private LinkedBlockingQueue<ArrayList<VoiceVO>> qRecvVccs;
    // private LinkedList<RestServiceData> serviceList;

    private List<HashMap<String, String>> filterTaxi;

    public AnalysisVccsAiMetaStatus(DataLoadDTO dataLoadDTO, QueueManageBean queMB){
        this.dto = dataLoadDTO;
        this.queMB = queMB;
        initialize();
    }

    public void initialize() {


        qRecvVccs = queMB.getQRecvVccs();

        try {
            customThreads = new ArrayList<CustomThread>();
            customThreads.add(new CustomThread("storeRequestTaxi", this, CustomThread.NO_SLEEP, this::storeRequestTaxi, null, false));
            // customThreads.add(new CustomThread("makeMapStandSttus", this, CustomThread.NO_SLEEP, this::analysis_Ai_Video_Meta, null, false));
            // customThreads.add(new CustomThread("analysisStandSttus", this, CustomThread.NO_SLEEP, this::analysis_Stand_Sttus, null, false));

            // 이중화 체크 및 업무스래드 체크
			chkThreadStatus = new CustomThread("chkThreadStatus", this, 3000, this::chkThreadStatus, null, false);

        } catch (Exception e) {
            logger.error("Start Fail: "+ e.toString());
        }
    }

    public void start() {
		try {
		
            for(CustomThread ct : customThreads ){
				ct.start();
			}

            chkThreadStatus.start();

		} catch (Exception e) {
            logger.error("Start Fail: "+ e);
		}
    }

    private void chkThreadStatus() throws InterruptedException{

    }
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

    private void storeRequestTaxi() throws InterruptedException{

        int queSize = qRecvVccs.size();

        filterTaxi = new ArrayList<HashMap<String, String>>();

        for(int idx = 0; idx<queSize; idx++){

            List<VoiceVO> lcDataList = qRecvVccs.take();

            for( VoiceVO voice : lcDataList  ){

                // List<VoiceVO> voiceList = lcData.getData();

                String[] clsgnToken = ((String) voice.getCallsign()).split("\\ ");
                
                String clsgn_num = clsgnToken[1];

                

                // 기장(p)이 요청(request)한 내용확인
                if(voice.getSpeaker().equals("p") && voice.getDoc_type().equals("request")){

                    List<VoiceKeyword> vKeywords = voice.getKeywords();

                    boolean bKeywordYn = false;
                    for(VoiceKeyword vk : vKeywords){
                        if(vk.getKeyword().equals("request taxi")){
                            bKeywordYn = true;
                            break;
                        }
                    }

                    if(!bKeywordYn){
                        for(VoiceKeyword vk : vKeywords){
                            if(vk.getKeyword().equals("request")){
                                bKeywordYn = true;
                                break;
                            }
                        }

                        if(bKeywordYn){
                            for(VoiceKeyword vk : vKeywords){
                                if(vk.getKeyword().equals("taxi")){
                                    bKeywordYn = true;
                                    break;
                                }
                            }
                        }
                    }

                    if(bKeywordYn){

                        HashMap<String, String> mapTaxi = new HashMap<>();

                        String sComDt = (String) voice.getEndtime();

                        sComDt = sComDt.replace("-", "");
                        sComDt = sComDt.replace(" ", "");
                        sComDt = sComDt.replace(":", "");
                        sComDt = sComDt.substring(0, 12);

                        mapTaxi.put("clsgn_num", clsgn_num);
                        mapTaxi.put("clsgn", (String) voice.getCallsign());
                        mapTaxi.put("com_dt", sComDt);
                        
                        
                        filterTaxi.add(mapTaxi);

                    }
                    
                }

            }
            
            logger.info("AI음성메타(VCCS)정보 분석 :" + lcDataList.size() + " 건");

            
        }

    }

    public List<HashMap<String, String>> getFilterTaxi(){
        return this.filterTaxi;
    }
    

}