package kr.gmtc.gw.elastic.parser.logingIndex;

import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.LinkedBlockingQueue;

import kr.gmtc.gw.elastic.utils.LogMsgParserUtile;
import kr.gmtc.gw.elastic.vo.ElasticMsgHeader;
import kr.gmtc.gw.elastic.vo.ElasticResultMsgVO;
import kr.gmtc.gw.elastic.vo.LogMsgParserVO;
import kr.gmtc.gw.elastic.vo.ReplayLog.RadarVO;
import kr.gmtc.gw.elastic.vo.ReplayLog.StandardRouteVO;
import kr.gmtc.gw.elastic.vo.ReplayLog.StandardRouteVO.RoutePoints;

public class ParserRadar {
    
    private List<ElasticResultMsgVO> logMsgs;

    Queue<LogMsgParserVO> parserAiLocQ = new LinkedBlockingQueue<LogMsgParserVO>();

    LogMsgParserUtile pUtile;
    
    public ParserRadar(){

        pUtile = new LogMsgParserUtile();

    }

    public void logParser(List<ElasticResultMsgVO> recvMsgs){

        this.logMsgs = recvMsgs;
        
        List<RadarVO> parserList = new ArrayList<RadarVO>();
        RadarVO parserVO;

        LogMsgParserVO   logVO;
        ElasticMsgHeader logHeader;

        for( ElasticResultMsgVO mv : logMsgs){

            String sMsg = mv.getMessage();
            
            logVO = new LogMsgParserVO();
            
            /*** 메시지 Pipe(|) 구분 ***/
            String[] split_Div = pUtile.splitString(sMsg, pUtile.DIV_PIPE);

            /*** 메시지 Header 변환 START ***/
            String[] split_Header = pUtile.splitString(split_Div[0], pUtile.DIV_COMMA);

            logHeader = new ElasticMsgHeader();
            logHeader.setLog_dt(split_Header[0]);
            logHeader.setLog_se(split_Header[1]);
            logHeader.setSw_se(split_Header[2]);
            logHeader.setServer_se(split_Header[3]);

            logVO.setHeader(logHeader);
            /*** 메시지 Header 변환 END ***/

            String sDatas = null;

            try {
                /*** 메시지 data(body) 변환 START ***/
                sDatas = split_Div[1];
            } catch (Exception e) {
                // continue;
            }

            if( sDatas == null ) continue;

            String[] split_Data = null;

            //Data부 여러줄 인지 확인(^)
            int iDivCnt_Datas = pUtile.countDivInMsg(sDatas, pUtile.DIV_CARET);

            // Data부 여러줄인경우 분리(^)
            if(iDivCnt_Datas > 0){
                split_Data = pUtile.splitString(sDatas, pUtile.DIV_CARET);
            }else{
                split_Data = new String[1];
                split_Data[0] = sDatas;
            }

            /**** Data부 한줄씩 처리 START ****/
            for(String loop1Data : split_Data){

                parserVO = new RadarVO();

                // 일반 필드 분리(,)
                String[] split_Field = pUtile.splitString(loop1Data, pUtile.DIV_COMMA);
                
                parserVO.setTrgt_id(split_Field[0]);
                parserVO.setRecptn_dt(split_Field[1]);
                parserVO.setLat(split_Field[2]);
                parserVO.setLon(split_Field[3]);
                parserVO.setSpd(split_Field[4]);
                parserVO.setCos(split_Field[5]);
                parserVO.setDetct_cnt(split_Field[6]);
                parserVO.setUndetct_cnt(split_Field[7]); 

                parserList.add(parserVO);
            }
            /**** Data부 한줄씩 처리 END ****/

            logVO.setData(parserList);
            /*** 메시지 data(body) 변환 END ***/

            // Que에 파싱 데이터 담기
            parserAiLocQ.offer(logVO) ;

        } // for( ElasticResultMsgVO mv : logMsgs){... END

        

    }




}
