// Author: fengshengxiong
// Date: 2024/5/8
// Description: 自定义播放列表盒子

import 'package:get/get.dart';
import 'package:hive/hive.dart';
import 'package:tone_snap/components/base_easyloading.dart';
import 'package:tone_snap/data/models/music_model.dart';
import 'package:tone_snap/data/models/playlist_model.dart';
import 'package:tone_snap/data/storage/hive_storage.dart';
import 'package:tone_snap/utils/date_util.dart';

class PlaylistsBox {
  PlaylistsBox._();

  static final PlaylistsBox _instance = PlaylistsBox._();

  factory PlaylistsBox() {
    return _instance;
  }

  /// 声明盒子
  /// 注意, main函数中这个盒子已经打开, 可以进行存储操作
  final _box = Hive.box<PlaylistModel>(playlistsBox);

  /// 获取列表
  List<PlaylistModel> getList() {
    return _box.values.toList();
  }

  /// 获取倒序列表
  List<PlaylistModel> getReversedList() {
    return _box.values.toList().reversed.toList();
  }

  /// 添加数据
  Future<void> add(String title) async {
    final milliseconds = DateUtil.getNowTimestamp();
    final id = '$title-$milliseconds';
    await _box.put(id, PlaylistModel(
      id: id,
      title: title,
      milliseconds: milliseconds,
      musicList: <MusicModel>[],
    ));
  }

  /// 删除
  Future<void> delete(String id) async {
    var list = getList();
    final playlistModel = list.firstWhereOrNull((e) => e.id == id);
    if (playlistModel != null) {
      await _box.deleteAt(list.indexOf(playlistModel));
      await _box.flush();
    }
  }

  /// 清空所有数据
  Future<void> clear() async {
    await _box.clear();
    await _box.flush();
  }

  PlaylistModel? getPlaylistModel(String id) {
    return _box.get(id);
  }

  /// 编辑
  void editTitle(String id, String title) {
    var list = getList();
    final playlistModel = list.firstWhereOrNull((e) => e.id == id);
    if (playlistModel != null) {
      playlistModel.title = title;
      _box.put(id, playlistModel);
    }
  }

  /// 添加歌曲
  Future<bool> addMusic(String id, MusicModel musicModel) async {
    final playlistModel = _box.get(id);
    if (playlistModel != null) {
      playlistModel.musicList ??= <MusicModel>[];
      if (playlistModel.musicList!.firstWhereOrNull((e) => e.videoId == musicModel.videoId) != null) {
        BaseEasyLoading.toast('The current song is already in this playlist');
        return false;
      }
      playlistModel.musicList!.insert(0, musicModel);
      _box.put(id, playlistModel);
      return true;
    }
    return false;
  }

  /// 删除歌曲
  Future<bool> removeMusic(String id, String videoId) async {
    final playlistModel = _box.get(id);
    if (playlistModel != null && playlistModel.musicList != null) {
      if (playlistModel.musicList!.firstWhereOrNull((e) => e.videoId == videoId) != null) {
        playlistModel.musicList!.removeWhere((e) => e.videoId == videoId);
        _box.put(id, playlistModel);
        return true;
      }
    }
    return false;
  }

  /// 获取当前列表的封面
  String? getFirstCoverUrl(String id) {
    final playlistModel = _box.get(id);
    if (playlistModel != null) {
      if (playlistModel.musicList != null && playlistModel.musicList!.isNotEmpty) {
        return playlistModel.musicList!.first.coverUrl;
      }
    }
    return null;
  }
}
