import 'package:permission_handler/permission_handler.dart';

import '../toast_utils.dart';

class IosPermissionHandler {
  Future<void> requestAllPermissions() async {
    await requestLocationPermission();
    await requestBluetoothPermission();
    await requestCameraPermission();
    await requestStoragePermission();
    await requestNotificationPermission();
    await requestPhonePermission();
    await requestMicrophonePermission();
  }

  Future<bool> requestCameraToMicrophonePermissions() async {
    var isCameraPermission = false;
    var isMicrophonePermissio = false;
    if(await requestCameraPermission() == true){
      isCameraPermission = true;
    }
    if(await requestMicrophonePermission() == true){
      isMicrophonePermissio = true;
    }
    return isCameraPermission == true && isMicrophonePermissio == true;
  }

  Future<void> requestLocationPermission() async {
    PermissionStatus status = await Permission.location.request();
    if (status.isGranted) {
      print("iOS: 位置权限已授予");
    } else if (status.isPermanentlyDenied) {
      print("iOS: 位置权限被永久拒绝，请前往设置开启");
    } else {
      print("iOS: 位置权限被拒绝");
    }
  }

  Future<void> requestBluetoothPermission() async {
    PermissionStatus status = await Permission.bluetooth.request();
    if (status.isGranted) {
      print("iOS: 蓝牙权限已授予");
    } else {
      print("iOS: 蓝牙权限被拒绝");
    }
  }

  Future<bool> requestCameraPermission() async {
    var isCameraPermission = false;
    PermissionStatus status = await Permission.camera.request();
    if (status.isGranted) {
      print("iOS: 摄像头权限已授予");
      isCameraPermission = true;
    } else if (status.isPermanentlyDenied) {
      print("iOS: 摄像头权限被永久拒绝，请前往设置开启");
      ToastUtils.showError("权限被永久拒绝，请前往设置开启！");
      openAppSettings();
    } else {
      print("iOS: 摄像头权限被拒绝");
      ToastUtils.showError("权限被拒绝，可能会导致相关功能不可用！");
    }
    return isCameraPermission;
  }

  Future<bool> requestStoragePermission() async {
    var isStoragePermission = false;
    PermissionStatus status = await Permission.photos.request();
    if (status.isGranted) {
      print("iOS: 存储权限已授予");
      isStoragePermission = true;
    } else if (status.isPermanentlyDenied) {
      print("iOS: 存储权限被永久拒绝，请前往设置开启");
    } else {
      print("iOS: 存储权限被拒绝");
    }
    return isStoragePermission;
  }

  Future<void> requestNotificationPermission() async {
    PermissionStatus status = await Permission.notification.request();
    if (status.isGranted) {
      print("iOS: 通知权限已授予");
    } else {
      print("iOS: 通知权限被拒绝");
    }
  }

  Future<void> requestPhonePermission() async {
    PermissionStatus status = await Permission.phone.request();
    if (status.isGranted) {
      print("iOS: 电话权限已授予");
    } else if (status.isPermanentlyDenied) {
      print("iOS: 电话权限被永久拒绝，请前往设置开启");
    } else {
      print("iOS: 电话权限被拒绝");
    }
  }

  static Future<bool> requestMicrophonePermission() async {
    var isMicrophonePermission = false;
    PermissionStatus status = await Permission.microphone.request();
    if (status.isGranted) {
      print("iOS: 麦克风权限已授予");
      isMicrophonePermission = true;
    } else if (status.isPermanentlyDenied) {
      print("iOS: 麦克风权限被永久拒绝，请前往设置开启");
      ToastUtils.showError("权限被永久拒绝，请前往设置开启！");
      openAppSettings();
    } else {
      print("iOS: 麦克风限被拒绝");
      ToastUtils.showError("权限被拒绝，可能会导致相关功能不可用！");
    }
    return isMicrophonePermission;
  }
}