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

public class ParserRoute {
    
    private List<ElasticResultMsgVO> logMsgs;

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

    LogMsgParserUtile pUtile;
    
    public ParserRoute(){

        pUtile = new LogMsgParserUtile();

    }

    public void logParser(List<ElasticResultMsgVO> recvMsgs){

        this.logMsgs = recvMsgs;
        
        StandardRouteVO routeVO;
        RoutePoints      routePoints;

        List<StandardRouteVO> routeList = new ArrayList<StandardRouteVO>();

        LogMsgParserVO   logVO;
        ElasticMsgHeader logHeader;

        for( ElasticResultMsgVO mv : logMsgs){

            String sMsg = mv.getMessage();
            
            logVO = new LogMsgParserVO();
            
            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){

                routeVO = new StandardRouteVO();

                // 일반 필드 분리(,)
                String[] split_Field = pUtile.splitString(loop1Data, pUtile.DIV_COMMA);

                routeVO.setMv_id(split_Field[0]);
                routeVO.setTrgt_id(split_Field[1]);
                routeVO.setMv_type(split_Field[2]);
                routeVO.setDep_arr_ty(split_Field[3]);

                routeVO.setMv_point(new ArrayList<RoutePoints>());

                String[] split_List = pUtile.splitString(split_Field[4], pUtile.DIV_DOLLAR);

                for(String loopList : split_List){

                    routePoints = new RoutePoints();

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

                    routePoints.setSpot_sn(split_Array[0]);
                    routePoints.setLat(split_Array[1]);
                    routePoints.setLon(split_Array[2]);

                    routeVO.getMv_point().add(routePoints);

                }

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

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

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

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

        

    }




}
