import 'package:guru_app/ads/core/ads.dart';
import 'package:guru_app/ads/core/strategy/ad_unit.dart';
import 'package:guru_app/guru_app.dart';
import 'package:guru_app/property/app_property.dart';
import 'package:guru_applovin_flutter/guru_applovin_flutter.dart';
import 'package:guru_applovin_flutter/interstitial_ad.dart';
import 'package:guru_utils/ads/data/ads_model.dart';

/// Created by Haoyi on 2023/6/28

class MaxInterstitialAdUnit extends AdUnit<InterstitialAdEvent> {
  late InterstitialAd interstitialAd;

  @override
  final String name;

  MaxInterstitialAdUnit.create(AdUnitId adUnitId, AdSlotId? amazonAdSlotId)
      : name = "MaxInterAdUnit-${adUnitId.id}",
        super(adUnitId, amazonAdSlotId: amazonAdSlotId) {
    addObserver(InterstitialAdsReportEventsObserver());
  }

  @override
  void init() {
    super.init();
    interstitialAd = InterstitialAd(
        adUnitId: adUnitId.id, adAmazonSlotId: amazonAdSlotId?.id, listener: dispatchEvent);
  }

  @override
  Map<InterstitialAdEvent, AdsEvent> get eventsMapping => {
        InterstitialAdEvent.onAdLoaded: AdsEvent.adLoaded,
        InterstitialAdEvent.onAdLoadFailed: AdsEvent.adLoadFailed,
        InterstitialAdEvent.onAdDisplayFailed: AdsEvent.adDisplayFailed,
        InterstitialAdEvent.onAdDisplayed: AdsEvent.adDisplayed,
        InterstitialAdEvent.onAdClicked: AdsEvent.adClick,
        InterstitialAdEvent.onAdHidden: AdsEvent.adHidden,
      };

  @override
  Future<bool> requestDispose() async {
    try {
      return await interstitialAd.dispose() ?? false;
    } catch (error, stacktrace) {
      Log.w("[$name] requestDispose error", error: error, stackTrace: stacktrace);
      return false;
    }
  }

  @override
  Future<bool> requestHide() async {
    return false;
  }

  @override
  Future<AdCause> requestLoad() async {
    try {
      Log.w("[$name] requestLoad", tag: PropertyTags.strategyAds);
      final result = await interstitialAd.load() ?? false;
      if (result) {
        return AdCause.success;
      } else {
        return AdCause.requestFailed;
      }
    } catch (error, stacktrace) {
      Log.w("requestLoad error! $error  $stacktrace", tag: PropertyTags.strategyAds);
      return AdCause.internalError;
    }
  }

  @override
  Future<AdCause> requestShow({required String scene, bool ignoreCheck = false}) async {
    Log.d("[$name] requestShow", tag: PropertyTags.strategyAds, syncFirebase: true);
    try {
      final result = await interstitialAd.show() ?? false;
      if (result) {
        return AdCause.success;
      } else {
        return AdCause.requestFailed;
      }
    } catch (error, stacktrace) {
      Log.w("requestShow error",
          error: error, stackTrace: stacktrace, syncFirebase: true, tag: PropertyTags.strategyAds);
      return AdCause.internalError;
    }
  }

  @override
  Future<bool> checkLoaded() async {
    try {
      return await interstitialAd.isLoaded() ?? false;
    } catch (error, stacktrace) {
      return false;
    }
  }

  @override
  Future<int> getStatus() async {
    try {
      return await interstitialAd.getAdState();
    } catch (error, stacktrace) {
      Log.w("[$name] getInterstitialAdStatus error",
          error: error, stackTrace: stacktrace, syncFirebase: true, tag: PropertyTags.strategyAds);
      return AdStatus.FAILED;
    }
  }
}
