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 {
    final granted = await Access.photos(
      showExplanation: true,
      purpose: purpose,
      context: context,
    );
    if (!granted) {
      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 granted = await Access.camera(
      showExplanation: true,
      purpose: purpose,
      context: context,
    );
    if (!granted) {
      return null;
    }

    try {
      return await _imagePicker.pickImage(
        source: source,
        // maxWidth: maxWidth,
        // maxHeight: maxHeight,
      );
    } catch (error) {
      Console.log('打开相机失败: $error');
      Toast.show('打开相机失败，请检查权限后重试');
      return null;
    }
  }
}
