package com.safemobile.lib;

import java.util.ArrayList;

import com.safemobile.lib.radio.Channel;
import com.safemobile.lib.radio.Emerg;
import com.safemobile.lib.radio.IncCall;
import com.safemobile.lib.radio.RadioGW;
import com.safemobile.lib.radio.RadioStatus;
import com.safemobile.lib.radio.SUstatus;
import com.safemobile.lib.radio.Zone;
import com.safemobile.lib.radio.Zone_and_channel;

public class RadioMSG extends TCPmsg {

	public int rOpcode;
	public String payload="";
	public ArrayList<RadioGW> RadioGWList = null;
	
	//zone and channel;
	public Zone_and_channel zac= null;
	
	//radio status
	public RadioStatus rStatus = null;
	
	//SU stauts
	public SUstatus suStatus = null;
	
	//Incomming call
	public IncCall incCall = null;
	
	//Emerg
	public Emerg emerg = null;
	
	// Contacts list
	public ArrayList<Contact> contacts;
	



	public RadioMSG(TCPmsg tcp) {
		super(tcp);
		String date4parsing = super.data;
		
		String[] arr = date4parsing.split("#");
		
		rOpcode = Integer.parseInt(arr[0]);
		payload = arr[1];
		
		switch(rOpcode)
		{
			case 200 /* gw list */:
				RadioGWList = getRadioGWList();
				break;


			case OperationCodes.CHANNEL_BRDCST:

				zac = parse_CHANNEL_BRDCST(payload);
				break;

			
			case OperationCodes.RADIO_STATUS_REP:

				rStatus = parse_RADIO_STATUS_REP(payload);
				break;

			
			case OperationCodes.UNIT_STATUS_UPDATE :
				suStatus = parse_UNIT_STATUS_UPDATE(payload);
				break;

			
			case 125:
			case OperationCodes.CALL_STATUS_BRDCST:

				incCall = parse_CALL_STATUS_BRDCST(payload);
				break;

			
			case 121:
			case 122:
			case 123:
			{
				try {
					incCall = new IncCall();
					incCall.opCode = rOpcode;
					
					incCall.callStatus = Integer.parseInt(payload.substring(payload.length()-1, payload.length()));
					
				} catch (Exception e) {
					SM.Debug("Cmd 121, 122, 123 error:"+e.toString());						
				}
				/*
				try {
					System.out.println("############ tmpARR: " + payload );
					incCall = new IncCall();
					incCall.opCode = rOpcode;
					String[] tempArr = payload.split("&");
					
					String[] gwID_and_rgwID_imei = tempArr[0].split("/");
					incCall.gwID = Integer.parseInt(gwID_and_rgwID_imei[0]);
					incCall.rgwID = Integer.parseInt(gwID_and_rgwID_imei[1]);
					
					incCall.callStatus = Integer.parseInt(tempArr[1]);
					
					incCall.callType = Integer.parseInt(tempArr[2]);
					
					incCall.groupId = Integer.parseInt(tempArr[3]);
				} catch (Exception e) {
					SM.Debug("Cmd 121, 122, 123 error:"+e.toString());					
				}
				*/
				break;
			}
			
			case 115:
			case 116:
			case 117:
			{
				
				try {
					incCall = new IncCall();
					incCall.opCode = rOpcode;
					
					incCall.callStatus = Integer.parseInt(payload.substring(payload.length()-1, payload.length()));
					
				} catch (Exception e) {
					SM.Debug("Cmd 115,116,117 error:"+e.toString());					
				}
				break;
				
				/*
				try {
					incCall = new IncCall();
					String[] tempArr = payload.split("&");
					incCall.opCode = rOpcode;
					String[] gwID_and_rgwID_imei = tempArr[0].split("/");
					incCall.gwID = Integer.parseInt(gwID_and_rgwID_imei[0]);
					incCall.rgwID = Integer.parseInt(gwID_and_rgwID_imei[1]);
					
					incCall.callStatus = 3;
					
					incCall.callType = Integer.parseInt(tempArr[2]);
					
					incCall.groupId = Integer.parseInt(tempArr[3]);
				} catch (Exception e) {
					SM.Debug("Cmd 115, 116, 117 error:"+e.toString());					
				}
				
				break;
				*/
			}
			
			case OperationCodes.CALL_TYPE_REP:
				incCall = 	parse_CALL_TYPE_REP(payload);
				break;

			
			case 172:
			{
				
				try {
					incCall = new IncCall();
					incCall.opCode = rOpcode;
					String[] tempArr = payload.split("/");
					incCall.mic = new Mic();
					incCall.mic.mic_type = Integer.parseInt(tempArr[0]);
					incCall.mic.signal_type = Integer.parseInt(tempArr[1]);
					incCall.mic.mic_state = Integer.parseInt(tempArr[2]);
					incCall.mic.mic_gain = Integer.parseInt(tempArr[3]);
					
					SM.Debug("GOT Mic state changed", incCall.opCode + " # " + incCall.mic.mic_state);
				} catch (Exception e) {
					SM.Debug("Cmd 172 error:"+e.toString());					
				}
				break;
			}
			
			case OperationCodes.EMERGENCY_REP:

				emerg = parse_EMERGENCY_REP(payload);
				break;

			
			/*
			case OperationCodes.CONTACTS_REP:
			{
				try
				{
					SM.Debug("Parsing Contacts");
					contacts = Contact.parseTCPMsg(payload); 
				}
				catch(Exception ex)
				{
					SM.Exception("Exception parse Contacts", ex.toString());
				}
				break;
			}
			*/
			
			default:
				break;
		}
		
		//SM.Debug("RadioMSG", "Done parsing");
		
	}

