package gps;

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

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

@BA.ShortName("GpsGPVTG")
public class GpsGPVTG {
	public String invalidmsg = "";
	public double Course_Heading = 0;
	public double Magnetic_Heading = 0;
	public double Horizontal_Knot_Speed = 0;
	public double Horizontal_KM_Speed = 0;
	
	public GpsGPVTG(String msg) {
		String partB[] = msg.split(",");
		if (partB.length<8) {
			invalidmsg = "GPVTG Split less than 8";
			return;
		}
		
		List<String> err = new ArrayList<String>();
		
		double[] result = new double[] {0};
		if (mycodes.Gps_Double(partB[1], result)) {
			Course_Heading = result[0];
		} else {
			Course_Heading = 0;
			err.add("GPVTG invalid Course Heading : "+partB[1]);
		}
		
		if (mycodes.Gps_Char(partB[2])!='T') {
			Course_Heading = 0;
			err.add("GPVTG Course Heading code is not T");
		}
		
		
		if (mycodes.Gps_Double(partB[3], result)) {
			Magnetic_Heading = result[0];
		} else {
			Magnetic_Heading = 0;
			err.add("GPVTG invalid Magnetic Heading : "+partB[3]);
		}
		
		if (mycodes.Gps_Char(partB[4])!='M') {
			Magnetic_Heading = 0;
			err.add("GPVTG Magnetic Heading code is not M");
		}
		
		
		if (mycodes.Gps_Double(partB[5], result)) {
			Horizontal_Knot_Speed = result[0];
		} else {
			Horizontal_Knot_Speed = 0;
			err.add("GPVTG Invalid Horizontal Knot Speed : "+partB[5]);
		}
		
		if (mycodes.Gps_Char(partB[6])!='N') {
			Horizontal_Knot_Speed = 0;
			err.add("GPVTG Horizontal Knot Speed code is not N");
		}
		
		if (mycodes.Gps_Double(partB[7], result)) {
			Horizontal_KM_Speed = result[0];
		} else {
			Horizontal_KM_Speed = 0;
			err.add("GPVTG Invalid Horizontal KM Speed : "+partB[7]);
		}
		
		if (mycodes.Gps_Char(partB[8])!='K') {
			Horizontal_KM_Speed = 0;
			err.add("GPVTG Horizontal KM Speed code is not K");
		}
		
		invalidmsg = mycodes.Gps_ErrList_to_String(err);
	}
	
	public boolean isValid() {
		if (invalidmsg=="") return true; else return false;
	}
}
