package com.merosee.bustrack.parent;


import anywheresoftware.b4a.B4AMenuItem;
import android.app.Activity;
import android.os.Bundle;
import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BALayout;
import anywheresoftware.b4a.B4AActivity;
import anywheresoftware.b4a.ObjectWrapper;
import anywheresoftware.b4a.objects.ActivityWrapper;
import java.lang.reflect.InvocationTargetException;
import anywheresoftware.b4a.B4AUncaughtException;
import anywheresoftware.b4a.debug.*;
import java.lang.ref.WeakReference;

public class mapactivity extends Activity implements B4AActivity{
	public static mapactivity mostCurrent;
	static boolean afterFirstLayout;
	static boolean isFirst = true;
    private static boolean processGlobalsRun = false;
	BALayout layout;
	public static BA processBA;
	BA activityBA;
    ActivityWrapper _activity;
    java.util.ArrayList<B4AMenuItem> menuItems;
	public static final boolean fullScreen = false;
	public static final boolean includeTitle = true;
    public static WeakReference<Activity> previousOne;
    public static boolean dontPause;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        mostCurrent = this;
		if (processBA == null) {
			processBA = new BA(this.getApplicationContext(), null, null, "com.merosee.bustrack.parent", "com.merosee.bustrack.parent.mapactivity");
			processBA.loadHtSubs(this.getClass());
	        float deviceScale = getApplicationContext().getResources().getDisplayMetrics().density;
	        BALayout.setDeviceScale(deviceScale);
            
		}
		else if (previousOne != null) {
			Activity p = previousOne.get();
			if (p != null && p != this) {
                BA.LogInfo("Killing previous instance (mapactivity).");
				p.finish();
			}
		}
        processBA.setActivityPaused(true);
        processBA.runHook("oncreate", this, null);
		if (!includeTitle) {
        	this.getWindow().requestFeature(android.view.Window.FEATURE_NO_TITLE);
        }
        if (fullScreen) {
        	getWindow().setFlags(android.view.WindowManager.LayoutParams.FLAG_FULLSCREEN,   
        			android.view.WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
		
        processBA.sharedProcessBA.activityBA = null;
		layout = new BALayout(this);
		setContentView(layout);
		afterFirstLayout = false;
        WaitForLayout wl = new WaitForLayout();
        if (anywheresoftware.b4a.objects.ServiceHelper.StarterHelper.startFromActivity(this, processBA, wl, false))
		    BA.handler.postDelayed(wl, 5);

	}
	static class WaitForLayout implements Runnable {
		public void run() {
			if (afterFirstLayout)
				return;
			if (mostCurrent == null)
				return;
            
			if (mostCurrent.layout.getWidth() == 0) {
				BA.handler.postDelayed(this, 5);
				return;
			}
			mostCurrent.layout.getLayoutParams().height = mostCurrent.layout.getHeight();
			mostCurrent.layout.getLayoutParams().width = mostCurrent.layout.getWidth();
			afterFirstLayout = true;
			mostCurrent.afterFirstLayout();
		}
	}
	private void afterFirstLayout() {
        if (this != mostCurrent)
			return;
		activityBA = new BA(this, layout, processBA, "com.merosee.bustrack.parent", "com.merosee.bustrack.parent.mapactivity");
        
        processBA.sharedProcessBA.activityBA = new java.lang.ref.WeakReference<BA>(activityBA);
        anywheresoftware.b4a.objects.ViewWrapper.lastId = 0;
        _activity = new ActivityWrapper(activityBA, "activity");
        anywheresoftware.b4a.Msgbox.isDismissing = false;
        if (BA.isShellModeRuntimeCheck(processBA)) {
			if (isFirst)
				processBA.raiseEvent2(null, true, "SHELL", false);
			processBA.raiseEvent2(null, true, "CREATE", true, "com.merosee.bustrack.parent.mapactivity", processBA, activityBA, _activity, anywheresoftware.b4a.keywords.Common.Density, mostCurrent);
			_activity.reinitializeForShell(activityBA, "activity");
		}
        initializeProcessGlobals();		
        initializeGlobals();
        
        BA.LogInfo("** Activity (mapactivity) Create " + (isFirst ? "(first time)" : "") + " **");
        processBA.raiseEvent2(null, true, "activity_create", false, isFirst);
		isFirst = false;
		if (this != mostCurrent)
			return;
        processBA.setActivityPaused(false);
        BA.LogInfo("** Activity (mapactivity) Resume **");
        processBA.raiseEvent(null, "activity_resume");
        if (android.os.Build.VERSION.SDK_INT >= 11) {
			try {
				android.app.Activity.class.getMethod("invalidateOptionsMenu").invoke(this,(Object[]) null);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

	}
	public void addMenuItem(B4AMenuItem item) {
		if (menuItems == null)
			menuItems = new java.util.ArrayList<B4AMenuItem>();
		menuItems.add(item);
	}
	@Override
	public boolean onCreateOptionsMenu(android.view.Menu menu) {
		super.onCreateOptionsMenu(menu);
        try {
            if (processBA.subExists("activity_actionbarhomeclick")) {
                Class.forName("android.app.ActionBar").getMethod("setHomeButtonEnabled", boolean.class).invoke(
                    getClass().getMethod("getActionBar").invoke(this), true);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (processBA.runHook("oncreateoptionsmenu", this, new Object[] {menu}))
            return true;
		if (menuItems == null)
			return false;
		for (B4AMenuItem bmi : menuItems) {
			android.view.MenuItem mi = menu.add(bmi.title);
			if (bmi.drawable != null)
				mi.setIcon(bmi.drawable);
            if (android.os.Build.VERSION.SDK_INT >= 11) {
				try {
                    if (bmi.addToBar) {
				        android.view.MenuItem.class.getMethod("setShowAsAction", int.class).invoke(mi, 1);
                    }
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			mi.setOnMenuItemClickListener(new B4AMenuItemsClickListener(bmi.eventName.toLowerCase(BA.cul)));
		}
        
		return true;
	}   
 @Override
 public boolean onOptionsItemSelected(android.view.MenuItem item) {
    if (item.getItemId() == 16908332) {
        processBA.raiseEvent(null, "activity_actionbarhomeclick");
        return true;
    }
    else
        return super.onOptionsItemSelected(item); 
}
@Override
 public boolean onPrepareOptionsMenu(android.view.Menu menu) {
    super.onPrepareOptionsMenu(menu);
    processBA.runHook("onprepareoptionsmenu", this, new Object[] {menu});
    return true;
    
 }
 protected void onStart() {
    super.onStart();
    processBA.runHook("onstart", this, null);
}
 protected void onStop() {
    super.onStop();
    processBA.runHook("onstop", this, null);
}
    public void onWindowFocusChanged(boolean hasFocus) {
       super.onWindowFocusChanged(hasFocus);
       if (processBA.subExists("activity_windowfocuschanged"))
           processBA.raiseEvent2(null, true, "activity_windowfocuschanged", false, hasFocus);
    }
	private class B4AMenuItemsClickListener implements android.view.MenuItem.OnMenuItemClickListener {
		private final String eventName;
		public B4AMenuItemsClickListener(String eventName) {
			this.eventName = eventName;
		}
		public boolean onMenuItemClick(android.view.MenuItem item) {
			processBA.raiseEventFromUI(item.getTitle(), eventName + "_click");
			return true;
		}
	}
    public static Class<?> getObject() {
		return mapactivity.class;
	}
    private Boolean onKeySubExist = null;
    private Boolean onKeyUpSubExist = null;
	@Override
	public boolean onKeyDown(int keyCode, android.view.KeyEvent event) {
        if (processBA.runHook("onkeydown", this, new Object[] {keyCode, event}))
            return true;
		if (onKeySubExist == null)
			onKeySubExist = processBA.subExists("activity_keypress");
		if (onKeySubExist) {
			if (keyCode == anywheresoftware.b4a.keywords.constants.KeyCodes.KEYCODE_BACK &&
					android.os.Build.VERSION.SDK_INT >= 18) {
				HandleKeyDelayed hk = new HandleKeyDelayed();
				hk.kc = keyCode;
				BA.handler.post(hk);
				return true;
			}
			else {
				boolean res = new HandleKeyDelayed().runDirectly(keyCode);
				if (res)
					return true;
			}
		}
		return super.onKeyDown(keyCode, event);
	}
	private class HandleKeyDelayed implements Runnable {
		int kc;
		public void run() {
			runDirectly(kc);
		}
		public boolean runDirectly(int keyCode) {
			Boolean res =  (Boolean)processBA.raiseEvent2(_activity, false, "activity_keypress", false, keyCode);
			if (res == null || res == true) {
                return true;
            }
            else if (keyCode == anywheresoftware.b4a.keywords.constants.KeyCodes.KEYCODE_BACK) {
				finish();
				return true;
			}
            return false;
		}
		
	}
    @Override
	public boolean onKeyUp(int keyCode, android.view.KeyEvent event) {
        if (processBA.runHook("onkeyup", this, new Object[] {keyCode, event}))
            return true;
		if (onKeyUpSubExist == null)
			onKeyUpSubExist = processBA.subExists("activity_keyup");
		if (onKeyUpSubExist) {
			Boolean res =  (Boolean)processBA.raiseEvent2(_activity, false, "activity_keyup", false, keyCode);
			if (res == null || res == true)
				return true;
		}
		return super.onKeyUp(keyCode, event);
	}
	@Override
	public void onNewIntent(android.content.Intent intent) {
        super.onNewIntent(intent);
		this.setIntent(intent);
        processBA.runHook("onnewintent", this, new Object[] {intent});
	}
    @Override 
	public void onPause() {
		super.onPause();
        if (_activity == null)
            return;
        if (this != mostCurrent)
			return;
		anywheresoftware.b4a.Msgbox.dismiss(true);
        if (!dontPause)
            BA.LogInfo("** Activity (mapactivity) Pause, UserClosed = " + activityBA.activity.isFinishing() + " **");
        else
            BA.LogInfo("** Activity (mapactivity) Pause event (activity is not paused). **");
        if (mostCurrent != null)
            processBA.raiseEvent2(_activity, true, "activity_pause", false, activityBA.activity.isFinishing());		
        if (!dontPause) {
            processBA.setActivityPaused(true);
            mostCurrent = null;
        }

        if (!activityBA.activity.isFinishing())
			previousOne = new WeakReference<Activity>(this);
        anywheresoftware.b4a.Msgbox.isDismissing = false;
        processBA.runHook("onpause", this, null);
	}

	@Override
	public void onDestroy() {
        super.onDestroy();
		previousOne = null;
        processBA.runHook("ondestroy", this, null);
	}
    @Override 
	public void onResume() {
		super.onResume();
        mostCurrent = this;
        anywheresoftware.b4a.Msgbox.isDismissing = false;
        if (activityBA != null) { //will be null during activity create (which waits for AfterLayout).
        	ResumeMessage rm = new ResumeMessage(mostCurrent);
        	BA.handler.post(rm);
        }
        processBA.runHook("onresume", this, null);
	}
    private static class ResumeMessage implements Runnable {
    	private final WeakReference<Activity> activity;
    	public ResumeMessage(Activity activity) {
    		this.activity = new WeakReference<Activity>(activity);
    	}
		public void run() {
            mapactivity mc = mostCurrent;
			if (mc == null || mc != activity.get())
				return;
			processBA.setActivityPaused(false);
            BA.LogInfo("** Activity (mapactivity) Resume **");
            if (mc != mostCurrent)
                return;
		    processBA.raiseEvent(mc._activity, "activity_resume", (Object[])null);
		}
    }
	@Override
	protected void onActivityResult(int requestCode, int resultCode,
	      android.content.Intent data) {
		processBA.onActivityResult(requestCode, resultCode, data);
        processBA.runHook("onactivityresult", this, new Object[] {requestCode, resultCode});
	}
	private static void initializeGlobals() {
		processBA.raiseEvent2(null, true, "globals", false, (Object[])null);
	}
    public void onRequestPermissionsResult(int requestCode,
        String permissions[], int[] grantResults) {
        for (int i = 0;i < permissions.length;i++) {
            Object[] o = new Object[] {permissions[i], grantResults[i] == 0};
            processBA.raiseEventFromDifferentThread(null,null, 0, "activity_permissionresult", true, o);
        }
            
    }

public anywheresoftware.b4a.keywords.Common __c = null;
public static anywheresoftware.b4a.objects.Timer _updatetimer = null;
public anywheresoftware.b4a.objects.WebViewWrapper _wvmap = null;
public anywheresoftware.b4a.objects.LabelWrapper _lblbusinfo = null;
public anywheresoftware.b4a.objects.LabelWrapper _lbleta = null;
public anywheresoftware.b4a.objects.ButtonWrapper _btnrefresh = null;
public anywheresoftware.b4a.objects.collections.Map _currentstudent = null;
public com.merosee.bustrack.parent.main _main = null;
public com.merosee.bustrack.parent.starter _starter = null;
public com.merosee.bustrack.parent.apimodule _apimodule = null;
public com.merosee.bustrack.parent.firebasemessaging _firebasemessaging = null;
public com.merosee.bustrack.parent.httputils2service _httputils2service = null;

public static void initializeProcessGlobals() {
             try {
                Class.forName(BA.applicationContext.getPackageName() + ".main").getMethod("initializeProcessGlobals").invoke(null, null);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
}
public static String  _activity_create(boolean _firsttime) throws Exception{
anywheresoftware.b4a.objects.collections.JSONParser _parser = null;
 //BA.debugLineNum = 20;BA.debugLine="Sub Activity_Create(FirstTime As Boolean)";
 //BA.debugLineNum = 21;BA.debugLine="Activity.Title = \"Track Bus\"";
mostCurrent._activity.setTitle(BA.ObjectToCharSequence("Track Bus"));
 //BA.debugLineNum = 24;BA.debugLine="If File.Exists(File.DirInternal, \"selected_stu";
if (anywheresoftware.b4a.keywords.Common.File.Exists(anywheresoftware.b4a.keywords.Common.File.getDirInternal(),"selected_student.json")) { 
 //BA.debugLineNum = 25;BA.debugLine="Dim parser As JSONParser";
_parser = new anywheresoftware.b4a.objects.collections.JSONParser();
 //BA.debugLineNum = 26;BA.debugLine="parser.Initialize(File.ReadString(File.Dir";
_parser.Initialize(anywheresoftware.b4a.keywords.Common.File.ReadString(anywheresoftware.b4a.keywords.Common.File.getDirInternal(),"selected_student.json"));
 //BA.debugLineNum = 27;BA.debugLine="CurrentStudent = parser.NextObject";
mostCurrent._currentstudent = _parser.NextObject();
 }else {
 //BA.debugLineNum = 29;BA.debugLine="ToastMessageShow(\"No student selected\", Tr";
anywheresoftware.b4a.keywords.Common.ToastMessageShow(BA.ObjectToCharSequence("No student selected"),anywheresoftware.b4a.keywords.Common.True);
 //BA.debugLineNum = 30;BA.debugLine="Activity.Finish";
mostCurrent._activity.Finish();
 //BA.debugLineNum = 31;BA.debugLine="Return";
if (true) return "";
 };
 //BA.debugLineNum = 34;BA.debugLine="InitializeUI";
_initializeui();
 //BA.debugLineNum = 36;BA.debugLine="UpdateTimer.Initialize(\"UpdateTimer\", 5000) '";
_updatetimer.Initialize(processBA,"UpdateTimer",(long) (5000));
 //BA.debugLineNum = 37;BA.debugLine="UpdateTimer.Enabled = True";
_updatetimer.setEnabled(anywheresoftware.b4a.keywords.Common.True);
 //BA.debugLineNum = 38;BA.debugLine="End Sub";
return "";
}
public static String  _activity_pause(boolean _userclosed) throws Exception{
 //BA.debugLineNum = 77;BA.debugLine="Sub Activity_Pause (UserClosed As Boolean)";
 //BA.debugLineNum = 78;BA.debugLine="UpdateTimer.Enabled = False";
_updatetimer.setEnabled(anywheresoftware.b4a.keywords.Common.False);
 //BA.debugLineNum = 79;BA.debugLine="End Sub";
return "";
}
public static String  _activity_resume() throws Exception{
 //BA.debugLineNum = 73;BA.debugLine="Sub Activity_Resume";
 //BA.debugLineNum = 74;BA.debugLine="FetchBusLocation";
_fetchbuslocation();
 //BA.debugLineNum = 75;BA.debugLine="End Sub";
return "";
}
public static String  _btnrefresh_click() throws Exception{
 //BA.debugLineNum = 85;BA.debugLine="Sub btnRefresh_Click";
 //BA.debugLineNum = 86;BA.debugLine="FetchBusLocation";
_fetchbuslocation();
 //BA.debugLineNum = 87;BA.debugLine="End Sub";
return "";
}
public static String  _fetchbuslocation() throws Exception{
int _studentid = 0;
com.merosee.bustrack.parent.httpjob _job = null;
 //BA.debugLineNum = 89;BA.debugLine="Sub FetchBusLocation";
 //BA.debugLineNum = 90;BA.debugLine="Dim studentId As Int = CurrentStudent.GetDefau";
_studentid = (int)(BA.ObjectToNumber(mostCurrent._currentstudent.GetDefault((Object)("id"),(Object)(0))));
 //BA.debugLineNum = 91;BA.debugLine="If studentId = 0 Then Return";
if (_studentid==0) { 
if (true) return "";};
 //BA.debugLineNum = 93;BA.debugLine="Dim job As HttpJob";
_job = new com.merosee.bustrack.parent.httpjob();
 //BA.debugLineNum = 94;BA.debugLine="job.Initialize(\"get_eta\", Me)";
_job._initialize /*String*/ (processBA,"get_eta",mapactivity.getObject());
 //BA.debugLineNum = 95;BA.debugLine="job.Download(APIModule.ServerURL & \"/get_eta.p";
_job._download /*String*/ (mostCurrent._apimodule._serverurl /*String*/ +"/get_eta.php?student_id="+BA.NumberToString(_studentid));
 //BA.debugLineNum = 96;BA.debugLine="End Sub";
return "";
}
public static String  _globals() throws Exception{
 //BA.debugLineNum = 11;BA.debugLine="Sub Globals";
 //BA.debugLineNum = 12;BA.debugLine="Private wvMap As WebView";
mostCurrent._wvmap = new anywheresoftware.b4a.objects.WebViewWrapper();
 //BA.debugLineNum = 13;BA.debugLine="Private lblBusInfo As Label";
mostCurrent._lblbusinfo = new anywheresoftware.b4a.objects.LabelWrapper();
 //BA.debugLineNum = 14;BA.debugLine="Private lblETA As Label";
mostCurrent._lbleta = new anywheresoftware.b4a.objects.LabelWrapper();
 //BA.debugLineNum = 15;BA.debugLine="Private btnRefresh As Button";
mostCurrent._btnrefresh = new anywheresoftware.b4a.objects.ButtonWrapper();
 //BA.debugLineNum = 17;BA.debugLine="Private CurrentStudent As Map";
mostCurrent._currentstudent = new anywheresoftware.b4a.objects.collections.Map();
 //BA.debugLineNum = 18;BA.debugLine="End Sub";
return "";
}
public static String  _initializeui() throws Exception{
 //BA.debugLineNum = 40;BA.debugLine="Sub InitializeUI";
 //BA.debugLineNum = 42;BA.debugLine="lblBusInfo.Initialize(\"\")";
mostCurrent._lblbusinfo.Initialize(mostCurrent.activityBA,"");
 //BA.debugLineNum = 43;BA.debugLine="lblBusInfo.Text = \"Bus: \" & CurrentStudent.Get";
mostCurrent._lblbusinfo.setText(BA.ObjectToCharSequence("Bus: "+BA.ObjectToString(mostCurrent._currentstudent.GetDefault((Object)("bus_number"),(Object)("N/A")))));
 //BA.debugLineNum = 44;BA.debugLine="lblBusInfo.TextSize = 18";
mostCurrent._lblbusinfo.setTextSize((float) (18));
 //BA.debugLineNum = 45;BA.debugLine="lblBusInfo.Gravity = Gravity.CENTER";
mostCurrent._lblbusinfo.setGravity(anywheresoftware.b4a.keywords.Common.Gravity.CENTER);
 //BA.debugLineNum = 46;BA.debugLine="Activity.AddView(lblBusInfo, 0, 0, 100%x, 50di";
mostCurrent._activity.AddView((android.view.View)(mostCurrent._lblbusinfo.getObject()),(int) (0),(int) (0),anywheresoftware.b4a.keywords.Common.PerXToCurrent((float) (100),mostCurrent.activityBA),anywheresoftware.b4a.keywords.Common.DipToCurrent((int) (50)));
 //BA.debugLineNum = 48;BA.debugLine="lblETA.Initialize(\"\")";
mostCurrent._lbleta.Initialize(mostCurrent.activityBA,"");
 //BA.debugLineNum = 49;BA.debugLine="lblETA.Text = \"Fetching location...\"";
mostCurrent._lbleta.setText(BA.ObjectToCharSequence("Fetching location..."));
 //BA.debugLineNum = 50;BA.debugLine="lblETA.TextSize = 16";
mostCurrent._lbleta.setTextSize((float) (16));
 //BA.debugLineNum = 51;BA.debugLine="lblETA.Gravity = Gravity.CENTER";
mostCurrent._lbleta.setGravity(anywheresoftware.b4a.keywords.Common.Gravity.CENTER);
 //BA.debugLineNum = 52;BA.debugLine="Activity.AddView(lblETA, 0, 50dip, 100%x, 40di";
mostCurrent._activity.AddView((android.view.View)(mostCurrent._lbleta.getObject()),(int) (0),anywheresoftware.b4a.keywords.Common.DipToCurrent((int) (50)),anywheresoftware.b4a.keywords.Common.PerXToCurrent((float) (100),mostCurrent.activityBA),anywheresoftware.b4a.keywords.Common.DipToCurrent((int) (40)));
 //BA.debugLineNum = 55;BA.debugLine="wvMap.Initialize(\"wvMap\")";
mostCurrent._wvmap.Initialize(mostCurrent.activityBA,"wvMap");
 //BA.debugLineNum = 56;BA.debugLine="Activity.AddView(wvMap, 0, 90dip, 100%x, 100%y";
mostCurrent._activity.AddView((android.view.View)(mostCurrent._wvmap.getObject()),(int) (0),anywheresoftware.b4a.keywords.Common.DipToCurrent((int) (90)),anywheresoftware.b4a.keywords.Common.PerXToCurrent((float) (100),mostCurrent.activityBA),(int) (anywheresoftware.b4a.keywords.Common.PerYToCurrent((float) (100),mostCurrent.activityBA)-anywheresoftware.b4a.keywords.Common.DipToCurrent((int) (150))));
 //BA.debugLineNum = 57;BA.debugLine="wvMap.LoadUrl(\"file:///android_asset/map.html\"";
mostCurrent._wvmap.LoadUrl("file:///android_asset/map.html");
 //BA.debugLineNum = 60;BA.debugLine="btnRefresh.Initialize(\"btnRefresh\")";
mostCurrent._btnrefresh.Initialize(mostCurrent.activityBA,"btnRefresh");
 //BA.debugLineNum = 61;BA.debugLine="btnRefresh.Text = \"Refresh Location\"";
mostCurrent._btnrefresh.setText(BA.ObjectToCharSequence("Refresh Location"));
 //BA.debugLineNum = 62;BA.debugLine="Activity.AddView(btnRefresh, 10%x, 100%y - 60d";
mostCurrent._activity.AddView((android.view.View)(mostCurrent._btnrefresh.getObject()),anywheresoftware.b4a.keywords.Common.PerXToCurrent((float) (10),mostCurrent.activityBA),(int) (anywheresoftware.b4a.keywords.Common.PerYToCurrent((float) (100),mostCurrent.activityBA)-anywheresoftware.b4a.keywords.Common.DipToCurrent((int) (60))),anywheresoftware.b4a.keywords.Common.PerXToCurrent((float) (80),mostCurrent.activityBA),anywheresoftware.b4a.keywords.Common.DipToCurrent((int) (50)));
 //BA.debugLineNum = 63;BA.debugLine="End Sub";
return "";
}
public static String  _jobdone(com.merosee.bustrack.parent.httpjob _job) throws Exception{
anywheresoftware.b4a.objects.collections.JSONParser _parser = null;
anywheresoftware.b4a.objects.collections.Map _root = null;
int _eta = 0;
double _dist = 0;
String _busnumber = "";
double _buslat = 0;
double _buslon = 0;
double _stoplat = 0;
double _stoplon = 0;
anywheresoftware.b4a.objects.collections.List _routestops = null;
anywheresoftware.b4a.objects.collections.JSONParser.JSONGenerator _gen = null;
String _errormsg = "";
 //BA.debugLineNum = 98;BA.debugLine="Sub JobDone(Job As HttpJob)";
 //BA.debugLineNum = 99;BA.debugLine="If Job.Success Then";
if (_job._success /*boolean*/ ) { 
 //BA.debugLineNum = 100;BA.debugLine="If Job.JobName = \"get_eta\" Then";
if ((_job._jobname /*String*/ ).equals("get_eta")) { 
 //BA.debugLineNum = 101;BA.debugLine="Try";
try { //BA.debugLineNum = 102;BA.debugLine="Dim parser As JSONParser";
_parser = new anywheresoftware.b4a.objects.collections.JSONParser();
 //BA.debugLineNum = 103;BA.debugLine="parser.Initialize(Job.GetString)";
_parser.Initialize(_job._getstring /*String*/ ());
 //BA.debugLineNum = 104;BA.debugLine="Dim root As Map = parser.NextObjec";
_root = new anywheresoftware.b4a.objects.collections.Map();
_root = _parser.NextObject();
 //BA.debugLineNum = 106;BA.debugLine="If True = root.Get(\"success\") Then";
if (anywheresoftware.b4a.keywords.Common.True==BA.ObjectToBoolean(_root.Get((Object)("success")))) { 
 //BA.debugLineNum = 107;BA.debugLine="Dim eta As Int = root.Get(\"eta";
_eta = (int)(BA.ObjectToNumber(_root.Get((Object)("eta_minutes"))));
 //BA.debugLineNum = 108;BA.debugLine="Dim dist As Double = root.Get(";
_dist = (double)(BA.ObjectToNumber(_root.Get((Object)("distance_km"))));
 //BA.debugLineNum = 109;BA.debugLine="Dim busNumber As String = root";
_busnumber = BA.ObjectToString(_root.Get((Object)("bus_number")));
 //BA.debugLineNum = 111;BA.debugLine="lblBusInfo.Text = \"Bus: \" & bu";
mostCurrent._lblbusinfo.setText(BA.ObjectToCharSequence("Bus: "+_busnumber+" ("+BA.NumberToString(_dist)+" km away)"));
 //BA.debugLineNum = 112;BA.debugLine="lblETA.Text = \"Estimated Arriv";
mostCurrent._lbleta.setText(BA.ObjectToCharSequence("Estimated Arrival: "+BA.NumberToString(_eta)+" minutes"));
 //BA.debugLineNum = 113;BA.debugLine="lblETA.TextColor = Colors.RGB(";
mostCurrent._lbleta.setTextColor(anywheresoftware.b4a.keywords.Common.Colors.RGB((int) (33),(int) (150),(int) (243)));
 //BA.debugLineNum = 116;BA.debugLine="Dim busLat As Double = root.Ge";
_buslat = (double)(BA.ObjectToNumber(_root.Get((Object)("bus_lat"))));
 //BA.debugLineNum = 117;BA.debugLine="Dim busLon As Double = root.Ge";
_buslon = (double)(BA.ObjectToNumber(_root.Get((Object)("bus_lon"))));
 //BA.debugLineNum = 118;BA.debugLine="Dim stopLat As Double = root.G";
_stoplat = (double)(BA.ObjectToNumber(_root.Get((Object)("student_lat"))));
 //BA.debugLineNum = 119;BA.debugLine="Dim stopLon As Double = root.G";
_stoplon = (double)(BA.ObjectToNumber(_root.Get((Object)("student_lon"))));
 //BA.debugLineNum = 121;BA.debugLine="Dim routeStops As List = root.";
_routestops = new anywheresoftware.b4a.objects.collections.List();
_routestops = (anywheresoftware.b4a.objects.collections.List) anywheresoftware.b4a.AbsObjectWrapper.ConvertToWrapper(new anywheresoftware.b4a.objects.collections.List(), (java.util.List)(_root.Get((Object)("route_stops"))));
 //BA.debugLineNum = 122;BA.debugLine="Dim gen As JSONGenerator";
_gen = new anywheresoftware.b4a.objects.collections.JSONParser.JSONGenerator();
 //BA.debugLineNum = 123;BA.debugLine="gen.Initialize2(routeStops)";
_gen.Initialize2(_routestops);
 //BA.debugLineNum = 125;BA.debugLine="UpdateMap(busLat, busLon, stop";
_updatemap(_buslat,_buslon,_stoplat,_stoplon,_gen.ToString());
 }else {
 //BA.debugLineNum = 127;BA.debugLine="Dim errorMsg As String = root.";
_errormsg = BA.ObjectToString(_root.Get((Object)("error")));
 //BA.debugLineNum = 128;BA.debugLine="lblETA.Text = \"Status: \" & err";
mostCurrent._lbleta.setText(BA.ObjectToCharSequence("Status: "+_errormsg));
 //BA.debugLineNum = 129;BA.debugLine="lblETA.TextColor = Colors.Red";
mostCurrent._lbleta.setTextColor(anywheresoftware.b4a.keywords.Common.Colors.Red);
 };
 } 
       catch (Exception e28) {
			processBA.setLastException(e28); //BA.debugLineNum = 132;BA.debugLine="Log(\"Error parsing ETA: \" & LastEx";
anywheresoftware.b4a.keywords.Common.LogImpl("11900578","Error parsing ETA: "+BA.ObjectToString(anywheresoftware.b4a.keywords.Common.LastException(mostCurrent.activityBA)),0);
 //BA.debugLineNum = 133;BA.debugLine="lblETA.Text = \"Error calculating E";
mostCurrent._lbleta.setText(BA.ObjectToCharSequence("Error calculating ETA"));
 };
 };
 }else {
 //BA.debugLineNum = 137;BA.debugLine="lblETA.Text = \"Network error\"";
mostCurrent._lbleta.setText(BA.ObjectToCharSequence("Network error"));
 //BA.debugLineNum = 138;BA.debugLine="Log(\"Network error: \" & Job.ErrorMessage)";
anywheresoftware.b4a.keywords.Common.LogImpl("11900584","Network error: "+_job._errormessage /*String*/ ,0);
 };
 //BA.debugLineNum = 140;BA.debugLine="Job.Release";
_job._release /*String*/ ();
 //BA.debugLineNum = 141;BA.debugLine="End Sub";
return "";
}
public static String  _process_globals() throws Exception{
 //BA.debugLineNum = 7;BA.debugLine="Sub Process_Globals";
 //BA.debugLineNum = 8;BA.debugLine="Private UpdateTimer As Timer";
_updatetimer = new anywheresoftware.b4a.objects.Timer();
 //BA.debugLineNum = 9;BA.debugLine="End Sub";
return "";
}
public static String  _updatemap(double _buslat,double _buslon,double _stoplat,double _stoplon,String _routestopsjson) throws Exception{
anywheresoftware.b4j.object.JavaObject _jo = null;
String _escapedjson = "";
 //BA.debugLineNum = 65;BA.debugLine="Sub UpdateMap(busLat As Double, busLon As Double,";
 //BA.debugLineNum = 67;BA.debugLine="Dim jo As JavaObject = wvMap";
_jo = new anywheresoftware.b4j.object.JavaObject();
_jo = (anywheresoftware.b4j.object.JavaObject) anywheresoftware.b4a.AbsObjectWrapper.ConvertToWrapper(new anywheresoftware.b4j.object.JavaObject(), (java.lang.Object)(mostCurrent._wvmap.getObject()));
 //BA.debugLineNum = 69;BA.debugLine="Dim escapedJson As String = routeStopsJson.Rep";
_escapedjson = _routestopsjson.replace("'","\\'");
 //BA.debugLineNum = 70;BA.debugLine="jo.RunMethod(\"evaluateJavascript\", Array(\"upda";
_jo.RunMethod("evaluateJavascript",new Object[]{(Object)("updatePositions("+BA.NumberToString(_buslat)+","+BA.NumberToString(_buslon)+","+BA.NumberToString(_stoplat)+","+BA.NumberToString(_stoplon)+",'"+_escapedjson+"')"),anywheresoftware.b4a.keywords.Common.Null});
 //BA.debugLineNum = 71;BA.debugLine="End Sub";
return "";
}
public static String  _updatetimer_tick() throws Exception{
 //BA.debugLineNum = 81;BA.debugLine="Sub UpdateTimer_Tick";
 //BA.debugLineNum = 82;BA.debugLine="FetchBusLocation";
_fetchbuslocation();
 //BA.debugLineNum = 83;BA.debugLine="End Sub";
return "";
}
}
