part of utils;

abstract class AssetsPicker {
  static final ImagePicker _imagePicker = ImagePicker();

  /// 获取图库
  /// [purpose] 权限使用目的说明，用于在申请权限前告知用户
  static Future<XFile?> image({
    required BuildContext context,
    ImageSource source = ImageSource.gallery,
    double maxWidth = 1024,
    double maxHeight = 1024,
    String purpose = '用于从相册中选择图片，用于设置头像、上传图片等功能。',
  }) async {
    // 先检查权限状态
    bool hasPermission = false;
    if (Platform.isIOS) {
      final status = await Permission.photos.status;
      hasPermission = status == PermissionStatus.granted || status == PermissionStatus.limited;
    } else if (Platform.isAndroid) {
      final status = await Permission.storage.status;
      hasPermission = status == PermissionStatus.granted;
    }
    
    // 如果没有权限，先显示说明对话框，然后申请权限
    if (!hasPermission) {
      final granted = await Access.photos(
        showExplanation: true,
        purpose: purpose,
        context: context,
      );
      
      if (!granted) {
        // 用户拒绝或取消，检查是否需要跳转设置
        if (context.mounted) {
          final status = Platform.isIOS 
              ? await Permission.photos.status
              : await Permission.storage.status;
          
          if (status.isPermanentlyDenied) {
            _showSettingDialog(context, '获取相册权限');
          }
        }
        return null;
      }
    }
    
    return _imagePicker.pickImage(
      source: source,
      // maxWidth: maxWidth,
      // maxHeight: maxHeight,
      // imageQuality: 100
    );
  }
  
  /// 拍照
  /// [purpose] 权限使用目的说明，用于在申请权限前告知用户
  static Future<XFile?> camera({
    required BuildContext context,
    ImageSource source = ImageSource.camera,
    double maxWidth = 512,
    double maxHeight = 512,
    String purpose = '用于使用相机拍照，用于设置头像、上传图片等功能。',
  }) async {
    // 先检查权限状态
    final cameraStatus = await Permission.camera.status;
    bool hasPermission = cameraStatus == PermissionStatus.granted;
    
    // 如果没有相机权限，先显示说明对话框，然后申请权限
    if (!hasPermission) {
      final granted = await Access.camera(
        showExplanation: true,
        purpose: purpose,
        context: context,
      );
      
      if (!granted) {
        // 用户拒绝或取消，检查是否需要跳转设置
        if (context.mounted) {
          final status = await Permission.camera.status;
          
          if (status.isPermanentlyDenied) {
            _showSettingDialog(context, '获取相机权限');
          }
        }
        return null;
      }
    }
    
    return _imagePicker.pickImage(
      source: source,
      // maxWidth: maxWidth,
      // maxHeight: maxHeight,
    );
  }
  /// 展示设置弹窗
  static _showSettingDialog(BuildContext context,String content) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return WillPopScope(
          onWillPop: () => Future.value(true),
          child: AlertDialog(
            insetPadding: EdgeInsets.zero, // 设置水平边距
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(8.0.w),
            ),
            // 去除操作按钮区域的内边距
            actionsPadding: EdgeInsets.zero,
            title: Container(
              alignment: Alignment.center,
              child: Text('权限申请', style: TextStyle(
                  fontSize: 20.w,
                  color: Colours.c3,
                  fontWeight: Fonts.boldSemi),),
            ),
            actions: <Widget>[
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Column(
                    mainAxisAlignment: MainAxisAlignment.start,
                    children: [
                      Padding(
                        padding: EdgeInsets.only(top: 5.5.w),
                        child: Text(
                          content,
                          style: TextStyle(
                              fontSize: 15.w,
                              color: Colours.c9,
                              fontWeight: Fonts.medium),
                        ),
                      ),
                      SizedBox(
                        height: 22.w,
                      ),
                      Container(
                        height: 1, // 设置分割线的高度
                        width: 216.5.w, // 设置分割线的宽度
                        color: Colours.cLine, // 设置分割线的颜色
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                            Padding(padding: EdgeInsets.only(right: 28.w),
                              child:  GestureDetector(
                                child:
                                Container(
                                  width: 85.w,
                                  color: Colours.cFF,
                                  alignment: Alignment.center,
                                  child:  const Text(
                                    '取消',
                                    style: TextStyle(
                                        color: Colours.c6,
                                        fontSize: 15,
                                        fontWeight: Fonts.medium),
                                  ),
                                ),
                                onTap: () => Navigator.of(context).pop(),
                              ),),
                          Container(
                            height: 39.5.w, // 设置分割线的高度
                            width: 1, // 设置分割线的宽度
                            color: Colours.cLine, // 设置分割线的颜色
                          ),
                          Padding(padding: EdgeInsets.only(left: 28.w),
                            child:   GestureDetector(
                              child: Container(
                                width: 85.w,
                                color: Colours.cFF,
                                alignment: Alignment.center,
                                child:  const Text(
                                  '设置',
                                  style: TextStyle(
                                      color: Colours.cAB1941,
                                      fontSize: 15,
                                      fontWeight: Fonts.boldSemi),
                                ),
                              ),
                              onTap: () {
                                Access.setting();
                              },
                            ),
                          ),

                        ],
                      ),
                    ],
                  ),
                ],
              ),
            ],
          ),
        );
      },
    );
  }
}
