package gmt.protocol;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import gmt.common.util.GmtUtils;
import gmt.protocol.ais.*;
import gmt.protocol.ext.*;
import gmt.protocol.ext.VO.PResultVO;
import gmt.protocol.std.*;
import gmt.protocol.std.type.*;

public class AISEnDecoder {

	private AISMsgType msgType;
	private AISDecoder aisDecoder;
	private GmtExt 	   gmtExt;
	private GmtNewExt  gmtNewExt;
	private STDEncoder stdEncoder;
	
	private String parsedData;
	
	private List<String> msgList;
	private List<String> dataList;
			
	
	public AISEnDecoder() {
		
		msgType 	= AISMsgType.None;
		aisDecoder 	= new AISDecoder();
		gmtExt 		= new GmtExt();
		gmtNewExt 	= new GmtNewExt();
		stdEncoder 	= new STDEncoder();
		
		msgList 	= new ArrayList<String>();
		dataList 	= new ArrayList<String>();
		
		
	}
	
	public AISDecoder aisDecoder() { return this.aisDecoder; }
	public GmtExt     gmtExt() { return this.gmtExt; }
	public GmtNewExt  gmtNewExt() { return this.gmtNewExt; }	
	public STDEncoder stdEncoder() { return this.stdEncoder; }
	
	public AISMsgType getAISMsgType(String msg) {
		msgType = AISMsgType.None;
		
		try 
		{
			if (msg.contains("|") ) {				
				if ( GmtUtils.checkCRC(msg,"*") == false ) {
					return msgType;
				}
				msgType = AISMsgType.IEC;
				return msgType;				
			}
			
			String[] splitMsg = msg.split("|");
			if (splitMsg.length != 2) {
				return msgType;
			}
			
			String header = splitMsg[0];			
			splitMsg = header.split(",");			
			
			if (splitMsg.length == 6)
				this.msgType = AISMsgType.Extend;
			else
			if (splitMsg.length == 9)
				this.msgType = AISMsgType.NewExtend;
			
		} catch (Exception e) {
			return msgType;
		}
		
		return msgType; 
	}
	
	public PResultVO parseExtend(String msg) {
		PResultVO result = new PResultVO();
		try {
			
			if (gmtExt.parse(msg)) {
				result.setID(gmtExt.getpResult().getID());
				result.setMsgType(gmtExt.getpResult().getMsgType());
				result.setMsgTypeStr(gmtExt.getpResult().getMsgTypeStr());
				
				this.parsedData = gmtExt.getData();
			}
		} catch(Exception e) {
			this.parsedData = "";
		}
		
		return result;
	}
	
	public PResultVO parseNewExtend(String msg) {
		PResultVO result = new PResultVO();
		try {
			
			if (gmtNewExt.parse(msg)) {
				result.setID(gmtNewExt.getpResult().getID());
				result.setMsgType(gmtNewExt.getpResult().getMsgType());
				result.setMsgTypeStr(gmtNewExt.getpResult().getMsgTypeStr());
				
				this.parsedData = gmtNewExt.getData();
			}
		} catch(Exception e) {
			this.parsedData = "";
		}
		
		return result;
		
	}
	
	public int parseIEC(final String data)
	{
		int result = aisDecoder.parse(data);
		
		if (result != -1) {
			aisDecoder.getRcvMessageList(this.msgList);
			aisDecoder.getRawDataList(this.dataList);
		}
		return result;
	}
	
	public int parseIEC(final String rcvMsg, final String data)
	{
		int result = aisDecoder.parse(rcvMsg, data);
		
		if (result != -1) {
			aisDecoder.getRcvMessageList(this.msgList);
			aisDecoder.getRawDataList(this.dataList);
		}
		return result;
	}
	
	public String makeNewExtendMessage(String msgType, String iecMsg, String vtsID, String bsid, int timeStamp, 
								LocalDateTime sysTime, int timeSyncCode, int dualCode, int msgSeq1, int msgSeq2) {
		
		String result = "";
		
		
		return result;
	}
	
	public String updateNewExtendMessage(String newExtMsg, boolean isPrimary, int secondMsgSeq)
	{
		return "";
//		return gmtNewExt.
	}
	
	public String makeExtendMessage(String msgType, String iecMsg, String vtsID, String bsid, boolean isPrimary, LocalDateTime msgDT)
	{
		return gmtExt.makeExtMsg(msgType, iecMsg, vtsID, bsid, isPrimary, msgDT);
	}
	
	public GMTResult encoding(final AISDecoder aisDecoder, LocalDateTime msgDT, String bsid, LocalDateTime bsUTC, BSCType bscType, SensorType sensor)
	{	
		return stdEncoder.encoding(aisDecoder, msgDT, bsid, bsUTC, bscType, sensor);
	}
	
	public GMTResult encoding(LocalDateTime msgDT, String bsid, LocalDateTime bsUTC, BSCType bscType, SensorType sensor)
	{	
		return stdEncoder.encoding(this.aisDecoder, msgDT, bsid, bsUTC, bscType, sensor);
	}
	
	
	
}
