// Author: fengshengxiong
// Date: 2024/5/8
// Description: 远程配置数据盒子

import 'dart:convert';

import 'package:flutter_translate/common/hive/hive.dart';
import 'package:flutter_translate/common/utils/log_utils.dart';
import 'package:flutter_translate/common/utils/obj_util.dart';
import 'package:flutter_translate/global/app_config.dart';

class RemoteConfigBox {
  RemoteConfigBox._();

  static final RemoteConfigBox _instance = RemoteConfigBox._();

  factory RemoteConfigBox() {
    return _instance;
  }

  /// isOpenKey
  final _isOpenKey = 'isOpenKey';

  /// 广告开关
  final _adSwitchKey = 'adSwitchKey';

  /// 声明盒子
  /// 注意, main函数中这个盒子已经打开, 可以进行存储操作
  final _box = getRemoteConfigBox();

  Future<void> putIsOpen(String isOpen) async {
    return await _box.put(_isOpenKey, isOpen);
  }

  /// 获取远程版本
  String? getVersion() {
    String? isOpen = _box.get(_isOpenKey);
    String? version;
    if (ObjUtil.isNotEmpty(isOpen)) {
      try {
        version = jsonDecode(isOpen!)['version'];
      } catch (e) {
        Log.d(e.toString());
      }
    }
    return version;
  }

  Future<void> putAdSwitch(bool adSwitch) async {
    return await _box.put(_adSwitchKey, adSwitch);
  }

  bool getAdSwitch() {
    return _box.get(_adSwitchKey, defaultValue: adSwitch);
  }
}