	private RadioGW parseRadioGW(String buffer)
	{
		RadioGW rgw = new RadioGW();
		String[] radioFields = buffer.split("&");

		if( radioFields.length > 3 ) {

			rgw.ID 		= Integer.parseInt(radioFields[0].trim());
			rgw.GW_ID 	= Integer.parseInt(radioFields[1].trim());
			rgw.IMEI 	= radioFields[2];
			rgw.IP 		= radioFields[3];
		}

		return rgw;
	}

	private Zone parseZone(String buffer)
	{
		Zone zone = new Zone();
		String[] zoneFields = buffer.split(":");

		if( zoneFields.length > 2 ) {

			zone.dbID 		= Integer.parseInt(zoneFields[0].trim());
			zone.id 		= Integer.parseInt(zoneFields[1].trim());
			zone.ZoneName 	= zoneFields[2];
		}

		return zone;
	}

	private Channel parseChannel(String buffer)
	{
		Channel channel = new Channel();
		String[] channelFields = buffer.split("/");

		if( channelFields.length > 2 ) {

			channel.dbID 	= Integer.parseInt(channelFields[0].trim());
			channel.id 		= Integer.parseInt(channelFields[1].trim());
			channel.chName 	= channelFields[2].trim();
		}

		return channel;
	}

	private String[] getChannelList(String zone)
	{
		String[] zoneArr = zone.split(":");
		if ( zoneArr.length > 3)
			return zoneArr[3].split(",");

		return null;
	}


	private String[] getZoneList(String gw)
	{
		String[] zoneArr = gw.split("&");
		if ( zoneArr.length > 4)
			return zoneArr[4].split("@");

		return null;
	}

	private ArrayList<RadioGW> getRadioGWList()
	{
		ArrayList<RadioGW> radioGWList = new ArrayList<RadioGW>();
		String[] radioGWArr = payload.split(";");

		for(int i=0; i < radioGWArr.length; i++)
		{
			RadioGW rgw = parseRadioGW(radioGWArr[i]);
			String[] zoneArr  = getZoneList(radioGWArr[i]);
			if( zoneArr == null)
				continue;

			for(int j=0; j < zoneArr.length; j++)
			{

				Zone zone = parseZone(zoneArr[j]);
				String[] channelArr =  getChannelList(zoneArr[j]);
				if( channelArr == null)
					continue;

				for(int k =0; k < channelArr.length; k++)
				{
					Channel channel = parseChannel(channelArr[k]);

					//add channel to zone
					zone.channelList.add(channel);
				}

				//add zone to radio GW
				rgw.zoneList.add(zone);
			}

			radioGWList.add(rgw);

		}

		return radioGWList;
	}

	private Zone_and_channel parse_CHANNEL_BRDCST(String payload) {
		Zone_and_channel zacObj = new Zone_and_channel();

		try {

			String[] tempArr = payload.split("&");

			String[] gwID_and_rgwID = tempArr[0].split("/");
			zacObj.gwID = Integer.parseInt(gwID_and_rgwID[0].trim());
			zacObj.rgwID = Integer.parseInt(gwID_and_rgwID[1].trim());

			String[] zoneNr_and_channelNr = tempArr[1].split("/");
			zacObj.zoneNr = Integer.parseInt(zoneNr_and_channelNr[0].trim());
			zacObj.channelNr = Integer.parseInt(zoneNr_and_channelNr[1].trim());

		} catch (Exception e) {
			SM.Debug("Cmd 204 error:"+e.toString());

		}

		return zacObj;

	}

