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';

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

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

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

  void onTapLove() async {
    if (musicModel.value.videoId == null) return;
    if (isLove.value) {
      await LoveSongsBox().delete(musicModel.value.videoId!);
      BaseEasyLoading.toast('Removed');
    } else {
      await LoveSongsBox().add(musicModel.value.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.value.videoId) || musicModel.value.taskStatus == TaskStatus.complete) {
      Get.dialog(
        RemindDialog(
          content: 'Confirm to remove this song?',
          confirmOnTap: () async {
            Get.back();
            await OfflineBox().delete(musicModel.value.videoId!);
            musicModel.update((fn) => fn?.taskStatus = null);
            BaseEasyLoading.toast('Removed');
            if (Get.isRegistered<PersonalMusicLibraryController>()) {
              PersonalMusicLibraryController.to.refreshOffline();
            }
            if (Get.isRegistered<OfflineController>()) {
              OfflineController.to.getOfflineList();
            }
          },
        ),
      );
    } else {
      if (musicModel.value.taskStatus == TaskStatus.enqueued
          || musicModel.value.taskStatus == TaskStatus.running) {
        DownloadManager().cancelDownload(musicModel);
      } else {
        DownloadManager().downloadMusic(musicModel);
      }
    }
  }

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

  Future<void> onTapRemove() async {
    if (playlistModelId != null && musicModel.value.videoId != null) {
      bool result = await PlaylistsBox().removeMusic(playlistModelId!, musicModel.value.videoId!);
      if (result) {
        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');
  }
}
