import 'dart:convert';
import 'package:fis_common/logger/logger.dart';
import 'package:flyinsono/architecture/services/entity/report_editor_setting.dart';
import 'package:flyinsono/architecture/storage/text_storage.dart';
import 'package:get/get.dart';

/// 本地报告编辑器服务
class LocalReportEditorService extends GetxService {
  String _cache = "";

  /// 获取设置的缓存
  void restoreLocalCache() {
    try {
      var map = json.decode(_cache);
      ReportEditorSetting.ins.setDataFromJson(map);
    } catch (e) {
      logger.e('LocalReportEditorService restoreLocalCache ex:', e);
    }
  }

  static const _SETTING_FILENAME = "local_report_editor_setting.conf";
  TextStorage _entryStorage = TextStorage.createInstance(_SETTING_FILENAME);

  ///初始化
  Future<void> init() async {
    // 创建实例
    final list = await _entryStorage.fetch();
    if (list == null) {
      _cache = "";
    } else {
      _cache = list;
      restoreLocalCache();
    }
  }

  ///保存配置
  Future<bool> save() async {
    var jsonStr = json.encode(ReportEditorSetting.ins.toJson());
    return _entryStorage.save(jsonStr);
  }
}
