package chat.vojo.app;

import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;

import com.getcapacitor.JSObject;
import com.getcapacitor.Plugin;
import com.getcapacitor.PluginCall;
import com.getcapacitor.PluginMethod;
import com.getcapacitor.annotation.CapacitorPlugin;

/**
 * Bridges Android 14+ (API 34) full-screen-intent opt-in into JS.
 *
 * On API 34 `USE_FULL_SCREEN_INTENT` was reclassified from "normal" to
 * "special appop" — declaring it in the manifest is no longer enough to
 * actually display a full-screen notification over the lockscreen. The user
 * must opt in via Settings → Apps → Vojo → Full-screen notifications. There's
 * no runtime grant API, only a deep-link.
 *
 * Without the opt-in, `setFullScreenIntent(launchPI, true)` still satisfies
 * the AOSP NotificationManagerService gate (so CallStyle doesn't get silently
 * dropped), but the notification renders as a regular heads-up and the screen
 * doesn't wake over the lockscreen — which was the "why is this just a banner
 * on the lockscreen?" symptom we saw on the Samsung OneUI test device.
 *
 * See docs/plans/dm_calls.md ADR 2.5-fsi for the full history.
 */
@CapacitorPlugin(name = "FullScreenIntent")
public class FullScreenIntentPlugin extends Plugin {

    @PluginMethod
    public void canUseFullScreenIntent(PluginCall call) {
        JSObject ret = new JSObject();
        ret.put("value", canUseFullScreenIntentInternal());
        call.resolve(ret);
    }

    @PluginMethod
    public void openSettings(PluginCall call) {
        Context ctx = getContext();
        Intent intent;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
            // API 34+ has a dedicated Settings screen for the full-screen notification opt-in.
            intent = new Intent(Settings.ACTION_MANAGE_APP_USE_FULL_SCREEN_INTENT);
            intent.setData(Uri.parse("package:" + ctx.getPackageName()));
        } else {
            // Fallback for API ≤33: the per-app notification Settings page is the closest
            // equivalent and also covers channel-level toggles (mute, DND bypass, etc).
            intent = new Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
            intent.putExtra(Settings.EXTRA_APP_PACKAGE, ctx.getPackageName());
        }
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        try {
            ctx.startActivity(intent);
            call.resolve();
        } catch (Throwable t) {
            call.reject("Failed to open FSI settings: " + t.getMessage());
        }
    }

    private boolean canUseFullScreenIntentInternal() {
        // On API ≤33 `USE_FULL_SCREEN_INTENT` is a normal permission — if it's
        // declared in the manifest, the app already has it. Skip the runtime check.
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.UPSIDE_DOWN_CAKE) return true;
        NotificationManager nm = (NotificationManager)
            getContext().getSystemService(Context.NOTIFICATION_SERVICE);
        if (nm == null) return false;
        return nm.canUseFullScreenIntent();
    }
}
