package gps;

import java.util.ArrayList;
import java.util.List;

import anywheresoftware.b4a.BA;
import jGPIO.mycodes;

@BA.ShortName("GpsGPGSA")
public class GpsGPGSA {
	public String invalidmsg = "";
	public String Mode = "";
	public String LockMode = "";
	public int[] SatelitteID = new int[] {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
	public double PDOP = -1;
	public double HDOP = -1;
	public double VDOP = -1;
	
	public GpsGPGSA(String msg) {
		String[] partB = msg.split(",");
		if (partB.length<18) {
			invalidmsg = "GPGSA Split less than 18";
			return;
		}
		
		List<String> err = new ArrayList<String>();
		switch(mycodes.Gps_Char(partB[1])) {
		case 'M' :
			Mode = "Manual";
			break;
		case 'A' :
			Mode = "Automatic";
			break;
		default :
			Mode = "";
			err.add("GPGSA Mode is invalid : "+partB[1]);
			break;
		}
		
		int[] intresult = new int[] {0};
		if (mycodes.Gps_Int(partB[2], intresult)) {
			switch(intresult[0]) {
			case 1 :
				LockMode = "No Fix";
				break;
			case 2 :
				LockMode = "2D";
				break;
			case 3 :
				LockMode = "3D";
				break;
			default :
				LockMode = "";
				err.add("GPGSA LockMode is invalid : "+partB[2]);
				break;
			}
		} else {
			err.add("GPGSA Lockmode is invalid : "+partB[2]);
		}
		
		// satelite , 12 unit
		for (int jj=0;jj<12;jj++) {
			if (mycodes.Gps_Int(partB[3+jj], intresult)) {
				SatelitteID[jj] = intresult[0];
			} else SatelitteID[jj] = -1;					
		}
		
		double[] doubleresult = new double[] {0};
		
		// PDOP = 15
		if (mycodes.Gps_Double(partB[15], doubleresult)) {
			PDOP = doubleresult[0];
		} else {
			err.add("GPGSA Invalid PDOP : "+partB[15]);
		}
		
		// HDOP = 16
		if (mycodes.Gps_Double(partB[16], doubleresult)) {
			HDOP = doubleresult[0];
		} else {
			err.add("GPGSA Invalid HDOP : "+partB[16]);
		}
		
		// VDOP = 17
		if (mycodes.Gps_Double(partB[17], doubleresult)) {
			VDOP = doubleresult[0];
		} else {
			err.add("GPGSA Invalid VDOP : "+partB[17]);
			
		}
		
		invalidmsg = mycodes.Gps_ErrList_to_String(err);
	}
	
	public boolean isValid() {
		if (invalidmsg=="") return true; else return false;
	}
}
