/// Created by @Haoyi on 4/19/21

part of "app_property.dart";

class PropertyBundle {
  final Map<PropertyKey, String> data;

  PropertyBundle({Map<PropertyKey, String>? map}) : data = <PropertyKey, String>{} {
    if (map != null) {
      data.addAll(map);
    }
  }

  PropertyBundle.empty() : data = <PropertyKey, String>{};

  bool containsName(PropertyKey name) {
    return data.containsKey(name);
  }

  void intersection(Map<PropertyKey, dynamic> map) {
    final keySets = map.keys.toSet();
    final currentSets = data.keys.toSet();

    final sets = currentSets.intersection(keySets);

    data.removeWhere((key, _) => !sets.contains(key));
  }

  void setInt(PropertyKey key, int value) {
    data[key] = value.toString();
  }

  void setDouble(PropertyKey key, double value) {
    data[key] = value.toString();
  }

  void setString(PropertyKey key, String value) {
    data[key] = value;
  }

  void setBool(PropertyKey key, bool value) {
    data[key] = value ? "1" : "0";
  }

  int? getInt(PropertyKey key) {
    final value = data[key];
    if (value == null) {
      return null;
    }
    return int.parse(value);
  }

  double? getDouble(PropertyKey key) {
    final value = data[key];
    if (value == null) {
      return null;
    }
    return double.parse(value);
  }

  String? getString(PropertyKey key) {
    return data[key];
  }

  bool? getBool(PropertyKey key) {
    final value = data[key];
    if (value == null) {
      return null;
    }
    return value == "1";
  }

  Future<void> forEach(void Function(PropertyKey key, String value) f) async {
    for (var entry in data.entries) {
      f.call(entry.key, entry.value);
    }
  }

  void clear() {
    data.clear();
  }
}
