import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tone_snap/components/dialog/add_playlist_dialog.dart';
import 'package:tone_snap/components/view_state_widget.dart';
import 'package:tone_snap/data/models/playlist_model.dart';
import 'package:tone_snap/data/storage/playlists_box.dart';

class PlaylistsController extends GetxController {
  static PlaylistsController get to => Get.find<PlaylistsController>();
  var viewState = ViewState.loading.obs;
  var scrollController = ScrollController();
  var playLists = <PlaylistModel>[].obs;

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

  @override
  void onClose() {
    scrollController.dispose();
    super.onClose();
  }

  void getList() {
    playLists.value = PlaylistsBox().getReversedList();
    viewState.value = playLists.isNotEmpty ? ViewState.normal : ViewState.empty;
  }

  void createPlayList() {
    Get.dialog(
      barrierDismissible: false,
      CreatePlaylistDialog(
        onTap: (value) async {
          await PlaylistsBox().add(value);
          getList();
        },
      ),
    );
  }
}