import 'package:get/get.dart';

import '../../../../config/translations/strings_enum.dart';
import '../../../../utils/device_manager.dart';
import '../../../controllers/api_controller.dart';
import '../../../dialog/loading/loading_dialog.dart';
import '../../../routes/app_pages.dart';

class AccountController extends GetxController {
  final apiController = Get.find<ApiController>();

  // UID
  String uid = '';

  void toSubscription() {
    Get.toNamed(Routes.SUBSCRIPTION);
  }

  @override
  void onInit() {
    super.onInit();
    uid = DeviceManager.getCacheDeviceId().length > 12
        ? '${DeviceManager.getCacheDeviceId().substring(0, 6)}***${DeviceManager.getCacheDeviceId().substring(DeviceManager.getCacheDeviceId().length - 6)}'
        : DeviceManager.getCacheDeviceId();
  }

  // 处理退出登录
  Future<void> handleLogout() async {
    await LoadingDialog.show(
      context: Get.context!,
      loadingText: Strings.loggingOut.tr,
      successText: Strings.logoutSuccessful.tr,
      onRequest: () async {
        // 执行你的异步请求
        await apiController.logout();
      },
      onSuccess: () {
        // 成功后的操作
        Get.offAllNamed(Routes.HOME);
      },
    );
  }

  // 处理删除账户
  Future<void> handleDeleteAccount() async {
    await LoadingDialog.show(
      context: Get.context!,
      loadingText: Strings.deletingAccount.tr,
      successText: Strings.deleteAccountSuccessful.tr,
      onRequest: () async {
        // 执行你的异步请求
        await apiController.deleteAccount();
      },
      onSuccess: () {
        // 成功后的操作
        Get.offAllNamed(Routes.HOME);
      },
    );
  }
}
