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.AnaVO;
import kr.gmtc.gw.elastic.vo.ReplayLog.AnaVO.Area;
import kr.gmtc.gw.elastic.vo.ReplayLog.AnaVO.Target;

public class ParserAna {
    
    private List<ElasticResultMsgVO> logMsgs;

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

    LogMsgParserUtile pUtile;
    
    public ParserAna(){

        pUtile = new LogMsgParserUtile();

    }

    public void logParser(List<ElasticResultMsgVO> recvMsgs){

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

        AnaVO parserVO;
        Area  area;
        Target trgt;

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

                area = new Area();
                

                // 일반 필드 분리(,)
                String[] split_Field = pUtile.splitString(loop1Data, pUtile.DIV_COMMA);
                
                parserVO.setAnals_id(split_Field[0]);
                parserVO.setAnals_dt(split_Field[1]);
                parserVO.setAnals_ty(split_Field[2]);
                // parserVO.setArea(pUtile.splitString(split_Field[3], pUtile.DIV_AT));
                // parserVO.setTarget1(pUtile.splitString(split_Field[4], pUtile.DIV_AT));
                // parserVO.setTarget2(pUtile.splitString(split_Field[5], pUtile.DIV_AT));

                String[] split_Array = null;

                    split_Array = pUtile.splitString(split_Field[3], pUtile.DIV_AT);

                    area.setArea_id(split_Array[0]);
                    area.setArea_type(split_Array[1]);

                parserVO.setArea(area);

                    trgt = new Target();
                    split_Array = pUtile.splitString(split_Field[3], pUtile.DIV_AT);

                    trgt.setTrgt_id(split_Array[0]);
                    trgt.setTrgt_ty(split_Array[1]);

                parserVO.setTarget1(trgt);

                    trgt = new Target();
                    split_Array = pUtile.splitString(split_Field[3], pUtile.DIV_AT);

                    area.setArea_id(split_Array[0]);
                    area.setArea_type(split_Array[1]);
                
                parserVO.setTarget2(trgt);    

                parserVO.setAnals_msg(split_Field[6]);
                parserVO.setAnalsMsg(split_Field[7]);
                parserVO.setRls_ty(split_Field[8]);

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

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

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

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

        

    }




}
