import 'package:electronic_assistant/base/base_controller.dart';
import 'package:electronic_assistant/data/consts/event_report_id.dart';
import 'package:electronic_assistant/data/repositories/config_repository.dart';
import 'package:electronic_assistant/handler/event_handler.dart';
import 'package:electronic_assistant/resource/colors.gen.dart';
import 'package:electronic_assistant/router/app_pages.dart';
import 'package:electronic_assistant/utils/error_handler.dart';
import 'package:electronic_assistant/utils/toast_util.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../../../data/api/response/user_info_response.dart';
import '../../../data/repositories/account_repository.dart';
import '../../../dialog/alert_dialog.dart';
import '../../../dialog/show_update_version_dialog.dart';
import '../../../resource/string.gen.dart';
import '../../../utils/app_info_util.dart';
import '../controller.dart';

class MainDrawerController extends BaseController {
  String get versionName => "v${appInfoUtil.appVersionName}";

  String get loginTxt =>
      accountRepository.getUserSubName(accountRepository.phone);

  final Rxn<UserInfoResponse> _userInfo = accountRepository.userInfo;

  UserInfoResponse? get userInfo => _userInfo.value;

  void accountLogout() {
    EAAlertDialog.show(
        contentWidget: Text(
          StringName.accountLogoutTips.tr,
          style: TextStyle(
              fontSize: 15.sp,
              color: ColorName.primaryTextColor,
              fontWeight: FontWeight.bold),
        ),
        cancelText: StringName.cancel.tr,
        confirmText: StringName.sure.tr,
        confirmOnTap: () {
          accountRepository.userDeprecate().then((data) {
            Get.find<MainController>().closeDrawer();
            accountRepository.logout();
            ToastUtil.showToast(StringName.accountLogoutSuccess.tr);
          }).catchError((error) {
            ErrorHandler.toastError(error);
          });
        });
  }

  void onCheckUpdate() {
    EventHandler.report(EventId.event_105001);
    configRepository.getAppVersion().then((bean) {
      if (bean == null) {
        ToastUtil.showToast(StringName.versionLatest.tr);
      } else {
        Get.find<MainController>().closeDrawer();
        UpdateVersionDialog.show(bean);
      }
    }).catchError((error) {
      ErrorHandler.toastError(error);
    });
  }

  void modelClick() {
    Get.toNamed(RoutePath.modelExplain);
  }
}
