package com.example.myapp;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

public class PermissionUtils {
    // 请求码
    public static final int REQUEST_STORAGE_PERMISSION = 101;
    public static final int REQUEST_MEDIA_PERMISSION = 102; // Android 13+ 媒体权限

    // 检查并请求所有必要权限
    public static boolean checkAndRequestPermissions(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
            // Android 14+ 需要检查媒体权限（如果要访问媒体文件）
            return checkMediaPermissions(activity);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            // Android 10-13 使用 SAF 或 MediaStore，不需要存储权限
            return true;
        } else {
            // Android 9 及以下需要传统存储权限
            return checkLegacyStoragePermission(activity);
        }
    }

    // Android 14+ 媒体权限检查
    private static boolean checkMediaPermissions(Activity activity) {
        String[] permissions;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            permissions = new String[]{
                    Manifest.permission.READ_MEDIA_IMAGES,
                    Manifest.permission.READ_MEDIA_VIDEO,
                    Manifest.permission.READ_MEDIA_AUDIO
            };
        } else {
            permissions = new String[]{Manifest.permission.READ_EXTERNAL_STORAGE};
        }

        boolean allGranted = true;
        for (String perm : permissions) {
            if (ContextCompat.checkSelfPermission(activity, perm) != PackageManager.PERMISSION_GRANTED) {
                allGranted = false;
                break;
            }
        }

        if (!allGranted) {
            ActivityCompat.requestPermissions(activity, permissions, REQUEST_MEDIA_PERMISSION);
            return false;
        }
        return true;
    }

    // Android 9 及以下传统存储权限
    private static boolean checkLegacyStoragePermission(Activity activity) {
        if (ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(activity,
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    REQUEST_STORAGE_PERMISSION);
            return false;
        }
        return true;
    }

    // 处理权限请求结果
    public static boolean handlePermissionResult(int requestCode,
                                                 String[] permissions, int[] grantResults) {
        if (grantResults.length == 0) return false;

        if (requestCode == REQUEST_STORAGE_PERMISSION ||
                requestCode == REQUEST_MEDIA_PERMISSION) {
            for (int result : grantResults) {
                if (result != PackageManager.PERMISSION_GRANTED) {
                    return false;
                }
            }
            return true;
        }
        return false;
    }
}