package com.tencent.qian.webview.permission;

import android.app.Activity;
import android.content.pm.PackageManager;

import androidx.activity.ComponentActivity;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import java.util.HashMap;
import java.util.Map;

/**
 * 运行时权限申请器（基于 ActivityResultContracts.RequestMultiplePermissions）。
 */
public class RuntimePermissionRequester {

    private final ActivityResultLauncher<String[]> launcher;
    private final State state;

    private RuntimePermissionRequester(@NonNull ActivityResultLauncher<String[]> launcher, @NonNull State state) {
        this.launcher = launcher;
        this.state = state;
    }

    private static class State {
        Callback pending;
    }

    public interface Callback {
        void onResult(@NonNull Map<String, Boolean> grantResults);
    }

    public boolean isGranted(@NonNull Activity activity, @NonNull String permission) {
        return ContextCompat.checkSelfPermission(activity, permission) == PackageManager.PERMISSION_GRANTED;
    }

    public void request(@NonNull Activity activity, @NonNull String[] permissions, @NonNull Callback callback) {
        boolean all = true;
        for (String perm : permissions) {
            if (!isGranted(activity, perm)) {
                all = false;
                break;
            }
        }
        if (all) {
            Map<String, Boolean> results = new HashMap<>();
            for (String perm : permissions) results.put(perm, true);
            callback.onResult(results);
            return;
        }
        state.pending = callback;
        launcher.launch(permissions);
    }

    @NonNull
    public static RuntimePermissionRequester register(@NonNull ComponentActivity activity) {
        State state = new State();
        ActivityResultLauncher<String[]> launcher = activity.registerForActivityResult(
                new ActivityResultContracts.RequestMultiplePermissions(),
                grants -> {
                    if (state.pending != null) {
                        state.pending.onResult(grants);
                        state.pending = null;
                    }
                }
        );
        return new RuntimePermissionRequester(launcher, state);
    }

    @NonNull
    public static RuntimePermissionRequester register(@NonNull Fragment fragment) {
        State state = new State();
        ActivityResultLauncher<String[]> launcher = fragment.registerForActivityResult(
                new ActivityResultContracts.RequestMultiplePermissions(),
                grants -> {
                    if (state.pending != null) {
                        state.pending.onResult(grants);
                        state.pending = null;
                    }
                }
        );
        return new RuntimePermissionRequester(launcher, state);
    }
}
