package com.amankumar.cordova.BackgroundServiceAndroid;

import android.app.ActivityManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.util.Log;
import android.widget.Toast;
import android.os.Handler;
import android.os.Looper;

import java.util.List;

import io.ionic.starter.MainActivity;

/**
 * Created by innotical on 31/3/18.
 */

public class MyReceiver extends BroadcastReceiver {

    private static String TAG = "BroadcastReceiver";
    private boolean isAudio;

    private String patient_name;
    private int patient_id;
    private int consultation_id;
    private String profile_pic;
    private boolean is_audio;
    private int total_time_call;
    private boolean is_expired;

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(TAG, "onReceive");

        if (intent != null) {

            //PackageManager pm = context.getPackageManager();
            // Intent launchIntent = pm.getLaunchIntentForPackage(context.getApplicationContext().getPackageName());
            Intent launchIntent = new Intent(context, MainActivity.class);
            launchIntent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(launchIntent);

            String data = intent.getStringExtra("data");

           // Log.d(TAG, data);

            if (data != null && !data.isEmpty()) {

                Handler handler = new Handler(Looper.getMainLooper());
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        BackgroundServiceAndroid.callJS(data);
                    }
                }, 2000);
            } else {
                Log.d(TAG, "Data part is empty");
            }
        }
    }

}