import 'package:fis_i18n/i18n.dart';
import 'package:fis_ui/index.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/managers/base_manager.dart';
import 'package:get/get.dart';
import 'package:permission_handler/permission_handler.dart';

import 'interfaces/permission.dart';

class PermissionManager extends BaseManager implements IPermissionManager {
  PermissionManager();

  ///检查摄像头权限
  Future<bool> checkCameraPermission() async {
    var status = await Permission.camera.status;
    if (status != PermissionStatus.granted) {
      status = await Permission.camera.request();
    }

    if (status == PermissionStatus.denied ||
        status == PermissionStatus.permanentlyDenied ||
        status == PermissionStatus.restricted) {
      await FConfirmAlert.showModalDialog(
        context: Get.context!,
        title: i18nBook.common.cameraPermissionDisabled.t,
        subTitle: i18nBook.common.goToSettingsPageToEnable.t,
        confrimLabel: i18nBook.common.confirm.t,
        cancelLabel: i18nBook.common.cancel.t,
        businessParent: FVirtualPageWidget(''),
        onConfirm: () async {
          await openAppSettings();
        },
      );
    }
    return status == PermissionStatus.granted;
  }

  @override
  Future<bool> checkMediaPermission() async {
    bool checkResult = true;
    if (!kIsMobile) {
      return checkResult;
    }
    var cameraPermissionResult = await checkCameraPermission();
    if (!cameraPermissionResult) {
      return false;
    }
    // 检查麦克风权限
    var microphoneStatus = await Permission.microphone.status;
    if (microphoneStatus != PermissionStatus.granted) {
      microphoneStatus = await Permission.microphone.request();
      if (microphoneStatus == PermissionStatus.denied ||
          microphoneStatus == PermissionStatus.permanentlyDenied ||
          microphoneStatus == PermissionStatus.restricted) {
        await FConfirmAlert.showModalDialog(
          context: Get.context!,
          title: i18nBook.common.microphonePermissionDisabled.t,
          subTitle: i18nBook.common.goToSettingsPageToEnable.t,
          confrimLabel: i18nBook.common.confirm.t,
          cancelLabel: i18nBook.common.cancel.t,
          businessParent: FVirtualPageWidget(''),
          onConfirm: () async {
            await openAppSettings();
          },
        );
      }
      if (microphoneStatus != PermissionStatus.granted) {
        return false;
      }
    }

    //检查存储权限
    var storageStatus = await Permission.storage.status;

    if (!storageStatus.isGranted) {
      // 存储未授予，请求权限
      // 测试发现此处检查的媒体权限不准确，先屏蔽
      // storageStatus = await Permission.storage.request();
      // if (storageStatus == PermissionStatus.denied ||
      //     storageStatus == PermissionStatus.permanentlyDenied ||
      //     storageStatus == PermissionStatus.restricted) {
      //   await FConfirmAlert.showModalDialog(
      //     context: Get.context!,
      //     title: i18nBook.common.mediaFilePermissionDisabled.t,
      //     subTitle: i18nBook.common.goToSettingsPageToEnable.t,
      //     confrimLabel: i18nBook.common.confirm.t,
      //     cancelLabel: i18nBook.common.cancel.t,
      //     businessParent: FVirtualPageWidget(''),
      //     onConfirm: () async {
      //       await openAppSettings();
      //     },
      //   );
      // }
    }
    return checkResult;
  }

  @override
  Future<bool> requestInstallPackagesPermission() async {
    var status = await Permission.requestInstallPackages.status;
    if (status != PermissionStatus.granted) {
      status = await Permission.requestInstallPackages.request();
    }
    if (status.isGranted) {
      return true;
    } else {
      return false;
    }
  }
}

enum PermissionEnum {
  //音视频相关
  VideoAndVoiceRelevant,

  //IO相关
  IORelevant,

  //App 需要的所有权限
  All,
}
