package wb.receipts.workers;

import co.smartreceipts.android.purchases.Subscription;
import co.smartreceipts.android.purchases.SubscriptionWallet;
import co.smartreceipts.android.workers.AdManager;
import co.smartreceipts.android.workers.WorkerManager;
import wb.receipts.R;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.view.View;
import co.smartreceipts.android.persistence.SharedPreferenceDefinitions;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

import java.lang.ref.WeakReference;

public class SRFreeAdManager extends AdManager {

    //Preference Identifiers - SubClasses Only
    private static final String AD_PREFERENECES = SharedPreferenceDefinitions.Subclass_Preferences.toString();
    private static final String HIDE_AD = "pref1";

    private WeakReference<AdView> mAdViewReference;

    public SRFreeAdManager(@NonNull WorkerManager manager, @NonNull SubscriptionWallet subscriptionWallet) {
        super(manager, subscriptionWallet);
    }

    public void onActivityCreated(@NonNull Activity activity) {
        final AdView adView = (AdView) activity.findViewById(R.id.adView);
        mAdViewReference = new WeakReference<>(adView);
        if (adView != null) {
            if (shouldShowAds(adView)) {
                adView.loadAd(getAdRequest());
            } else {
                adView.setVisibility(View.GONE);
            }
        }
    }

    public void onResume() {
        final AdView adView = mAdViewReference.get();
        if (adView != null) {
            if (shouldShowAds(adView)) {
                adView.resume();
            } else {
                adView.setVisibility(View.GONE);
            }
        }
    }

    public void onPause() {
        final AdView adView = mAdViewReference.get();
        if (adView != null) {
            if (shouldShowAds(adView)) {
                adView.pause();
            } else {
                adView.setVisibility(View.GONE);
            }
        }
    }

    public void onDestroy() {
        final AdView adView = mAdViewReference.get();
        if (adView != null) {
            if (shouldShowAds(adView)) {
                adView.destroy();
            } else {
                adView.setVisibility(View.GONE);
            }
        }
    }

    private boolean shouldShowAds(@NonNull AdView adView) {
        return !adView.getContext().getSharedPreferences(AD_PREFERENECES, 0).getBoolean(HIDE_AD, false) && !getSubscriptionWallet().hasSubscription(Subscription.SmartReceiptsPro);
    }

    private static AdRequest getAdRequest() {
        return new AdRequest.Builder()
                .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                .addTestDevice("BFB48A3556EED9C87CB3AD907780D610")
                .build();
    }

}

@@ -438,24 +438,70 @@
 finitions;%0A%0A
+import com.google.android.gms.ads.AdListener;%0A
 import com.g
@@ -1399,16 +1399,334 @@
 est());%0A
+                adView.setAdListener(new AdListener() %7B%0A                    @Override%0A                    public void onAdFailedToLoad(int errorCode) %7B%0A                        // If we fail to load the ad, just hide it%0A                        adView.setVisibility(View.GONE);%0A                    %7D%0A                %7D);%0A
         

Set the ad view to GONE when we fail to load the ad