import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:injectable/injectable.dart';
import 'package:keyboard/data/repository/account_repository.dart';
import 'package:keyboard/data/repository/characters_repository.dart';
import 'package:keyboard/data/repository/config_repository.dart';
import 'package:keyboard/data/repository/keyboard_repository.dart';
import 'package:keyboard/data/repository/store_repository.dart';
import 'package:keyboard/module/main/enums/main_tab.dart';
import 'package:keyboard/module/new_user/new_user_page.dart';
import 'package:keyboard/module/store/new_discount/new_discount_page.dart';
import 'package:lottie/lottie.dart';

import '../../base/base_controller.dart';
import '../../data/bean/member_info.dart';
import '../../resource/assets.gen.dart';
import '../../resource/string.gen.dart';

import '../../router/app_page_arguments.dart';
import '../../utils/atmob_log.dart';
import '../../utils/keyboard_tutorial_util.dart';
import '../character/character_view.dart';
import '../keyboard/keyboard_view.dart';
import '../mine/mine_view.dart';

@injectable
class MainController extends BaseController {
  final String _tag = "MainController";

  final _currentIndex = 0.obs;

  int get currentIndex => _currentIndex.value;

  final AccountRepository accountRepository;
  final KeyboardRepository keyboardRepository;
  final ConfigRepository configRepository;
  final StoreRepository storeRepository;
  final CharactersRepository charactersRepository;

  bool get isLogin => accountRepository.isLogin.value;

  MemberInfo? get memberStatusInfo => accountRepository.memberStatusInfo.value;


  MainController(
    this.accountRepository,
    this.keyboardRepository,
    this.configRepository,
    this.storeRepository,
    this.charactersRepository,
  );

  final List<TabBean> tabBeans = [
    TabBean(
      StringName.mainTabKeyboard,
      Assets.images.iconTabKeyboardUnselect.path,
      Assets.anim.animTabKeyboardSelectedData,
      AnimatedSwitcher(
        duration: Duration(milliseconds: 300),
        transitionBuilder: (Widget child, Animation<double> animation) {
          return FadeTransition(opacity: animation, child: child);
        },
        child: KeyBoardView(),
      ),
    ),
    TabBean(
      StringName.mainTabCharacter,
      Assets.images.iconTabCharacterUnselect.path,
      Assets.anim.animTabCharacterSelectedData,

      AnimatedSwitcher(
        duration: Duration(milliseconds: 300),

        transitionBuilder: (Widget child, Animation<double> animation) {
          return FadeTransition(opacity: animation, child: child);
        },
        child: CharacterView(),
      ),
    ),
    TabBean(
      StringName.mainTabMine,
      Assets.images.iconTabMineUnselect.path,
      Assets.anim.animTabMineSelectedData,

      AnimatedSwitcher(
        duration: Duration(milliseconds: 300),
        transitionBuilder: (Widget child, Animation<double> animation) {
          return FadeTransition(opacity: animation, child: child);
        },

        child: MineView(),
      ),
    ),
  ];

  void changeIndex(int index) {
    if (_currentIndex.value != index) {
      _currentIndex.value = index;
    }
  }

  @override
  void onInit() {
    super.onInit();
    AssetLottie(Assets.anim.animSurpriseDialogData).load();


  }

  @override
  Future<void> onReady() async {
    super.onReady();
    if (memberStatusInfo != null && memberStatusInfo!.isMember&&isLogin) {
      return;
    }
   await NewDiscountPage.start();
    // 第一次显示首页，显示键盘引导页
    KeyboardTutorialUtil.firstMainPageShowTutorial();




  }

  /// 处理跳转参数
  void handleJumpParameters() {
    final parameters = Get.parameters as Map<String, dynamic>?;

    // 当前索引
    MainTab tab = MainTab.keyBoard;
    if (parameters?[AppPageArguments.tabName] == null) {
      AtmobLog.i(_tag, '没有传递 tabName 参数');
    } else {
      final String? tabName = parameters?[AppPageArguments.tabName] as String?;
      if (tabName != null) {
        tab = MainTab.fromTabName(tabName) ?? MainTab.keyBoard;
        AtmobLog.i(_tag, "tabName: $tabName");
      }
    }

    // 根据参数，切换Tab
    WidgetsBinding.instance.addPostFrameCallback((_) {
      changeIndex(tab.tabIndex);
    });
  }
}

class TabBean {
  final String title;
  final String normalIcon;
  final String selectedIcon;
  final Widget page;

  const TabBean(this.title, this.normalIcon, this.selectedIcon, this.page);
}
