import 'package:injectable/injectable.dart';
import 'package:keyboard/dialog/deprecate_diolog.dart';

import '../../base/base_controller.dart';
import '../../data/api/response/user_info_response.dart';
import '../../data/repository/account_repository.dart';
import 'package:get/get.dart';

import '../../dialog/common_alert_dialog_impl.dart';
import '../../utils/error_handler.dart';

@injectable
class UserInfoController extends BaseController {
  final AccountRepository accountRepository;

  Rxn<UserInfoResponse> get _userInfo => accountRepository.userInfo;

  UserInfoResponse? get userInfo => _userInfo.value;

  UserInfoController(this.accountRepository);

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

  void clickLogout() {
    logoutAccountDialog(
      confirmOnTap: () {
        accountRepository.logout();
        Get.back();
      },
    );
  }

  void clickDeprecate() {
    DeprecateDialog.show(
      btnConfirm: () async {
        try {
          await accountRepository.deprecateAccount();
          accountRepository.logout();
          Get.back();
        } catch (error) {
          ErrorHandler.toastError(error);
        }
      },
    );
  }

  void clickBack() {
    Get.back();
  }
}
