import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:injectable/injectable.dart';
import 'package:keyboard/base/base_controller.dart';
import 'package:keyboard/data/bean/keyboard_info.dart';
import 'package:keyboard/data/repository/config_repository.dart';
import 'package:keyboard/module/keyboard_manage/keyboard_manage_page.dart';
import 'package:keyboard/utils/atmob_log.dart';

import '../../data/bean/character_group_info.dart';
import '../../data/repository/characters_repository.dart';
import '../../data/repository/keyboard_repository.dart';
import '../character_custom/character_custom_page.dart';

@injectable
class CharacterController extends BaseController
    with GetTickerProviderStateMixin {
  final String tag = "CharacterController";
  final CharactersRepository charactersRepository;
  final ConfigRepository configRepository;
  final KeyboardRepository keyboardRepository;

  CharacterController(
    this.charactersRepository,
    this.configRepository,
    this.keyboardRepository,
  );

  // 人设主题
  List<CharacterGroupInfo> get characterGroupList =>
      charactersRepository.characterGroupList;

  // 键盘列表
  List<KeyboardInfo> get keyboardInfoList =>
      keyboardRepository.keyboardInfoList;

  late Rx<TabController> tabController;
  late PageController pageController;

  RxInt currentTabBarIndex = 0.obs;

  Rx<CharacterGroupInfo> currentCharacterGroupInfo = CharacterGroupInfo().obs;

  Rx<KeyboardInfo> currentKeyboardInfo = KeyboardInfo().obs;

  @override
  void onInit() {
    super.onInit();

    if (characterGroupList.isNotEmpty) {
      currentCharacterGroupInfo.value = characterGroupList.first;
    }
    if (keyboardInfoList.isNotEmpty) {
      currentKeyboardInfo.value = keyboardInfoList.first;
    }

    _dataLoad();
  }

  void _dataLoad() async {
    pageController = PageController();

    tabController =
        TabController(
          length: characterGroupList.length,
          vsync: this,
          initialIndex: 0,
        ).obs;

    ever(charactersRepository.characterGroupList, (value) {
      AtmobLog.d(tag, "characterGroupList changed");
      if (value.isNotEmpty) {
        tabController.value.dispose();
        tabController.value = TabController(
          length: characterGroupList.length,
          vsync: this,
          initialIndex: 0,
        );
        currentCharacterGroupInfo.value = characterGroupList.first;
        AtmobLog.d(
          tag,
          "currentCharacterGroupInfo.value: ${characterGroupList.first.id}",
        );
      }
    });

    ever(keyboardRepository.keyboardInfoList, (value) {
      AtmobLog.d(tag, "keyboardInfoList1 changed");
      if (value.isNotEmpty) {
        currentKeyboardInfo.value = keyboardInfoList.first;
        print("currentKeyboardInfo.value: ${currentKeyboardInfo.value}");
      }
    });
  }

  void onTabChanged(int index) {
    if (index >= characterGroupList.length) {
      return;
    }

    pageController.animateToPage(
      index,
      duration: const Duration(milliseconds: 300),
      curve: Curves.easeInToLinear,
    );
  }

  void onPageChanged(int index) {
    if (index >= characterGroupList.length) {
      return;
    }
    currentTabBarIndex.value = index;
    currentCharacterGroupInfo.value = characterGroupList[index];
    tabController.value.animateTo(
      index,
      duration: const Duration(milliseconds: 300),
    );
  }

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

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

  void clickMyKeyboard() {
    AtmobLog.d(tag, "clickMyKeyboard");
    KeyboardManagePage.start();
  }

  void clickCustomCharacter() {
    AtmobLog.d(tag, "clickCustomCharacter");
    CharacterCustomPage.start();
  }

  // 切换键盘
  void switchKeyboard(String? newValue) {
    currentKeyboardInfo.value = keyboardInfoList.firstWhere(
      (element) => element.name == newValue,
      orElse: () => keyboardInfoList.first,
    );
  }
}
