import 'dart:io';
import 'dart:typed_data';

import 'package:wechat_assets_picker/wechat_assets_picker.dart';

class AssetInfo {
  final String id;
  final int width;
  final int height;
  final int? duration;
  final int orientation;
  final int typeInt;
  final int? createDateSecond;
  final DateTime createDateTime;
  final DateTime modifiedDateTime;
  final String? title;
  final String? relativePath;
  final String? localPath;
  final String? filePath;
  final String? thumbFilePath;
  final String? originalPath;
  String? dateTitle;
  int? size;

  AssetInfo({
    required this.id,
    required this.width,
    required this.height,
    this.duration,
    required this.orientation,
    required this.typeInt,
    this.createDateSecond,
    required this.createDateTime,
    required this.modifiedDateTime,
    this.localPath,
    this.title,
    this.relativePath,
    this.filePath,
    this.thumbFilePath,
    this.originalPath,
    this.size,
    this.dateTitle,
  });

  // 从 AssetEntity 创建
  factory AssetInfo.fromAssetEntity(AssetEntity entity, String filePath,
      String thumbFilePath, String originalPath) {
    return AssetInfo(
      id: entity.id,
      width: entity.width,
      height: entity.height,
      duration: entity.duration,
      orientation: entity.orientation,
      typeInt: entity.typeInt,
      createDateSecond: entity.createDateSecond,
      createDateTime: entity.createDateTime,
      modifiedDateTime: entity.modifiedDateTime,
      title: entity.title,
      relativePath: entity.relativePath,
      filePath: filePath,
      thumbFilePath: thumbFilePath,
      originalPath: originalPath,
    );
  }

  // 转换为 JSON
  Map<String, dynamic> toJson() => {
        'id': id,
        'width': width,
        'height': height,
        'duration': duration,
        'orientation': orientation,
        'typeInt': typeInt,
        'createDateSecond': createDateSecond,
        'createDateTime': createDateTime.toIso8601String(),
        'modifiedDateTime': modifiedDateTime.toIso8601String(),
        'title': title,
        'relativePath': relativePath,
        'localPath': localPath,
        'file': filePath,
        'thumbFile': thumbFilePath,
        'originalPath': originalPath,
      };

  // 从 JSON 创建
  factory AssetInfo.fromJson(Map<String, dynamic> json) => AssetInfo(
        id: json['id'] as String,
        width: json['width'] as int,
        height: json['height'] as int,
        duration: json['duration'] as int,
        orientation: json['orientation'] as int,
        // isAll: json['isAll'] as bool,
        typeInt: json['typeInt'] as int,
        createDateSecond: json['createDateSecond'] as int?,
        createDateTime: DateTime.parse(json['createDateTime'] as String),
        modifiedDateTime: DateTime.parse(json['modifiedDateTime'] as String),
        title: json['title'] as String?,
        relativePath: json['relativePath'] as String?,
        localPath: json['localPath'] as String?,
        filePath: json['filePath'] as String?,
        thumbFilePath: json['thumbFilePath'] as String?,
        originalPath: json['originalPath'] as String?,
      );

  // MARK: - To Do
  // 转换为 AssetEntity
  Future<AssetEntity?> toAssetEntity() async {
    return await AssetEntity.fromId(id);
  }

  // 获取文件对象
  File get file => File(filePath!);

  // 获取缩略图数据
  Future<Uint8List?> get thumbnailData async {
    File thumbFile = File(thumbFilePath!);
    try {
      if (await thumbFile.exists()) {
        return await thumbFile.readAsBytes();
      }
      return null;
    } catch (e) {
      print('获取缩略图失败: $e');
      return null;
    }
  }
}
