import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:permission_handler/permission_handler.dart';

class PermissionUtils {
  /// 检测是否有权限
  /// [permissionList] 权限申请列表
  static Future<bool> checkPermission(List<Permission> permissionList,
      {bool showDialog = true}) async {
    // 一个新待申请权限列表
    List<Permission> newPermissionList = [];
    // 遍历当前权限申请列表
    for (Permission permission in permissionList) {
      PermissionStatus status = await permission.status;
      // 如果不是允许状态就添加到新的申请列表中
      if (!status.isGranted) {
        newPermissionList.add(permission);
      }
    }

    // 如果需要重新申请的列表不是空的
    if (newPermissionList.isNotEmpty) {
      PermissionStatus permissionStatus =
          await _requestPermission(newPermissionList);
      switch (permissionStatus) {
        // 拒绝状态
        case PermissionStatus.denied:
          if (showDialog) _showFailedDialog(newPermissionList);
          return false;
        // 允许状态
        case PermissionStatus.granted:
        case PermissionStatus.limited:
          return true;
        // 永久拒绝
        case PermissionStatus.provisional:
        case PermissionStatus.restricted:
        case PermissionStatus.permanentlyDenied:
          if (showDialog) {
            _showFailedDialog(newPermissionList, isPermanentlyDenied: true);
          }
          break;
      }
    } else {
      return true;
    }
    return false;
  }

  /// 获取新列表中的权限 如果有一项不合格就返回false
  static Future<PermissionStatus> _requestPermission(
      List<Permission> permissionList) async {
    Map<Permission, PermissionStatus> statuses = await permissionList.request();
    PermissionStatus currentPermissionStatus = PermissionStatus.granted;
    statuses.forEach((key, value) {
      if (!value.isGranted || !value.isLimited) {
        currentPermissionStatus = value;
        return;
      }
    });
    return currentPermissionStatus;
  }

  /// 请求位置权限
  static Future<bool> checkLocationAlways() async {
    // 获取前置状态
    // Android没有这一步 ios会先访问这个再访问其他的
    PermissionStatus status1 = PermissionStatus.granted;
    status1 = await _checkSinglePermission(Permission.locationWhenInUse);

    // 获取第二个状态
    PermissionStatus status2 = PermissionStatus.denied;

    // 如果前置状态为成功才能执行获取第二个状态
    if (status1.isGranted) {
      status2 = await _checkSinglePermission(Permission.locationAlways);
    }

    // 如果两个都成功那么就返回成功
    if (status1.isGranted && status2.isGranted) {
      return true;
    } else {
      // 如果有一个拒绝那么就失败了
      _showFailedDialog(
        [Permission.locationWhenInUse, Permission.locationAlways],
        isPermanentlyDenied: Platform.isIOS ? true : false,
      );
    }
    return false;
  }

  /// 请求单个权限
  static _checkSinglePermission(Permission permission) async {
    // 获取当前状态
    PermissionStatus status = await permission.status;
    PermissionStatus currentPermissionStatus = PermissionStatus.granted;
    // 如果它状态不是允许那么就去获取
    if (!status.isGranted) {
      currentPermissionStatus = await _requestPermission([permission]);
    }
    // 返回最终状态
    return currentPermissionStatus;
  }

  /// 权限拒绝后弹窗
  static _showFailedDialog(
    List<Permission> permissionList, {
    bool isPermanentlyDenied = false,
  }) async {
    Get.dialog(
      barrierDismissible: false,
      CupertinoAlertDialog(
        title: Text(
          "Reminder",
          style: TextStyle(
            color: Colors.black,
            fontSize: 18.sp,
            fontWeight: FontWeight.w600,
          ),
        ),
        content: Text(
          await _getDescription(permissionList),
          style: TextStyle(
            color: const Color(0xFF333333),
            fontSize: 14.sp,
          ),
        ),
        actions: [
          CupertinoDialogAction(
            onPressed: Get.back,
            textStyle: TextStyle(
              color: const Color(0xFF666666),
              fontSize: 16.sp,
              fontWeight: FontWeight.w500,
            ),
            child: const Text('Cancel'),
          ),
          CupertinoDialogAction(
            onPressed: () {
              if (isPermanentlyDenied) {
                openAppSettings();
              } else {
                checkPermission(permissionList);
              }
            },
            textStyle: TextStyle(
              color: Colors.black,
              fontSize: 16.sp,
              fontWeight: FontWeight.w500,
            ),
            child: Text(isPermanentlyDenied ? 'Go open' : 'Confirm'),
          ),
        ],
      ),
    );
  }

  /// 获取权限描述
  static Future<String> _getDescription(List<Permission> permissionList) async {
    late Permission failedPermission;
    for (Permission permission in permissionList) {
      if (!await permission.status.isGranted) {
        failedPermission = permission;
        break;
      }
    }

    String description = '';
    if (failedPermission == Permission.camera) {
      description =
          'This will enable you to take photos and recognize text within them for translation.';
    } else if (failedPermission == Permission.photos) {
      description =
          'This will enable you to select photos from your library for text recognition and translation.';
    } else if (failedPermission == Permission.microphone) {
      description =
          'This will enable you to input content through voice for recognition and translation.';
    } else if (failedPermission == Permission.speech) {
      description =
          'This will enable you to use speech recognition to convert voice content to text for translation.';
    } else if (failedPermission == Permission.appTrackingTransparency) {
      description =
          'This will help us provide a more personalized advertising experience. Your data privacy will be protected.';
    }
    return description;
  }
}
