
package io.raks.wakeupapp;

import android.annotation.SuppressLint;
import android.app.Activity;

import android.content.Context;
import android.content.Intent;

import android.os.Build;
import android.os.Bundle;

import android.os.PowerManager;
import android.util.Log;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;

import java.util.concurrent.TimeUnit;

public class RNWakeUpApp extends ReactContextBaseJavaModule {

    private static ReactApplicationContext reactContext;
    public static final String LOG_TAG = "RNWakeUpApp";

    public RNWakeUpApp(ReactApplicationContext context) {
        super(context);
        reactContext = context;
    }

    @Override
    public String getName() {
        return "ReactNativeWakeUpApp";
    }

    @ReactMethod
    public void wakeUpApp(ReadableMap params) {
        ReadableMap data = params.hasKey("data") ? params.getMap("data") : null;
        Bundle bundle = null;
        if (data != null) {
            bundle = Arguments.toBundle(data);
        }

        String packageName = reactContext.getPackageName();
        Intent launchIntent = reactContext.getPackageManager().getLaunchIntentForPackage(packageName);
        String className = launchIntent.getComponent().getClassName();

        try {
            Activity currentActivity = getCurrentActivity();

            Class<?> activityClass = Class.forName(className);
            Intent activityIntent = new Intent(reactContext, activityClass);

            activityIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
            activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            if (currentActivity != null) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
                    currentActivity.setShowWhenLocked(true);
                    currentActivity.setTurnScreenOn(true);
                } else {
                    PowerManager pm = (PowerManager) reactContext.getSystemService(Context.POWER_SERVICE);
                    @SuppressLint("InvalidWakeLockTag") PowerManager.WakeLock wakelock = pm.newWakeLock(
                            PowerManager.FULL_WAKE_LOCK |
                                    PowerManager.ACQUIRE_CAUSES_WAKEUP, packageName);

                    wakelock.acquire(TimeUnit.SECONDS.toMillis(5));
                }
                currentActivity.startActivity(activityIntent);
            }
            //had to start the intent as well along with current activity to wake the app up. will look into this later
            reactContext.startActivity(activityIntent);
        } catch (Exception e) {
            Log.e(LOG_TAG, "Class not found", e);
            return;
        }

        sendEvent(bundle);
    }

    public static void sendEvent(Bundle bundle) {
        if (bundle != null) {
            reactContext
                    .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
                    .emit("wakeUpApp", Arguments.fromBundle(bundle));
        }
    }
}
