package com.amankumar.cordova;

import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaWebView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Service;

import android.util.Log;


/**
 * This class keeps app running in the background.
 */
 
public class MyService extends Service {

    private static final String TAG = "BackgroundService-MyService";
	public static String JS_callBack = "BackgroundService.callbackResult";
    
	public static CordovaWebView gWebView;


    @Override
    public void onTaskRemoved(Intent rootIntent) {
        super.onTaskRemoved(rootIntent);
        Toast.makeText(this, "App got killed", Toast.LENGTH_SHORT).show();
        callJS("LOGOUT");
    }
    

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e(TAG, "onStartCommand");
        Toast.makeText(this, "App got killed from startcommand", Toast.LENGTH_SHORT).show();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            Intent notificationIntent = new Intent(this, MainActivity.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(this,
                    0, notificationIntent, 0);

            Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                    .setContentTitle("App Update")
                    .setContentText("This is a foreground service notification")
                    .setSmallIcon(0)
                    .setContentIntent(pendingIntent)
                    .build();

            startForeground(1, notification);

            return START_NOT_STICKY;
        } else {
            return START_STICKY;
        }
    }

    @Override
    public void onCreate() {
        Log.e(TAG, "onCreate");
        // Toast.makeText(this, "App got killed from oncreate", Toast.LENGTH_SHORT).show();
        // Toast.makeText(this, "onCreate Service Executed", Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onDestroy() {
        Log.e(TAG, "onDestroy");
        super.onDestroy();
    }


    public void callJS(String data){

        String callBack = "javascript:" + JS_callBack + "(" + data + ")";

        try {
            Log.d(TAG, "sending data to JS");

            gWebView.sendJavascript(callBack);

        } catch(e) {
            Log.d(TAG, "some error in sending data to JS, error => " + e.toString());
        }

    }
}