package com.prankapp.fartsounds.airhorn.ad_util;

import android.app.Activity;
import android.content.Context;
import android.util.Log;

import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;
import androidx.lifecycle.ProcessLifecycleOwner;

import com.applovin.mediation.MaxAd;
import com.applovin.mediation.MaxAdListener;
import com.applovin.mediation.MaxError;
import com.applovin.mediation.ads.MaxAppOpenAd;
import com.applovin.mediation.ads.MaxInterstitialAd;

public class MaxPrankAdManager implements LifecycleObserver, MaxAdListener {
    private MaxInterstitialAd appOpenAd;
    private Context context;
    AdPrankListener adListener;
    public MaxPrankAdManager(Context context, String id, AdPrankListener adLoadedListener) {
        ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
        this.adListener = adLoadedListener;
        this.context = context;
        appOpenAd = new MaxInterstitialAd(id, context);
        appOpenAd.setListener(this);
        appOpenAd.loadAd();
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    public void onStart() {
        //showAdIfReady();
    }

    @Override
    public void onAdLoaded(final MaxAd ad) {
        Log.e("prank","onAdLoaded");
        adListener.adPrankShow();
        appOpenAd.showAd();
    }

    @Override
    public void onAdLoadFailed(final String adUnitId, final MaxError error) {
        Log.e("prank","onAdLoadFailed");
    }

    @Override
    public void onAdDisplayed(final MaxAd ad) {
        Log.e("prank","onAdDisplayed");
    }

    @Override
    public void onAdClicked(final MaxAd ad) {
        Log.e("prank","onAdClicked");
    }

    @Override
    public void onAdHidden(final MaxAd ad) {
        adListener.adPrankClose();
        // appOpenAd.loadAd();
        Log.e("prank","onAdHidden");
    }

    @Override
    public void onAdDisplayFailed(final MaxAd ad, final MaxError error) {
        appOpenAd.loadAd();
    }
}
