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.mobileads.MoPubErrorCode;
import com.mopub.mobileads.MoPubView;
import com.smaato.sdk.demoapp.R;

public class ShowMopubBannerActivity extends AppCompatActivity {

    private static final String TAG = "ShowMopubBannerActivity";
    private static final String AD_UNIT_ID = "174e2e8a00e546e09123317a055c4d8a";//old"befde797ef9f4792a8d29c328737f925";//new

    private MoPubView mAdView;

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

        mAdView = findViewById(R.id.adView);
        mAdView.setAdUnitId(AD_UNIT_ID);
        mAdView.setBannerAdListener(new MoPubView.BannerAdListener() {
            @Override
            public void onBannerLoaded(MoPubView banner) {
                ((TextView) findViewById(R.id.label_onAdLoaded)).setTextColor(Color.BLACK);
            }

            @Override
            public void onBannerFailed(MoPubView banner, MoPubErrorCode errorCode) {
                ((TextView) findViewById(R.id.label_onAdFailedToLoad)).setTextColor(Color.BLACK);
            }

            @Override
            public void onBannerClicked(MoPubView banner) {
                ((TextView) findViewById(R.id.label_onAdClicked)).setTextColor(Color.BLACK);
            }

            @Override
            public void onBannerExpanded(MoPubView banner) {
                ((TextView) findViewById(R.id.label_onAdExpanded)).setTextColor(Color.BLACK);
            }

            @Override
            public void onBannerCollapsed(MoPubView banner) {
                ((TextView) findViewById(R.id.label_onAdCollapsed)).setTextColor(Color.BLACK);
            }
        });

        ((TextView) findViewById(R.id.ad_unit_id)).setText(mAdView.getAdUnitId());

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

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

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

    // Called before the activity is destroyed
    @Override
    public void onDestroy() {
        mAdView.destroy();
        super.onDestroy();
    }
}
