import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tone_snap/data/storage/love_songs_box.dart';
import 'package:tone_snap/data/storage/offline_box.dart';
import 'package:tone_snap/modules/sideb/collect_playlists/collect_playlists_view.dart';
import 'package:tone_snap/modules/sideb/playlists/playlists_view.dart';
import 'package:tone_snap/routes/app_routes.dart';

class PersonalMusicLibraryController extends GetxController with GetSingleTickerProviderStateMixin {
  static PersonalMusicLibraryController get to => Get.find<PersonalMusicLibraryController>();
  var loveSongsCoverUrl = ''.obs;
  var loveSongsTotal = 0.obs;

  var artistsCoverUrl = ''.obs;
  var artistsTotal = 0.obs;

  var offlineCoverUrl = ''.obs;
  var offlineTotal = 0.obs;

  late TabController tabController;
  final labels = ['Playlists', 'Collect Playlists'];
  final pages = [const PlaylistsView(), const CollectPlaylistsView()];

  @override
  void onInit() {
    super.onInit();
    tabController = TabController(length: labels.length, vsync: this);
    refreshLoveSongs();
    refreshOffline();
  }

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

  void onTapModule(int index) {
    if (index == 0) {
      Get.toNamed(AppRoutes.loveSongs);
    }
    if (index == 1) {
      Get.toNamed(AppRoutes.artists);
    }
    if (index == 2) {
      Get.toNamed(AppRoutes.offline);
    }
  }

  void refreshLoveSongs() {
    loveSongsCoverUrl.value = LoveSongsBox().getFirstCoverUrl() ?? '';
    loveSongsTotal.value = LoveSongsBox().getList().length;
  }

  void refreshOffline() {
    offlineCoverUrl.value = OfflineBox().getFirstCoverUrl() ?? '';
    offlineTotal.value = OfflineBox().getList().length;
  }
}
