/// Created by Haoyi on 2022/12/22

part of "../app_property.dart";

extension AnalyticsPropertyExtension on AppProperty {
  Future setAdId(String adId) async {
    if (adId.isNotEmpty) {
      await setString(PropertyKeys.analyticsAdId, adId);
    }
  }

  Future setFirebaseId(String firebaseId) async {
    if (firebaseId.isNotEmpty) {
      await setString(PropertyKeys.analyticsFirebaseId, firebaseId);
    }
  }

  Future setAdjustId(String adjustId) async {
    if (adjustId.isNotEmpty) {
      await setString(PropertyKeys.analyticsAdjustId, adjustId);
    }
  }

  Future setAnalyticsDeviceId(String deviceId) async {
    if (deviceId.isNotEmpty) {
      await setString(PropertyKeys.analyticsDeviceId, deviceId);
    }
  }

  Future setUserId(String userId) async {
    if (userId.isNotEmpty) {
      await setString(PropertyKeys.analyticsUserId, userId);
    }
  }

  Future setIdfa(String idfa) async {
    if (idfa.isNotEmpty) {
      await setString(PropertyKeys.analyticsIdfa, idfa);
    }
  }

  Future<Map<String, ABTestExperiment>> loadRunningExperiments() async {
    final bundle = await AppProperty.getInstance().loadValuesByTag(PropertyTags.guruExperiment);
    final result = <String, ABTestExperiment>{};
    bundle.forEach((key, value) {
      try {
        if (value.isNotEmpty) {
          final map = json.decode(value);
          final experiment = ABTestExperiment.fromJson(map);
          result[key.name] = experiment;
          Log.d("loadRunningExperiments: ${key.name} => $experiment");
        }
      } catch (error, stacktrace) {
        Log.w("getExperiment error! $error");
      }
    });
    return result;
  }

  Future<ABTestExperiment?> getExperiment(String experimentName, {PropertyBundle? bundle}) async {
    final experimentKey = PropertyKeys.buildExperimentProperty(experimentName);
    final result = bundle?.getString(experimentKey) ?? await getString(experimentKey, defValue: "");
    try {
      if (result.isNotEmpty) {
        final map = json.decode(result);
        return ABTestExperiment.fromJson(map);
      }
    } catch (error, stacktrace) {
      Log.w("getExperiment error! $error");
    }
    return null;
  }

  Future<String> getExperimentVariant(String experimentName) async {
    final variantKey =
        PropertyKeys.buildABTestProperty(GuruAnalytics.buildVariantKey(experimentName));
    return await getString(variantKey, defValue: "");
  }

  Future<String> setExperiment(ABTestExperiment experiment) async {
    PropertyBundle propertyBundle = PropertyBundle();

    final variantKey =
        PropertyKeys.buildABTestProperty(GuruAnalytics.buildVariantKey(experiment.name));
    final variantName = experiment.variantName;
    propertyBundle.setString(variantKey, variantName);

    final experimentKey = PropertyKeys.buildExperimentProperty(experiment.name);
    propertyBundle.setString(experimentKey, json.encode(experiment));
    await setProperties(propertyBundle);
    return variantName;
  }

  removeExperiment(String experimentName) async {
    final experimentKey = PropertyKeys.buildExperimentProperty(experimentName);
    await remove(experimentKey);
    final variantKey = PropertyKeys.buildABTestProperty(experimentName);
    await remove(variantKey);
  }

  Future<bool> refreshGoogleDma(String googleDma) async {
    final oldGoogleDma =
        await AppProperty.getInstance().getString(PropertyKeys.googleDma, defValue: "");
    if (googleDma != oldGoogleDma) {
      await AppProperty.getInstance().setString(PropertyKeys.googleDma, googleDma);
      return true;
    }
    return false;
  }
}