	private IncCall parse_CALL_STATUS_BRDCST(String payload)
	{
		IncCall incCallObj = new IncCall();

		try {

			incCallObj.opCode = rOpcode;
			String[] tempArr = payload.split("&");

			String[] gwID_and_rgwID_imei = tempArr[0].split("/");
			incCallObj.gwID = Integer.parseInt(gwID_and_rgwID_imei[0]);
			incCallObj.rgwID = Integer.parseInt(gwID_and_rgwID_imei[1]);
			incCallObj.Imei = Long.parseLong(gwID_and_rgwID_imei[2]);

			incCallObj.callStatus = Integer.parseInt(tempArr[1]);
			incCallObj.callType = Integer.parseInt(tempArr[2]);
			incCallObj.groupId = Integer.parseInt(tempArr[3]);
			if (tempArr.length > 4 )
				incCallObj.userID = Integer.parseInt(tempArr[4]);


		} catch (Exception e) {
			SM.Debug("Cmd 125, 126 error:"+e.toString());
		}

		return incCallObj;
	}

	private RadioStatus parse_RADIO_STATUS_REP(String payload)
	{
		RadioStatus radioStatus = new RadioStatus();

		try
		{
			String[] tempArr = payload.split("&");

			if(tempArr.length == 4 || tempArr.length == 5) {

				radioStatus.status = 1;
				radioStatus.incCall.callStatus = Integer.parseInt(tempArr[0]);
				radioStatus.incCall.callType = Integer.parseInt(tempArr[1]);
				radioStatus.incCall.Imei = Integer.parseInt(tempArr[2]);
				radioStatus.incCall.callerID = Integer.parseInt(tempArr[2]);
				radioStatus.incCall.groupId = Integer.parseInt(tempArr[3]);
				radioStatus.incCall.callDestID = Integer.parseInt(tempArr[3]);

				if(tempArr.length == 5)
					radioStatus.incCall.userID = Integer.parseInt(tempArr[4]);
			}
			else {
				String[] gwID_and_rgwID = tempArr[0].split("/");
				radioStatus.gwID = Integer.parseInt(gwID_and_rgwID[0]);
				radioStatus.rgwID = Integer.parseInt(gwID_and_rgwID[1]);
				radioStatus.status = Integer.parseInt(tempArr[1]);
			}
		}
		catch (Exception e) {
			SM.Debug("Cmd 199 error:"+e.toString());
		}

		return radioStatus;

	}

	private Emerg parse_EMERGENCY_REP(String payload)
	{
		Emerg emergObj = new Emerg();

		try {

			String[] tempArr = payload.split("/");

			emergObj.function = Integer.parseInt(tempArr[0]);
			emergObj.status = Integer.parseInt(tempArr[1]);

			// emerg.userID = Integer.parseInt(tempArr[2]);

		} catch (Exception e) {
			SM.Debug("Cmd 230 error:"+e.toString());
		}

		return emergObj;
	}

	private SUstatus parse_UNIT_STATUS_UPDATE(String payload)
	{
		SUstatus suStatusObj = new SUstatus();

		try {

			String[] tempArr = payload.split("&");

			suStatusObj.imei = Integer.parseInt(tempArr[0]);
			suStatusObj.status = Integer.parseInt(tempArr[1]);

		} catch (Exception e) {
			SM.Debug("Cmd 250 error:"+e.toString());
		}

		return suStatusObj;

	}

	private IncCall parse_CALL_TYPE_REP(String payload)
	{
		IncCall incCallObj = new IncCall();

		try {

			incCallObj.opCode = rOpcode;
			String[] tempArr = payload.split("/");
			incCallObj.callType = Integer.parseInt(tempArr[0]);
			incCallObj.callStatus = Integer.parseInt(tempArr[1]);

			SM.Debug("GOT CHANGE CALL TYPE MSG", incCallObj.opCode + " # " + incCallObj.callType + " # " + incCallObj.callStatus);
		} catch (Exception e) {
			SM.Debug("Cmd 115,116,117 error:"+e.toString());
		}

		return incCallObj;
	}

}