import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tone_snap/controllers/player_controller.dart';
import 'package:tone_snap/data/models/voice_model.dart';
import 'package:tone_snap/data/storage/favorite_data.dart';
import 'package:tone_snap/generated/assets.dart';
import 'package:tone_snap/modules/voice/favourite/favourite_controller.dart';
import 'package:tone_snap/modules/voice/my_voice/my_voice_controller.dart';
import 'package:tone_snap/modules/voice/voice_home/voice_home_view.dart';
import 'package:tone_snap/modules/voice/voice_me/voice_me_view.dart';
import 'package:tone_snap/modules/voice/voice_settings/voice_settings_view.dart';
import 'package:tone_snap/routes/app_routes.dart';

class VoiceInitialController extends GetxController {
  static VoiceInitialController get to => Get.find<VoiceInitialController>();
  late PageController pageController;
  final pages = [
    PageItem([Assets.changeVoiceBnb1Selected, Assets.changeVoiceBnb1Unselected], const VoiceHomeView()),
    PageItem([Assets.changeVoiceBnb2Selected, Assets.changeVoiceBnb2Unselected], Container()),
    PageItem([Assets.changeVoiceBnb3Selected, Assets.changeVoiceBnb3Unselected], const VoiceMeView()),
    PageItem([Assets.changeVoiceBnb4Selected, Assets.changeVoiceBnb4Unselected], const VoiceSettingsView()),
  ];
  var currentIndex = 0.obs;
  Rx<VoiceModel?> currentPlayVoiceModel = Rx<VoiceModel?>(null);
  var playerController = PlayerController.to;

  /// 是否加入喜欢列表
  var isFavourite = false.obs;

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

  @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 {
      if (index == 2) _refreshMe();
      currentIndex.value = index;
      pageController.jumpToPage(index);
    }
  }

  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 = FavoriteData().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 FavoriteData().addData(currentPlayVoiceModel.value!.copyWith());
        isFavourite.value = true;
      }
    }
    _refreshMe();
  }

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

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

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