import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tone_snap/data/models/voice_model.dart';
import 'package:tone_snap/data/storage/favorite_box.dart';
import 'package:tone_snap/firebase/firebase_analytics_manager.dart';
import 'package:tone_snap/generated/assets.dart';
import 'package:tone_snap/global/app_tracking_transparency_manager.dart';
import 'package:tone_snap/modules/sidea/controllers/player_controller.dart';
import 'package:tone_snap/modules/sidea/favourite/favourite_controller.dart';
import 'package:tone_snap/modules/sidea/home/home_view.dart';
import 'package:tone_snap/modules/sidea/me/me_view.dart';
import 'package:tone_snap/modules/sidea/my_voice/my_voice_controller.dart';
import 'package:tone_snap/modules/sidea/settings/settings_view.dart';
import 'package:tone_snap/routes/app_routes.dart';

class InitialController extends GetxController {
  static InitialController get to => Get.find<InitialController>();
  var playerController = PlayerController.to;
  late PageController pageController;
  final pages = [
    PageItem([Assets.sideABnb1Selected, Assets.sideABnb1Unselected], const HomeView()),
    PageItem([Assets.sideABnb2Selected, Assets.sideABnb2Unselected], Container()),
    PageItem([Assets.sideABnb3Selected, Assets.sideABnb3Unselected], const MeView()),
    PageItem([Assets.sideABnb4Selected, Assets.sideABnb4Unselected], const SettingsView()),
  ];
  var currentIndex = 0.obs;
  Rx<VoiceModel?> currentPlayVoiceModel = Rx<VoiceModel?>(null);

  /// 是否收藏
  var isFavourite = false.obs;

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

  @override
  void onReady() async {
    super.onReady();
    _addEventLog();
  }

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

  Future<void> onBottomAppBarItemChanged(int index) async {
    if (index == 1) {
      await PlayerController.to.stopPlay();
      Get.toNamed(AppRoutes.uploadMethod);
    } else {
      currentIndex.value = index;
      pageController.jumpToPage(index);
      if (index == 0) {
        _addEventLog();
      }
      if (index == 2) _refreshMe();
    }
  }

  void onTapPlayBar() {
    Get.toNamed(AppRoutes.playSound, arguments: currentPlayVoiceModel.value);
  }

  Future<void> togglePlayback() async {
    playerController.isPlaying.value
        ? await playerController.pausePlay()
        : await playerController.startPlay();
  }

  VoiceModel? getIsFavouriteModel() {
    final list = FavoriteBox().getList();
    return list.firstWhereOrNull((e) => e.path == currentPlayVoiceModel.value?.path);
  }

  Future<void> onTapFavourite() async {
    if (isFavourite.value) {
      getIsFavouriteModel()?.delete();
      isFavourite.value = false;
    } else {
      if (currentPlayVoiceModel.value != null) {
        await FavoriteBox().addData(currentPlayVoiceModel.value!.copyWith());
        isFavourite.value = true;
      }
    }
    _refreshMe();
  }

  /// 刷新我的页面
  void _refreshMe() {
    if (Get.isRegistered<MyVoiceController>()) MyVoiceController.to.getData();
    if (Get.isRegistered<FavouriteController>()) FavouriteController.to.getData();
  }

  /// 埋点
  void _addEventLog() {
    FirebaseAnalyticsManager.logEvent(FirebaseAnalyticsManager.homeApv);
  }
}

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

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