import 'dart:convert';

import 'package:fis_common/logger/logger.dart';

class LocalSetting {
  LocalSetting._internal();

  static LocalSetting? _instance;

  /// 获取单例
  static LocalSetting get ins {
    if (_instance == null) {
      _instance = LocalSetting._internal();
    }
    return _instance!;
  }

  ///是否显示第二窗口
  bool? isUseSecondWindow = false;

  /// 开机自启动
  bool? isAutoStartUp = false;

  /// 直播录屏保存位置（杏聆荟学院-培训业务），仅 PC 端有效
  String? liveRecordPath = "";

  LocalSetting({
    this.isUseSecondWindow,
    this.isAutoStartUp,
    this.liveRecordPath,
  });

  ///当前实体转为json字符串
  String toJson() {
    var object = {
      'isUseSecondWindow': isUseSecondWindow,
      'isAutoStartUp': isAutoStartUp,
      'liveRecordPath': liveRecordPath,
    };
    return json.encode(object);
  }

  ///从json中初始化数据
  LocalSetting? fromJson(Map<String, dynamic> json) {
    print("json :" + json.toString());
    if (json.containsKey('isUseSecondWindow')) {
      isUseSecondWindow = json['isUseSecondWindow'];
      isAutoStartUp = json['isAutoStartUp'];
      print("isUseSecondWindow 0:" + isUseSecondWindow.toString());
    } else {
      logger.e('LocalSetting form json ex: isUseSecondWindow is null');
    }
    try {
      liveRecordPath = json['liveRecordPath'];
    } catch (e) {
      logger.e('LocalSetting form json ex:', e);
    }
    return null;
  }
}
