/// Created by Haoyi on 5/26/21

part of '../ads.dart';

class RewardedAdsReportEventsObserver extends AdsLifecycleObserver {
  String get name => "RewardedAdsReportEventsHandler";

  void _apply(AdsBundle adsBundle, void Function(RewardedAds ads) callback) {
    if (adsBundle.ads is RewardedAds) {
      try {
        callback(adsBundle.ads as RewardedAds);
      } catch (error, stacktrace) {
        Log.w('RewardedAdsHandler apply error', error: error, stackTrace: stacktrace);
      }
    }
  }

  @override
  void onRequestLoad(AdsBundle adsBundle) {
    _apply(adsBundle, (ads) {
      GuruAnalytics.instance.logEventEx("rads_load");
      AiBi.instance.adsLoad(AdsType.rewarded, adScene: ads.scene);
    });
  }

  @override
  void onAdLoaded(AdsBundle adsBundle) {
    _apply(adsBundle, (ads) {
      GuruAnalytics.instance.logEventEx("rads_loaded",
          parameters: {"duration": ads.elapsedTimeInMillisSinceStartLoadAds});
      AiBi.instance.adsLoaded(AdsType.rewarded,
          adScene: ads.scene, duration: ads.elapsedTimeInMillisSinceStartLoadAds);
    });
  }

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

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

  @override
  void onAdDisplayFailed(AdsBundle adsBundle) {
    _apply(adsBundle, (ads) {
      String errorCode = adsBundle.arguments["errorName"] ?? "Unknown";
      GuruAnalytics.instance.logEventEx("rads_failed",
          itemCategory: "imp",
          parameters: {"duration": ads.elapsedTimeInMillisSinceLoadedAds, "error_code": errorCode});
      Log.d(
          "rads_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.rewarded, adScene: ads.scene);
    });
  }

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

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

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

  @override
  void onAdRewarded(AdsBundle adsBundle) {
    _apply(adsBundle, (ads) async {
      GuruAnalytics.instance.logEventEx("rads_rewarded", itemCategory: ads.scene);
      final userRewardedCount =
          await AppProperty.getInstance().getInt(PropertyKeys.userRewardedCount, defValue: 0);
      if (userRewardedCount == 0) {
        GuruAnalytics.instance.logEventEx("first_rads_rewarded", itemCategory: ads.scene);
      }
      await AppProperty.getInstance().setInt(PropertyKeys.userRewardedCount, userRewardedCount + 1);
      AiBi.instance.adsRewarded(ads.scene);
    });
  }
}
