/// Created by Haoyi on 5/10/21
part of '../ads.dart';

class InterstitialAdsReportEventsObserver extends AdsLifecycleObserver {
  @override
  String get name => "InterstitialAdsReportEventsHandler";

  void _apply(AdsBundle adsBundle, void Function(AdsAudit ads) callback) {
    if (adsBundle.ads is AdsAudit) {
      try {
        callback(adsBundle.ads as AdsAudit);
      } catch (error, stacktrace) {
        Log.i('InterstitialAdsHandler apply error:$error, $stacktrace');
      }
    }
  }

  @override
  void onRequestLoad(AdsBundle adsBundle) {
    _apply(adsBundle, (ads) {
      GuruAnalytics.instance.logEventEx("iads_load");
    });
  }

  @override
  void onRequestReset(AdsBundle adsBundle) {
    _apply(adsBundle, (ads) {
      GuruAnalytics.instance.logEventEx("iads_rebuild");
    });
  }

  @override
  void onAdLoaded(AdsBundle adsBundle) {
    _apply(adsBundle, (ads) {
      GuruAnalytics.instance.logEventEx("iads_loaded",
          parameters: {"duration": ads.elapsedTimeInMillisSinceStartLoadAds});

      AiBi.instance.adsLoaded(AdsType.interstitial,
          adScene: ads.scene, duration: ads.elapsedTimeInMillisSinceStartLoadAds);
    });
  }

  @override
  void onAdLoadFailed(AdsBundle adsBundle) {
    _apply(adsBundle, (ads) {
      String errorCode = adsBundle.arguments["errorName"] ?? "Unknown";
      GuruAnalytics.instance.logEventEx("iads_failed", itemCategory: "load", parameters: {
        "duration": ads.elapsedTimeInMillisSinceStartLoadAds,
        "error_code": errorCode
      });
      AiBi.instance.adsFailed(AdsType.interstitial, adScene: ads.scene);
    });
  }

  @override
  void onAdDisplayFailed(AdsBundle adsBundle) {
    _apply(adsBundle, (ads) {
      String errorCode = adsBundle.arguments["errorName"] ?? "Unknown";
      GuruAnalytics.instance.logEventEx("iads_failed",
          itemCategory: "imp",
          parameters: {"duration": ads.elapsedTimeInMillisSinceLoadedAds, "error_code": errorCode});
      Log.d(
          "iads_display_failed creativeId:${adsBundle.getString("ad_creative_id")} networkName:${adsBundle.getString("ad_network_name")} errorCode:$errorCode duration:${ads.elapsedTimeInMillisSinceLoadedAds}",
          syncFirebase: true);
      AiBi.instance.adsFailed(AdsType.interstitial, adScene: ads.scene);
    });
  }

  @override
  void onAdDisplayed(AdsBundle adsBundle) {
    _apply(adsBundle, (ads) {
      GuruAnalytics.instance.logAdImpression("iads_imp", AdTypeName.AD_TYPE_INTERSTITIAL,
          scene: ads.scene,
          adName: ads.scene,
          parameters: {"duration": "${ads.elapsedTimeInMillisSinceLoadedAds}"});
      Log.d(
          "iads_imp creativeId:${adsBundle.getString("ad_creative_id")} networkName:${adsBundle.getString("ad_network_name")} duration:${ads.elapsedTimeInMillisSinceLoadedAds}",
          syncFirebase: true);
      AiBi.instance.adsImp(AdsType.interstitial,
          adScene: ads.scene,
          adRevenue: adsBundle.getDouble("ad_revenue", defValue: 0.0),
          network: adsBundle.getString("ad_network_name", defValue: "unknown"));
    });
  }

  @override
  void onAdClicked(AdsBundle adsBundle) {
    _apply(adsBundle, (ads) {
      GuruAnalytics.instance.logAdClick("iads_clk", AdTypeName.AD_TYPE_INTERSTITIAL,
          scene: ads.scene, adName: ads.scene);
      AiBi.instance.adsClk(AdsType.interstitial, adScene: ads.scene);
    });
  }

  @override
  void onAdHidden(AdsBundle adsBundle) {
    _apply(adsBundle, (ads) {
      GuruAnalytics.instance.logEventEx("iads_close", itemCategory: ads.scene);
      AiBi.instance.adsHide(AdsType.interstitial, adScene: ads.scene);
    });
  }
}
