import 'dart:ffi';

import 'package:permission_handler/permission_handler.dart';
import 'dart:io';

import '../device_info.dart';
import '../toast_utils.dart';

class AndroidPermissionHandler {
  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;
    if (Platform.isAndroid && await DeviceInfo.getAndroidSdkVersion() >= 29) {
      status = await Permission.locationAlways.request();
    } else {
      status = await Permission.locationWhenInUse.request();
    }

    if (status.isGranted) {
      print("Android: 位置权限已授予");
    } else if (status.isPermanentlyDenied) {
      print("Android: 位置权限被永久拒绝，请前往设置开启");
    } else {
      print("Android: 位置权限被拒绝");
    }
  }

  Future<void> requestBluetoothPermission() async {
    PermissionStatus status;
    if (Platform.isAndroid && await DeviceInfo.getAndroidSdkVersion() >= 31) {
      status = (await [
        Permission.bluetoothScan,
        Permission.bluetoothConnect,
        Permission.bluetoothAdvertise,
      ].request()) as PermissionStatus;
    } else {
      status = await Permission.bluetooth.request();
    }

    if (status.isGranted) {
      print("Android: 蓝牙权限已授予");
    } else {
      print("Android: 蓝牙权限被拒绝");
    }
  }

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

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

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

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

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