// /// Created by Haoyi on 2022/2/28
//
// part of "../analytics.dart";
//
//

part of "../guru_analytics.dart";

extension AdsAnalytics on GuruAnalytics {
  void logAdRevenue(double adRevenue, String adPlatform, String currency,
      {String? orderType, String? orderId, String? productId, int? transactionDate}) {
    // logEventEx(name, itemCategory: scene, itemName: adName);
    final orderExtras = CollectionUtils.filterOutNulls(<String, dynamic>{
      "order_type": orderType,
      "order_id": orderId,
      "product_id": productId,
      "trans_ts": transactionDate
    });
    if (release) {
      EventLogger.logAdRevenue(adRevenue, adPlatform, currency, extras: orderExtras);
    } else {
      Log.d("[firebase] logAdRevenue ${<String, dynamic>{
        "adRevenue": adRevenue,
        "adPlatform": adPlatform,
        "currency": currency,
        ...orderExtras
      }}");
    }
  }

  void logAdRevenue020(double adRevenue, String adPlatform, String currency,
      {String? orderType, String? orderId, String? productId, int? transactionDate}) {
    // logEventEx(name, itemCategory: scene, itemName: adName);
    final orderExtras = CollectionUtils.filterOutNulls(<String, dynamic>{
      "order_type": orderType,
      "order_id": orderId,
      "product_id": productId,
      "trans_ts": transactionDate
    });
    if (release) {
      EventLogger.logAdRevenue020(adRevenue, adPlatform, currency, extras: orderExtras);
    } else {
      Log.d("[firebase] logAdRevenue020 ${<String, dynamic>{
        "adRevenue": adRevenue,
        "adPlatform": adPlatform,
        "currency": currency,
        ...orderExtras
      }}");
    }
  }

  void logAdLtv(String phase, double ltv) {
    if (release) {
      EventLogger.logAdLtv(phase, ltv);
    } else {
      Log.d("[firebase] logAdLtv ${<String, dynamic>{"phase": phase, "ltv": ltv}}");
    }
  }

  void logAdImpression(String name, String adType,
      {String scene = "", String adName = "", Map<String, dynamic> parameters = const {}}) {
    logEventEx(name, itemCategory: scene, itemName: adName, parameters: parameters);
    if (release) {
      EventLogger.logFbAdImpression(adType);
      FirebaseCrashlytics.instance
          .log("adImp: name($name) scene($scene) adName($adName) adType($adType)");
    } else {
      Log.d("[facebook] logEvent logFbAdImpression: $adType");
    }
  }

  void logAdImp(ImpressionData data) {
    EventLogger.logAdImpression(
        adPlatform: data.platform,
        adSource: data.networkName,
        adFormat: data.unitFormat,
        adUnitName: data.unitName,
        value: data.publisherRevenue,
        currency: data.currency);
  }

  void logAdClick(String name, String adType, {String scene = "", String adName = ""}) {
    logEventEx(name, itemCategory: scene, itemName: adName);
    if (release) {
      EventLogger.logFbAdClick(adType);
    } else {
      Log.d("[facebook] logEvent logAdClick: $adType");
    }
  }
}
