import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tone_snap/generated/assets.dart';
import 'package:tone_snap/global/app_tracking_transparency_manager.dart';
import 'package:tone_snap/modules/sideb/home/home_view.dart';
import 'package:tone_snap/modules/sideb/personal_music_library/personal_music_library_controller.dart';
import 'package:tone_snap/modules/sideb/personal_music_library/personal_music_library_view.dart';
import 'package:tone_snap/modules/sideb/search_music/search_music_view.dart';

class InitialController extends GetxController {
  static InitialController get to => Get.find<InitialController>();
  late PageController pageController;
  final pages = [
    PageItem([Assets.sideBBnb1Unselected, Assets.sideBBnb1Selected], const HomeView()),
    PageItem([Assets.sideBBnb2Unselected, Assets.sideBBnb2Selected], const SearchMusicView()),
    PageItem([Assets.sideBBnb3Unselected, Assets.sideBBnb3Selected], const PersonalMusicLibraryView()),
  ];
  var currentIndex = 0.obs;

  @override
  void onInit() {
    super.onInit();
    AppTrackingTransparencyManager().requestATT();
    pageController = PageController(initialPage: currentIndex.value);
  }

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

  Future<void> onBottomAppBarItemChanged(int index) async {
    currentIndex.value = index;
    pageController.jumpToPage(index);

    if (index == 2) {
      if (Get.isRegistered<PersonalMusicLibraryController>()) {
        PersonalMusicLibraryController.to.refreshLoveSongs();
        PersonalMusicLibraryController.to.refreshOffline();
      }
    }
  }
}

class PageItem {
  late final List<String> icons;
  late final StatelessWidget widget;

  PageItem(this.icons, this.widget);
}

