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/dialog/login/login_dialog.dart';
import 'package:keyboard/module/profile/edit/profile_edit_page.dart';
import 'package:keyboard/module/user_info/user_info_page.dart';
import 'package:keyboard/module/user_profile/user_profile_page.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/consts/error_code.dart';
import '../../data/repository/keyboard_repository.dart';
import '../../utils/error_handler.dart';
import '../../utils/http_handler.dart';
import '../../utils/toast_util.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;

  bool get isLogin => accountRepository.isLogin.value;

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

  KeyboardInfo get currentCustomKeyboardInfo =>
      _currentCustomKeyboardInfo.value;

  RxList<KeyboardInfo> get customKeyboardInfoList =>
      keyboardRepository.customKeyboardInfoList;

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

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

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

    getCustomKeyboard();
  }

  Future<void> getCustomKeyboard() async {
    keyboardRepository.refreshData();
    print('getCustomKeyboard: ${customKeyboardInfoList.length}');
    if (keyboardRepository.chooseKeyboardInfo.value == null) {
      return;
    }
    _currentCustomKeyboardInfo.value =
        keyboardRepository.chooseKeyboardInfo.value!;
  }

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

    _currentCustomKeyboardInfo.value = keyboardInfo;
  }

  clickBack() {
    Get.back(result: _currentCustomKeyboardInfo.value);
  }

  clickAddButton() async {
    AtmobLog.d(tag, "clickAddButton");
    var result = await ProfileEditPage.start();
    if (result == true) {
      await getCustomKeyboard();
    }
  }

  clickSaveButton() async {
    AtmobLog.d(tag, "clickSaveButton");
    final keyboardInfo = _currentCustomKeyboardInfo.value;
    // if (_currentCustomKeyboardInfo.value.isChoose == true) {
    //   ToastUtil.show("当前键盘已选择");
    //   clickBack();
    //   return;
    // }
    if (keyboardInfo.id?.isNotEmpty == true) {
      try {
        await keyboardRepository.keyboardChoose(keyboardId: keyboardInfo.id!);
        keyboardRepository.refreshData();
        ToastUtil.show("当前键盘已选择");
        clickBack();
      } catch (error) {
        if (error is ServerErrorException) {
          ErrorHandler.toastError(error);
          if (error.code == ErrorCode.noLoginError) {
            LoginDialog.show();
          }
        } else {
          AtmobLog.d(tag, " $error");
        }
      }
    }else {
      ToastUtil.show("请选择档案~");
    }
  }

  clickAvatar({required bool isUser, KeyboardInfo? keyboardInfo}) async {
    AtmobLog.d(tag, "clickAvatar");
    if (!isUser) {
      if (isLogin == false) {
        ToastUtil.show('请先登录~');
        LoginDialog.show();
        return;
      }

      var result = await ProfileEditPage.start(keyboardInfo: keyboardInfo);
      if (result == true) {
        await getCustomKeyboard();
      }
    } else {
      UserProfilePage.start();
    }
  }
}
