import 'package:get/get.dart';
import 'package:tone_snap/components/base_easyloading.dart';
import 'package:tone_snap/components/dialog/add_playlist_dialog.dart';
import 'package:tone_snap/components/dialog/remind_dialog.dart';
import 'package:tone_snap/data/models/playlist_model.dart';
import 'package:tone_snap/data/storage/playlists_box.dart';
import 'package:tone_snap/modules/sideb/custom_playlist/custom_playlist_controller.dart';
import 'package:tone_snap/modules/sideb/playlists/playlists_controller.dart';

class MorePlaylistBottomSheetController extends GetxController {
  late PlaylistModel playlistModel;

  void setPlaylistModel(PlaylistModel playlistModel) {
    this.playlistModel = playlistModel;
  }

  void onTapEdit() {
    Get.back();
    Get.dialog(
      barrierDismissible: false,
      CreatePlaylistDialog(
        name: playlistModel.title,
        onTap: (value) async {
          PlaylistsBox().editTitle(playlistModel.id, value);
          if (Get.isRegistered<CustomPlaylistController>()) {
            CustomPlaylistController.to.getPlaylistMode();
          }
          if (Get.isRegistered<PlaylistsController>()) {
            PlaylistsController.to.getList();
          }
        },
      ),
    );
  }

  void onTapRemove() {
    Get.back();
    Get.dialog(
      RemindDialog(
        content: 'Confirm to delete this playlist?',
        confirmOnTap: () async {
          await PlaylistsBox().delete(playlistModel.id);
          BaseEasyLoading.toast('Removed');
          Get.until((route) => route.isFirst);
          if (Get.isRegistered<PlaylistsController>()) {
            PlaylistsController.to.getList();
          }
        },
      ),
    );
  }
}
