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

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.FusionVO;
import kr.gmtc.gw.elastic.vo.ReplayLog.FusionVO.BoundingBox;


public class ParserFusion {
    
    private List<ElasticResultMsgVO> logMsgs;

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

    LogMsgParserUtile pUtile;
    
    public ParserFusion(){

        pUtile = new LogMsgParserUtile();

    }

    public void logParser(List<ElasticResultMsgVO> recvMsgs){

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

        FusionVO parserVO;
        BoundingBox bBox;

        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 ***/

            /*** 메시지 data(body) 변환 START ***/
            String sDatas = split_Div[1];

            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 FusionVO();

                // 일반 필드 분리(,)
                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.setAlt(split_Field[6]);
                parserVO.setTrgt_ty(split_Field[7]);
                parserVO.setSchdul_id(split_Field[8]);
                parserVO.setMode_s_cd(split_Field[9]);
                parserVO.setSsr_cd(split_Field[10]);
                parserVO.setClsgn(split_Field[11]);
                parserVO.setTail_no(split_Field[12]);
                parserVO.setCrrct_lat(split_Field[13]);
                parserVO.setCrrct_lon(split_Field[14]);
                parserVO.setIs_cntrlzone(split_Field[15]);

                parserVO.setBbox(new ArrayList<BoundingBox>());

                // bbox 담기
                String[] split_List = pUtile.splitString(split_Field[17], pUtile.DIV_DOLLAR);

                for(String loopList : split_List){

                    bBox = new BoundingBox();

                    String[] split_Array = pUtile.splitString(loopList, pUtile.DIV_AT);

                    bBox.setLt_x(split_Array[0]);
                    bBox.setLt_y(split_Array[1]);
                    bBox.setRb_x(split_Array[2]);
                    bBox.setRb_y(split_Array[3]);
                    bBox.setPx(split_Array[4]);
                    bBox.setPy(split_Array[5]);

                    parserVO.getBbox().add(bBox);

                }

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

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

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

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

        

    }




}
