package com.foxx.androidcast.dev;

import android.app.Activity;
import android.app.KeyguardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.PowerManager;
import android.util.Log;
import android.view.WindowManager;

import com.foxx.androidcast.BuildConfig;
import com.foxx.androidcast.MainActivity;

/** Debug deploy: wake screen, keep awake briefly, show UI over lockscreen after adb install. */
public final class DevInstallWakeHelper {
    public static final String ACTION_PREPARE_FOR_DEPLOY =
            "com.foxx.androidcast.dev.PREPARE_FOR_DEPLOY";
    public static final String EXTRA_AFTER_DEPLOY = "after_deploy";
    private static final String TAG = "DevDeployWake";
    private static final long WAKE_MS = 45_000L;

    private static PowerManager.WakeLock deployWakeLock;

    private DevInstallWakeHelper() {}

    /** Called from running app when adb install is about to start (broadcast from rebuild.sh). */
    public static void wakeForDeploy(Context context, String reason) {
        if (!BuildConfig.DEBUG) {
            return;
        }
        Context app = context.getApplicationContext();
        PowerManager pm = app.getSystemService(PowerManager.class);
        if (pm == null) {
            return;
        }
        releaseWakeLock();
        int flags = PowerManager.PARTIAL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP;
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            @SuppressWarnings("deprecation")
            int legacy = flags | PowerManager.ON_AFTER_RELEASE;
            flags = legacy;
        }
        deployWakeLock = pm.newWakeLock(flags, "androidcast:deploy");
        deployWakeLock.acquire(WAKE_MS);
        Log.i(TAG, "wake for deploy (" + reason + ")");
    }

    public static void prepareActivityShowOnDeploy(Activity activity) {
        if (!BuildConfig.DEBUG || activity == null) {
            return;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
            activity.setShowWhenLocked(true);
            activity.setTurnScreenOn(true);
        }
        activity.getWindow().addFlags(
                WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                        | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
                        | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                        | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            KeyguardManager km = activity.getSystemService(KeyguardManager.class);
            if (km != null && km.isKeyguardLocked()) {
                km.requestDismissKeyguard(activity, null);
            }
        }
    }

    public static void launchMainAfterDeploy(Context context) {
        if (!BuildConfig.DEBUG) {
            return;
        }
        Intent launch = new Intent(context, MainActivity.class);
        launch.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                | Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        launch.putExtra(EXTRA_AFTER_DEPLOY, true);
        context.startActivity(launch);
        Log.i(TAG, "launch MainActivity after deploy");
    }

    public static boolean shouldShowAfterDeploy(Intent intent) {
        return intent != null && intent.getBooleanExtra(EXTRA_AFTER_DEPLOY, false);
    }

    public static void releaseWakeLock() {
        PowerManager.WakeLock lock = deployWakeLock;
        deployWakeLock = null;
        if (lock != null && lock.isHeld()) {
            lock.release();
        }
    }
}
