import 'package:get/get.dart';
import 'package:vitalapp/architecture/app_parameters.dart';
import 'package:vitalapp/architecture/defines.dart';
import 'package:vitalapp/architecture/utils/prompt_box.dart';

import '../defines.dart';

class AppState extends StateModuleBase {
  final _busy = false.obs;
  final _serverUrl = AppParameters.data.serverGateway.obs;
  final _homePageUrl = AppParameters.data.homePageUrl.obs;
  final _enableEncryptSensitiveInfo = false.obs;

  /// 程序处理是否忙碌中
  bool get busy => _busy.value;
  set busy(bool v) {
    _busy.value = v;
    if (v) {
      PromptBox.loading("加载中...");
    } else {
      PromptBox.dismiss();
    }
  }

  /// 服务地址
  String get serverUrl => _serverUrl.value;
  set serverUrl(String v) => _serverUrl.updateValue(v);

  /// 首页地址
  String get homePageUrl => _homePageUrl.value;
  set homePageUrl(String v) => _homePageUrl.updateValue(v);

  /// 启用敏感信息脱敏
  bool get enableEncryptSensitiveInfo => _enableEncryptSensitiveInfo.value;
  set enableEncryptSensitiveInfo(bool v) =>
      _enableEncryptSensitiveInfo.updateValue(v);

  void setBusy(String text) {
    _busy.value = true;
    PromptBox.loading(text);
  }

  void cancelBusy() => busy = false;

  @override
  Future<void> acceptPersistenceJson(Map<String, dynamic> map) async {
    map.pickPersistentProp('serverUrl', (x) => serverUrl = x);
    map.pickPersistentProp('homePageUrl', (x) => homePageUrl = x);
  }

  @override
  Map<String, dynamic> toPersistenceJson() {
    return {
      "serverUrl": serverUrl,
      "homePageUrl": homePageUrl,
    };
  }
}
