import 'dart:ui';
import 'package:fis_common/index.dart';
import 'package:fis_common/logger/logger.dart';
import 'package:fis_i18n/i18n.dart';
import 'package:fis_jsonrpc/rpc.dart';
import 'package:fis_ui/index.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/architecture/services/modules/config_language.dart';
import 'package:flyinsono/architecture/states/states.dart';
import 'package:flyinsono/rpc.dart';
import 'package:get/get.dart';
import 'interfaces/language.dart';
import 'interfaces/user_info.dart';
import 'base_manager.dart';

class LanguageConfigManager extends BaseManager
    implements ILanguageConfigManager {
  /// 本地缓存语言包
  final languageService = Get.find<LanguageService>();

  /// 检查翻译
  /// [code] 翻译key
  @override
  String getExamLanguageValue(String code) {
    try {
      var value = languageService.findLanguageValueByCode('exam', code);
      return value;
    } catch (e) {
      print(e);
    }
    return code;
  }

  /// 诊断相关翻译
  /// [code] 翻译key
  @override
  String getConsultationLanguageValue(String code) {
    try {
      var value = languageService.findLanguageValueByCode('consultation', code);
      return value;
    } catch (e) {
      print(e);
    }
    return code;
  }

  /// 器官部位相关翻译
  /// [code] 翻译key
  @override
  String getApplicationLanguageValue(String code) {
    try {
      var value = languageService.findLanguageValueByCode('application', code);
      return value;
    } catch (e) {
      print(e);
    }
    return code;
  }

  @override
  Future<bool> swithLanguageByCode(String languageCode) async {
    Locale locale;
    bool result = false;
    switch (languageCode) {
      case 'zh':
        locale = CHINESE_LOCALE;
        break;
      case 'en':
        locale = ENGLISH_LOCALE;
        break;
      case 'ru':
        locale = RUSSIAN_LOCALE;
        break;
      case 'es':
        locale = SPANISH_LOCALE;
        break;
      case 'ro':
        locale = ROMANIA_LOCALE;
        break;
      default:
        locale = CHINESE_LOCALE;
        break;
    }
    _setCommonSettingsAsync(CommonSettingsEnum.Language, languageCode);
    if (FPlatform.isMacOS || FPlatform.isWindows) {
      FConfirmAlert.show(
        context: Get.context!,
        title: i18nBook.common.tip.t,
        subTitle: i18nBook.homePage.swtichLanguageConfirm.t,
        businessParent: FVirtualPageWidget(languageCode),
        confrimLabel: i18nBook.common.confirm.t,
        cancelLabel: i18nBook.common.cancel.t,
        onConfirm: () {
          _updateLanguage(locale);
          result = true;
        },
        onCancel: () {
          result = false;
        },
      );
    } else {
      _updateLanguage(locale);
    }
    return result;
  }

  /// 设置 语言 或者报告标签是否弹出
  Future<void> _setCommonSettingsAsync(
    CommonSettingsEnum currCommonSetting,
    String signatureValue,
  ) async {
    try {
      if (Store.user.token.isNullOrEmpty) {
        return;
      }
      final result = await rpc.user.setCommonSettingsAsync(
        CommonSettingsRequest(
          token: Store.user.token,
          value: signatureValue,
          settingType: currCommonSetting,
        ),
      );
      if (result) {
        var manager = Get.find<IUserInfoManager>();
        await manager.getUserInfoAsync();
      }
    } catch (e) {
      printError(info: e.toString());
      logger.e("setCommonSettingsAsync ex:", e);
    }
  }

  /// 更新语言方法
  Future<void> _updateLanguage(Locale locale) async {
    Store.app.setBusy(i18nBook.auth.switching.t);
    try {
      if (FPlatform.isMacOS || FPlatform.isWindows) {
        rpc.platform.setLanguageCodeAsync(
          locale.toCodeString("-"),
        );
      }
      await Store.app.updateLocale(
        locale,
        updateApp: true,
        saveImmediately: true,
      );
    } catch (e) {
      logger.e('UserSettingController updateLanguage ex:', e);
      Store.app.cancelBusy();
    }
    final languageService = Get.find<LanguageService>();

    /// 初始化语言包
    languageService.init();
    Store.app.cancelBusy();
  }
}
