part of utils;

abstract class Access {
  /// 图片权限
  /// [showExplanation] 是否在申请权限前显示说明对话框
  /// [purpose] 权限使用目的说明
  /// [context] 上下文，用于显示对话框
  static Future<bool> photos({
    bool showExplanation = false,
    String purpose = '',
    BuildContext? context,
  }) async {
    // 如果需要在申请权限前显示说明，且提供了上下文
    if (showExplanation && context != null && purpose.isNotEmpty) {
      final bool? shouldRequest = await _showPermissionExplanationDialog(
        context: context,
        permissionName: Platform.isIOS ? '相册权限' : '存储权限',
        purpose: purpose,
      );

      // 用户取消，不申请权限
      if (shouldRequest != true) {
        return false;
      }
    }

    if (Platform.isIOS) {
      final result = await [Permission.photos].request();
      return result[Permission.photos] == PermissionStatus.granted ||
          result[Permission.photos] == PermissionStatus.limited;
    }
    if (Platform.isAndroid) {
      final result = await [Permission.storage].request();
      return result[Permission.storage] == PermissionStatus.granted;
    }
    return false;
  }

  /// 相机权限
  /// [showExplanation] 是否在申请权限前显示说明对话框
  /// [purpose] 权限使用目的说明
  /// [context] 上下文，用于显示对话框
  static Future<bool> camera({
    bool showExplanation = false,
    String purpose = '',
    BuildContext? context,
  }) async {
    // 如果需要在申请权限前显示说明，且提供了上下文
    if (showExplanation && context != null && purpose.isNotEmpty) {
      final bool? shouldRequest = await _showPermissionExplanationDialog(
        context: context,
        permissionName: '相机权限',
        purpose: purpose,
      );

      // 用户取消，不申请权限
      if (shouldRequest != true) {
        return false;
      }
    }

    final result = await [Permission.camera].request();
    return result[Permission.camera] == PermissionStatus.granted;
  }

  /// 显示权限使用目的说明对话框
  /// 对话框不会自动消失，需要用户主动确认
  static Future<bool?> _showPermissionExplanationDialog({
    required BuildContext context,
    required String permissionName,
    required String purpose,
  }) async {
    return await showDialog<bool>(
      context: context,
      barrierDismissible: false, // 不允许点击外部关闭
      builder: (BuildContext dialogContext) {
        return WillPopScope(
          onWillPop: () async => false, // 不允许返回键关闭
          child: AlertDialog(
            insetPadding: EdgeInsets.symmetric(horizontal: 40.w),
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(12.w),
            ),
            title: Text(
              '权限申请说明',
              style: TextStyle(
                fontSize: 18.w,
                color: Colours.c3,
                fontWeight: Fonts.boldSemi,
              ),
              textAlign: TextAlign.center,
            ),
            content: Column(
              mainAxisSize: MainAxisSize.min,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  '权限名称：',
                  style: TextStyle(
                    fontSize: 15.w,
                    color: Colours.c3,
                    fontWeight: Fonts.medium,
                  ),
                ),
                SizedBox(height: 5.w),
                Text(
                  permissionName,
                  style: TextStyle(
                    fontSize: 15.w,
                    color: Colours.c6,
                  ),
                ),
                SizedBox(height: 15.w),
                Text(
                  '使用目的：',
                  style: TextStyle(
                    fontSize: 15.w,
                    color: Colours.c3,
                    fontWeight: Fonts.medium,
                  ),
                ),
                SizedBox(height: 5.w),
                Text(
                  purpose,
                  style: TextStyle(
                    fontSize: 15.w,
                    color: Colours.c6,
                    height: 1.5,
                  ),
                ),
              ],
            ),
            actions: <Widget>[
              Row(
                children: [
                  Expanded(
                    child: TextButton(
                      onPressed: () {
                        Navigator.of(dialogContext).pop(false);
                      },
                      child: Text(
                        '取消',
                        style: TextStyle(
                          fontSize: 16.w,
                          color: Colours.c6,
                          fontWeight: Fonts.medium,
                        ),
                      ),
                    ),
                  ),
                  Container(
                    width: 1,
                    height: 40.w,
                    color: Colours.cLine,
                  ),
                  Expanded(
                    child: TextButton(
                      onPressed: () {
                        Navigator.of(dialogContext).pop(true);
                      },
                      child: Text(
                        '确定',
                        style: TextStyle(
                          fontSize: 16.w,
                          color: Colours.cAB1941,
                          fontWeight: Fonts.boldSemi,
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            ],
          ),
        );
      },
    );
  }

  /// 打开设置
  static Future<void> setting() async => await openAppSettings();

  /// 存储权限
  static Future<bool> storage() async {
    final result = await [Permission.storage].request();
    return result[Permission.storage] == PermissionStatus.granted;
  }

  /// 获取麦克风权限
  static Future<bool> microphone() async {
    final result = await [Permission.microphone].request();
    return result[Permission.microphone] == PermissionStatus.granted;
  }
}
