// Author: fengshengxiong
// Date: 2024/6/24
// Description: 音乐个体模型

import 'dart:convert';

import 'package:background_downloader/background_downloader.dart';
import 'package:dio/dio.dart';
import 'package:hive/hive.dart';

part 'music_model.g.dart';

@HiveType(typeId: 2)
class MusicModel extends HiveObject {
  @HiveField(0)
  String? videoId;
  @HiveField(1)
  String? title;
  @HiveField(2)
  String? subtitle;
  @HiveField(3)
  String? coverUrl;
  @HiveField(4)
  String? url;
  @HiveField(5)
  String? localPath;
  @HiveField(6)
  String? musicType;
  @HiveField(7)
  String? playlistId;
  @HiveField(8)
  String? browseId;
  @HiveField(9)
  String? params;

  /// 收藏状态
  bool isLove = false;

  /// 下载进度、任务状态
  double progress;
  TaskStatus? taskStatus;
  CancelToken? cancelToken;

  MusicModel({
    this.videoId,
    this.title,
    this.subtitle,
    this.coverUrl,
    this.url,
    this.localPath,
    this.musicType,
    this.playlistId,
    this.browseId,
    this.params,
    this.isLove = false,
    this.progress = 0.0,
    this.taskStatus,
    this.cancelToken,
  });

  MusicModel copyWith({
    String? videoId,
    String? title,
    String? subtitle,
    String? coverUrl,
    String? url,
    String? localPath,
    String? musicType,
    String? playlistId,
    String? browseId,
    String? params,
    bool? isLove,
    double? progress,
    TaskStatus? taskStatus,
    CancelToken? cancelToken,
  }) =>
      MusicModel(
        videoId: videoId ?? this.videoId,
        title: title ?? this.title,
        subtitle: subtitle ?? this.subtitle,
        coverUrl: coverUrl ?? this.coverUrl,
        url: url ?? this.url,
        localPath: localPath ?? this.localPath,
        musicType: musicType ?? this.musicType,
        playlistId: playlistId ?? this.playlistId,
        browseId: browseId ?? this.browseId,
        params: params ?? this.params,
        isLove: isLove ?? this.isLove,
        progress: progress ?? this.progress,
        taskStatus: taskStatus ?? this.taskStatus,
        cancelToken: cancelToken ?? this.cancelToken,
      );

  factory MusicModel.fromJson(String str) =>
      MusicModel.fromMap(json.decode(str));

  String toJson() => json.encode(toMap());

  factory MusicModel.fromMap(Map<String, dynamic> json) => MusicModel(
        videoId: json["videoId"],
        title: json["title"],
        subtitle: json["subtitle"],
        coverUrl: json["coverUrl"],
        url: json["url"],
        localPath: json["localPath"],
        musicType: json["musicType"],
        playlistId: json["playlistId"],
        browseId: json["browseId"],
        params: json["params"],
        isLove: json["isLove"] ?? false,
        progress: json["progress"] ?? 0.0,
        taskStatus: json["taskStatus"],
        cancelToken: json["cancelToken"],
      );

  Map<String, dynamic> toMap() => {
        "videoId": videoId,
        "title": title,
        "subTitle": subtitle,
        "coverUrl": coverUrl,
        "url": url,
        "localPath": localPath,
        "musicType": musicType,
        "playlistId": playlistId,
        "browseId": browseId,
        "params": params,
        "isLove": isLove,
        "progress": progress,
        "taskStatus": taskStatus,
        "cancelToken": cancelToken,
      };
}
