import 'package:get/get_rx/src/rx_types/rx_types.dart';
import 'package:injectable/injectable.dart';
import 'package:keyboard/base/base_controller.dart';
import 'package:keyboard/data/repository/account_repository.dart';
import 'package:keyboard/utils/atmob_log.dart';
import 'package:get/get.dart';
import '../../data/api/response/user_info_response.dart';
import '../../data/bean/keyboard_info.dart';
import '../../data/repository/keyboard_repository.dart';
import '../keyboard_manage/keyboard_manage_controller.dart';

@injectable
class ProfileController extends BaseController {
  final tag = "ProfileController";
  final KeyboardRepository keyboardRepository;

  final AccountRepository accountRepository;

  UserInfoResponse? get userInfo => accountRepository.userInfo.value;

  int get userGender => userInfo?.gender ?? 0;

  final Rx<KeyboardInfo> _currentCustomKeyboardInfo = KeyboardInfo().obs;

  KeyboardInfo get currentCustomKeyboardInfo =>
      _currentCustomKeyboardInfo.value;




  final RxList<KeyboardInfo> _customKeyboardInfoList = RxList();
  RxList<KeyboardInfo> get keyboardInfoList =>  _customKeyboardInfoList;
  // 当前自定义键盘亲密度
  final RxInt _currentCustomIntimacy = 0.obs;

  int get currentCustomIntimacy => _currentCustomIntimacy.value;

  // 当前定制亲密度是否有变化
  final RxBool _customIntimacyChanged = false.obs;

  bool get customIntimacyChanged => _customIntimacyChanged.value;

  ProfileController(this.keyboardRepository, this.accountRepository) {
    AtmobLog.d(tag, '....init');
  }

  final RxBool isTest = false.obs;

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

  //   获取定制键盘
  void getCustomKeyboard() {
    AtmobLog.i(tag, 'getCustomKeyboard');
    keyboardRepository.getKeyboardList(type: KeyboardType.custom.name).then((
      keyboardListResponse,
    ) {
      AtmobLog.i(
        tag,
        'keyboardListResponse: ${keyboardListResponse.keyboardInfos}',
      );
      _customKeyboardInfoList.value = keyboardListResponse.keyboardInfos;

      //检查是否是选择的键盘，如果没有选择的键盘，默认选择第一个
      if (_customKeyboardInfoList.isNotEmpty) {
        _currentCustomKeyboardInfo.value = _customKeyboardInfoList.firstWhere(
          (element) => element.isChoose == true,
          orElse: () => _customKeyboardInfoList.first,
        );
        _currentCustomIntimacy.value =
            _currentCustomKeyboardInfo.value.intimacy ?? 0;
        _currentCustomIntimacy.listen((intimacy) {
          _customIntimacyChanged.value =
              _currentCustomKeyboardInfo.value.intimacy != intimacy;
          AtmobLog.d(tag, 'intimacyChanged: $_customIntimacyChanged');
        });
      }
    });
  }

  clickOnChangeKeyboard(KeyboardInfo keyboardInfo) {
    AtmobLog.d(tag, 'clickOnChangeKeyboard: ${keyboardInfo.toJson()}');

    _currentCustomKeyboardInfo.value = keyboardInfo;
  }

  clickBack() {
    Get.back();
  }

  clickAddButton() {
    AtmobLog.d(tag, "clickAddButton");
  }
}
