import 'package:background_downloader/background_downloader.dart';
import 'package:get/get.dart';
import 'package:tone_snap/components/base_easyloading.dart';
import 'package:tone_snap/components/dialog/remind_dialog.dart';
import 'package:tone_snap/data/models/music_model.dart';
import 'package:tone_snap/data/storage/love_songs_box.dart';
import 'package:tone_snap/data/storage/offline_box.dart';
import 'package:tone_snap/data/storage/playlists_box.dart';
import 'package:tone_snap/global/download_manager.dart';
import 'package:tone_snap/modules/sideb/add_to_playlist_bottom_sheet/add_to_playlist_bottom_sheet_view.dart';
import 'package:tone_snap/modules/sideb/custom_playlist/custom_playlist_controller.dart';
import 'package:tone_snap/modules/sideb/love_songs/love_songs_controller.dart';
import 'package:tone_snap/modules/sideb/offline/offline_controller.dart';
import 'package:tone_snap/modules/sideb/personal_music_library/personal_music_library_controller.dart';
import 'package:tone_snap/modules/sideb/playlists/playlists_controller.dart';
import 'package:tone_snap/utils/obj_util.dart';

class MoreBottomSheetController extends GetxController {
  late MusicModel musicModel;
  String? playlistModelId;
  var isLove = false.obs;

  void setMusicModel(MusicModel musicModel, {String? playlistModelId}) {
    this.musicModel = musicModel;
    this.playlistModelId = playlistModelId;
    _checkIsLove();
  }

  void _checkIsLove() {
    isLove.value = LoveSongsBox().checkLove(musicModel.videoId);
  }

  void onTapLove() async {
    if (ObjUtil.isEmpty(musicModel.videoId)) return;
    if (isLove.value) {
      await LoveSongsBox().delete(musicModel.videoId!);
      BaseEasyLoading.toast('Removed');
    } else {
      await LoveSongsBox().add(musicModel.copyWith());
      BaseEasyLoading.toast('Collected');
    }
    _checkIsLove();
    if (Get.isRegistered<PersonalMusicLibraryController>()) {
      PersonalMusicLibraryController.to.refreshLoveSongs();
    }
    if (Get.isRegistered<LoveSongsController>()) {
      LoveSongsController.to.getLoveList();
    }
  }

  void onTapDownload() {
    if (OfflineBox().checkDownloaded(musicModel.videoId)) {
      Get.dialog(
        RemindDialog(
          content: 'Confirm to remove this song?',
          confirmOnTap: () async {
            Get.back();
            await OfflineBox().delete(musicModel.videoId!);
            DownloadManager.to.updateDownloadState();
            BaseEasyLoading.toast('Removed');
            if (Get.isRegistered<PersonalMusicLibraryController>()) {
              PersonalMusicLibraryController.to.refreshOffline();
            }
            if (Get.isRegistered<OfflineController>()) {
              OfflineController.to.getOfflineList();
            }
          },
        ),
      );
    } else {
      if (DownloadManager.to.getMusicModel(musicModel.videoId)?.taskStatus == TaskStatus.enqueued
          || DownloadManager.to.getMusicModel(musicModel.videoId)?.taskStatus == TaskStatus.running) {
        DownloadManager.to.cancelDownload(musicModel.videoId);
      } else {
        DownloadManager.to.downloadMusic(musicModel);
      }
    }
  }

  void onTapAddToPlaylist() {
    Get.back();
    Get.bottomSheet(
      AddToPlaylistBottomSheetView(
        musicModel: musicModel,
      ),
    );
  }

  Future<void> onTapRemove() async {
    if (ObjUtil.isNotEmpty(playlistModelId) && ObjUtil.isNotEmpty(musicModel.videoId)) {
      await PlaylistsBox().removeMusic(playlistModelId!, musicModel.videoId!);
      BaseEasyLoading.toast('Removed');
      Get.back();
      if (Get.isRegistered<PlaylistsController>()) {
        PlaylistsController.to.getList();
      }
      if (Get.isRegistered<CustomPlaylistController>()) {
        CustomPlaylistController.to.getPlaylistMode();
      }
    }
  }

  void onTapReport() {
    Get.back();
    BaseEasyLoading.toast('We have received your feedback and will deal with it as soon as possible');
  }
}
