import 'package:flutter/material.dart';
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/data/models/music_model.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/playlists/playlists_controller.dart';

class AddToPlaylistBottomSheetController extends GetxController {
  late MusicModel musicModel;

  var playlists = <PlaylistModel>[].obs;

  @override
  void onReady() {
    super.onReady();
    _getList();
  }

  void _getList() {
    playlists.value = PlaylistsBox().getReversedList();
  }

  void setMusicModel(MusicModel musicModel) {
    this.musicModel = musicModel;
  }

  void createPlayList() {
    Get.dialog(
      barrierDismissible: false,
      CreatePlaylistDialog(
        onTap: (value) async {
          await PlaylistsBox().add(value);
          _getList();
          if (Get.isRegistered<PlaylistsController>()) {
            PlaylistsController.to.getList();
          }
        },
      ),
    );
  }

  void onTapItem(PlaylistModel playlistModel) async {
    bool result = await PlaylistsBox().addMusic(playlistModel.id, musicModel);
    if (result) {
      BaseEasyLoading.toast('Added to playlist');
      Get.back();
      if (Get.isRegistered<PlaylistsController>()) {
        PlaylistsController.to.getList();
      }
    }
  }
}
