package com.smaato.sdk.demoapp.adapters.mopub;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import com.mopub.common.MoPub;
import com.mopub.mobileads.MoPubErrorCode;
import com.mopub.mobileads.MoPubInterstitial;
import com.smaato.sdk.demoapp.R;

public class ShowMopubInterstitialActivity extends AppCompatActivity {

    private static final String TAG = "ShowMopubInterstitialActivity";
    private static final String AD_UNIT_ID = "a31fdeca836e421fbb4a2b76be427b16";//old"24534e1901884e398f1253216226017e";//

    private MoPubInterstitial mAdView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_mopub_interstitial);

        mAdView = new MoPubInterstitial(this, AD_UNIT_ID);
        mAdView.setInterstitialAdListener(new MoPubInterstitial.InterstitialAdListener() {
            @Override
            public void onInterstitialLoaded(MoPubInterstitial interstitial) {
                ((TextView) findViewById(R.id.label_onAdLoaded)).setTextColor(Color.BLACK);
                findViewById(R.id.button_show).setEnabled(true);
            }

            @Override
            public void onInterstitialFailed(MoPubInterstitial interstitial, MoPubErrorCode errorCode) {
                ((TextView) findViewById(R.id.label_onAdFailedToLoad)).setTextColor(Color.BLACK);
            }

            @Override
            public void onInterstitialShown(MoPubInterstitial interstitial) {
                ((TextView) findViewById(R.id.label_onAdImpression)).setTextColor(Color.BLACK);
            }

            @Override
            public void onInterstitialClicked(MoPubInterstitial interstitial) {
                ((TextView) findViewById(R.id.label_onAdClicked)).setTextColor(Color.BLACK);
            }

            @Override
            public void onInterstitialDismissed(MoPubInterstitial interstitial) {
                ((TextView) findViewById(R.id.label_onAdDismissed)).setTextColor(Color.BLACK);
            }
        });

        ((TextView) findViewById(R.id.ad_unit_id)).setText(AD_UNIT_ID);

        findViewById(R.id.button_load).setOnClickListener(btnLoadClickListener);
        findViewById(R.id.button_show).setOnClickListener(btnShowClickListener);
    }

    private final View.OnClickListener btnLoadClickListener = v -> {
        resetEventsHighlightning();
        mAdView.load();
    };

    private final View.OnClickListener btnShowClickListener = v -> {
        mAdView.show();
    };

    private void resetEventsHighlightning() {
        ((TextView) findViewById(R.id.label_onAdLoaded)).setTextColor(Color.LTGRAY);
        ((TextView) findViewById(R.id.label_onAdFailedToLoad)).setTextColor(Color.LTGRAY);
        ((TextView) findViewById(R.id.label_onAdImpression)).setTextColor(Color.LTGRAY);
        ((TextView) findViewById(R.id.label_onAdClicked)).setTextColor(Color.LTGRAY);
        ((TextView) findViewById(R.id.label_onAdDismissed)).setTextColor(Color.LTGRAY);
    }

    // Additional lifecycle events
    @Override
    protected void onPause() {
        super.onPause();
        MoPub.onPause(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        MoPub.onStop(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        MoPub.onResume(this);
    }

    @Override
    protected void onDestroy() {
        mAdView.destroy();
        super.onDestroy();
    }
}
