import 'dart:io';

import 'package:photo_manager/photo_manager.dart';

class AudioPickerHandler {
  AudioPickerHandler._();

  //申请权限
  static Future<bool> requestPermissionExtend() async {
    final PermissionState ps = await PhotoManager.requestPermissionExtend();
    return ps.hasAccess;
  }

  //是否有权限
  static Future<bool> hasPermission() async {
    final PermissionState ps = await PhotoManager.getPermissionState(
        requestOption: const PermissionRequestOption());
    return ps.hasAccess;
  }

  //获取AssetPathEntity
  static Future<List<AssetPathEntity>> getAssetPathList() async {
    final PMFilter filter = FilterOptionGroup(
      audioOption: const FilterOption(
        sizeConstraint: SizeConstraint(ignoreSize: true),
      ),
    );
    final List<AssetPathEntity> paths = await PhotoManager.getAssetPathList(
      onlyAll: true,
      type: RequestType.audio,
      filterOption: filter,
    );
    return paths;
  }

  //获取本地音频资源列表
  static Future<List<AssetEntity>> getAssetList(AssetPathEntity path, int page,
      {int size = 100}) async {
    final List<AssetEntity> entities = await path.getAssetListPaged(
      page: page,
      size: size,
    );
    return entities;
  }

  //根据id获取AssetEntity
  static Future<AssetEntity?> getAssetEntity(String? id) async {
    if (id == null) {
      return null;
    }
    return await AssetEntity.fromId(id);
  }

  //根据id获取文件
  static Future<File?> getAssetFile(String? id) async {
    if (id == null) {
      return null;
    }
    AssetEntity? assetEntity = await AssetEntity.fromId(id);
    return await assetEntity?.file;
  }
}
