package com.merosee.bustrack.parent;


import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.objects.ServiceHelper;
import anywheresoftware.b4a.debug.*;

public class firebasemessaging extends android.app.Service{
	public static class firebasemessaging_BR extends android.content.BroadcastReceiver {

		@Override
		public void onReceive(android.content.Context context, android.content.Intent intent) {
            BA.LogInfo("** Receiver (firebasemessaging) OnReceive **");
			android.content.Intent in = new android.content.Intent(context, firebasemessaging.class);
			if (intent != null)
				in.putExtra("b4a_internal_intent", intent);
            ServiceHelper.StarterHelper.startServiceFromReceiver (context, in, false, BA.class);
		}

	}
    static firebasemessaging mostCurrent;
	public static BA processBA;
    private ServiceHelper _service;
    public static Class<?> getObject() {
		return firebasemessaging.class;
	}
	@Override
	public void onCreate() {
        super.onCreate();
        mostCurrent = this;
        if (processBA == null) {
		    processBA = new BA(this, null, null, "com.merosee.bustrack.parent", "com.merosee.bustrack.parent.firebasemessaging");
            if (BA.isShellModeRuntimeCheck(processBA)) {
                processBA.raiseEvent2(null, true, "SHELL", false);
		    }
            try {
                Class.forName(BA.applicationContext.getPackageName() + ".main").getMethod("initializeProcessGlobals").invoke(null, null);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            processBA.loadHtSubs(this.getClass());
            ServiceHelper.init();
        }
        _service = new ServiceHelper(this);
        processBA.service = this;
        
        if (BA.isShellModeRuntimeCheck(processBA)) {
			processBA.raiseEvent2(null, true, "CREATE", true, "com.merosee.bustrack.parent.firebasemessaging", processBA, _service, anywheresoftware.b4a.keywords.Common.Density);
		}
        if (!false && ServiceHelper.StarterHelper.startFromServiceCreate(processBA, false) == false) {
				
		}
		else {
            processBA.setActivityPaused(false);
            BA.LogInfo("*** Service (firebasemessaging) Create ***");
            processBA.raiseEvent(null, "service_create");
        }
        processBA.runHook("oncreate", this, null);
        if (false) {
			if (ServiceHelper.StarterHelper.runWaitForLayouts() == false) {
                BA.LogInfo("stopping spontaneous created service");
                stopSelf();
            }
		}
    }
		@Override
	public void onStart(android.content.Intent intent, int startId) {
		onStartCommand(intent, 0, 0);
    }
    @Override
    public int onStartCommand(final android.content.Intent intent, int flags, int startId) {
    	if (ServiceHelper.StarterHelper.onStartCommand(processBA, new Runnable() {
            public void run() {
                handleStart(intent);
            }}))
			;
		else {
			ServiceHelper.StarterHelper.addWaitForLayout (new Runnable() {
				public void run() {
                    processBA.setActivityPaused(false);
                    BA.LogInfo("** Service (firebasemessaging) Create **");
                    processBA.raiseEvent(null, "service_create");
					handleStart(intent);
                    ServiceHelper.StarterHelper.removeWaitForLayout();
				}
			});
		}
        processBA.runHook("onstartcommand", this, new Object[] {intent, flags, startId});
		return android.app.Service.START_NOT_STICKY;
    }
    public void onTaskRemoved(android.content.Intent rootIntent) {
        super.onTaskRemoved(rootIntent);
        if (false)
            processBA.raiseEvent(null, "service_taskremoved");
            
    }
    private void handleStart(android.content.Intent intent) {
    	BA.LogInfo("** Service (firebasemessaging) Start **");
    	java.lang.reflect.Method startEvent = processBA.htSubs.get("service_start");
    	if (startEvent != null) {
    		if (startEvent.getParameterTypes().length > 0) {
    			anywheresoftware.b4a.objects.IntentWrapper iw = ServiceHelper.StarterHelper.handleStartIntent(intent, _service, processBA);
    			processBA.raiseEvent(null, "service_start", iw);
    		}
    		else {
    			processBA.raiseEvent(null, "service_start");
    		}
    	}
    }

	public void onTimeout(int startId) {
        BA.LogInfo("** Service (firebasemessaging) Timeout **");
        anywheresoftware.b4a.objects.collections.Map params = new anywheresoftware.b4a.objects.collections.Map();
        params.Initialize();
        params.Put("StartId", startId);
        processBA.raiseEvent(null, "service_timeout", params);
            
    }
	@Override
	public void onDestroy() {
        super.onDestroy();
        if (false) {
            BA.LogInfo("** Service (firebasemessaging) Destroy (ignored)**");
        }
        else {
            BA.LogInfo("** Service (firebasemessaging) Destroy **");
		    processBA.raiseEvent(null, "service_destroy");
            processBA.service = null;
		    mostCurrent = null;
		    processBA.setActivityPaused(true);
            processBA.runHook("ondestroy", this, null);
        }
	}

@Override
	public android.os.IBinder onBind(android.content.Intent intent) {
		return null;
	}public anywheresoftware.b4a.keywords.Common __c = null;
public static anywheresoftware.b4a.objects.FirebaseNotificationsService.FirebaseMessageWrapper _fm = null;
public com.merosee.bustrack.parent.main _main = null;
public com.merosee.bustrack.parent.starter _starter = null;
public com.merosee.bustrack.parent.mapactivity _mapactivity = null;
public com.merosee.bustrack.parent.apimodule _apimodule = null;
public com.merosee.bustrack.parent.httputils2service _httputils2service = null;
public static String  _fm_messagearrived(anywheresoftware.b4a.objects.FirebaseNotificationsService.RemoteMessageWrapper _message) throws Exception{
anywheresoftware.b4a.objects.collections.Map _data = null;
String _title = "";
String _body = "";
anywheresoftware.b4a.objects.NotificationWrapper _n = null;
 //BA.debugLineNum = 48;BA.debugLine="Sub fm_MessageArrived (Message As RemoteMessage)";
 //BA.debugLineNum = 49;BA.debugLine="Log(\"Message arrived\")";
anywheresoftware.b4a.keywords.Common.LogImpl("65177345","Message arrived",0);
 //BA.debugLineNum = 51;BA.debugLine="Dim data As Map = Message.GetData";
_data = new anywheresoftware.b4a.objects.collections.Map();
_data = _message.GetData();
 //BA.debugLineNum = 52;BA.debugLine="Dim title As String = \"BusTrack Alert\"";
_title = "BusTrack Alert";
 //BA.debugLineNum = 53;BA.debugLine="Dim body As String = \"Status updated.\"";
_body = "Status updated.";
 //BA.debugLineNum = 56;BA.debugLine="If data.ContainsKey(\"title\") Then";
if (_data.ContainsKey((Object)("title"))) { 
 //BA.debugLineNum = 57;BA.debugLine="title = data.Get(\"title\")";
_title = BA.ObjectToString(_data.Get((Object)("title")));
 };
 //BA.debugLineNum = 60;BA.debugLine="If data.ContainsKey(\"body\") Then";
if (_data.ContainsKey((Object)("body"))) { 
 //BA.debugLineNum = 61;BA.debugLine="body = data.Get(\"body\")";
_body = BA.ObjectToString(_data.Get((Object)("body")));
 };
 //BA.debugLineNum = 64;BA.debugLine="Log(\"Displaying Notification: \" & title & \" | \" &";
anywheresoftware.b4a.keywords.Common.LogImpl("65177360","Displaying Notification: "+_title+" | "+_body,0);
 //BA.debugLineNum = 66;BA.debugLine="Dim n As Notification";
_n = new anywheresoftware.b4a.objects.NotificationWrapper();
 //BA.debugLineNum = 69;BA.debugLine="n.Initialize";
_n.Initialize();
 //BA.debugLineNum = 70;BA.debugLine="n.Icon = \"icon\"";
_n.setIcon("icon");
 //BA.debugLineNum = 71;BA.debugLine="n.SetInfo(title, body, Main)";
_n.SetInfoNew(processBA,BA.ObjectToCharSequence(_title),BA.ObjectToCharSequence(_body),(Object)(mostCurrent._main.getObject()));
 //BA.debugLineNum = 72;BA.debugLine="n.Notify(1)";
_n.Notify((int) (1));
 //BA.debugLineNum = 73;BA.debugLine="End Sub";
return "";
}
public static String  _fm_tokenrefresh(String _token) throws Exception{
 //BA.debugLineNum = 26;BA.debugLine="Sub fm_TokenRefresh (Token As String)";
 //BA.debugLineNum = 27;BA.debugLine="Log(\"FCM Token Received/Refreshed: \" & Token)";
anywheresoftware.b4a.keywords.Common.LogImpl("65111809","FCM Token Received/Refreshed: "+_token,0);
 //BA.debugLineNum = 30;BA.debugLine="If Token <> \"\" And Token <> \"null\" And Token.Leng";
if ((_token).equals("") == false && (_token).equals("null") == false && _token.length()>20) { 
 //BA.debugLineNum = 32;BA.debugLine="File.WriteString(File.DirInternal, \"fcm_token.tx";
anywheresoftware.b4a.keywords.Common.File.WriteString(anywheresoftware.b4a.keywords.Common.File.getDirInternal(),"fcm_token.txt",_token);
 //BA.debugLineNum = 33;BA.debugLine="Log(\"Valid FCM token saved\")";
anywheresoftware.b4a.keywords.Common.LogImpl("65111815","Valid FCM token saved",0);
 //BA.debugLineNum = 36;BA.debugLine="File.WriteString(File.DirInternal, \"fcm_token_ti";
anywheresoftware.b4a.keywords.Common.File.WriteString(anywheresoftware.b4a.keywords.Common.File.getDirInternal(),"fcm_token_timestamp.txt",BA.NumberToString(anywheresoftware.b4a.keywords.Common.DateTime.getNow()));
 //BA.debugLineNum = 39;BA.debugLine="CallSubDelayed(Main, \"TryRegisterFCM\")";
anywheresoftware.b4a.keywords.Common.CallSubDelayed(processBA,(Object)(mostCurrent._main.getObject()),"TryRegisterFCM");
 //BA.debugLineNum = 41;BA.debugLine="Log(\"Token auto-refresh: Re-registered with serv";
anywheresoftware.b4a.keywords.Common.LogImpl("65111823","Token auto-refresh: Re-registered with server",0);
 }else {
 //BA.debugLineNum = 43;BA.debugLine="Log(\"Invalid FCM token received, not saving: \" &";
anywheresoftware.b4a.keywords.Common.LogImpl("65111825","Invalid FCM token received, not saving: "+_token,0);
 };
 //BA.debugLineNum = 45;BA.debugLine="End Sub";
return "";
}
public static String  _gettoken() throws Exception{
String _token = "";
 //BA.debugLineNum = 75;BA.debugLine="Public Sub GetToken As String";
 //BA.debugLineNum = 76;BA.debugLine="If File.Exists(File.DirInternal, \"fcm_token.txt\")";
if (anywheresoftware.b4a.keywords.Common.File.Exists(anywheresoftware.b4a.keywords.Common.File.getDirInternal(),"fcm_token.txt")) { 
 //BA.debugLineNum = 77;BA.debugLine="Dim token As String = File.ReadString(File.DirIn";
_token = anywheresoftware.b4a.keywords.Common.File.ReadString(anywheresoftware.b4a.keywords.Common.File.getDirInternal(),"fcm_token.txt");
 //BA.debugLineNum = 80;BA.debugLine="If token <> \"\" And token <> \"null\" And token.Len";
if ((_token).equals("") == false && (_token).equals("null") == false && _token.length()>20) { 
 //BA.debugLineNum = 81;BA.debugLine="Return token";
if (true) return _token;
 };
 };
 //BA.debugLineNum = 84;BA.debugLine="Return \"\"";
if (true) return "";
 //BA.debugLineNum = 85;BA.debugLine="End Sub";
return "";
}
public static String  _process_globals() throws Exception{
 //BA.debugLineNum = 6;BA.debugLine="Sub Process_Globals";
 //BA.debugLineNum = 7;BA.debugLine="Private fm As FirebaseMessaging";
_fm = new anywheresoftware.b4a.objects.FirebaseNotificationsService.FirebaseMessageWrapper();
 //BA.debugLineNum = 8;BA.debugLine="End Sub";
return "";
}
public static String  _service_create() throws Exception{
 //BA.debugLineNum = 10;BA.debugLine="Sub Service_Create";
 //BA.debugLineNum = 11;BA.debugLine="fm.Initialize(\"fm\")";
_fm.Initialize(processBA,"fm");
 //BA.debugLineNum = 12;BA.debugLine="Log(\"FirebaseMessaging Initialized\")";
anywheresoftware.b4a.keywords.Common.LogImpl("64915202","FirebaseMessaging Initialized",0);
 //BA.debugLineNum = 14;BA.debugLine="fm.SubscribeToTopic(\"all_parents\")";
_fm.SubscribeToTopic("all_parents");
 //BA.debugLineNum = 15;BA.debugLine="End Sub";
return "";
}
public static String  _service_destroy() throws Exception{
 //BA.debugLineNum = 21;BA.debugLine="Sub Service_Destroy";
 //BA.debugLineNum = 23;BA.debugLine="End Sub";
return "";
}
public static String  _service_start(anywheresoftware.b4a.objects.IntentWrapper _startingintent) throws Exception{
 //BA.debugLineNum = 17;BA.debugLine="Sub Service_Start (StartingIntent As Intent)";
 //BA.debugLineNum = 18;BA.debugLine="If fm.HandleIntent(StartingIntent) Then Return";
if (_fm.HandleIntent((android.content.Intent)(_startingintent.getObject()))) { 
if (true) return "";};
 //BA.debugLineNum = 19;BA.debugLine="End Sub";
return "";
}
}
