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

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

import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.smaato.sdk.demoapp.R;

public class ShowAdMobBannerActivity extends AppCompatActivity {

    private AdView mAdView;

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

        mAdView = findViewById(R.id.adView);

        mAdView.setAdListener(new AdListener() {
            @Override
            public void onAdClosed() {
                super.onAdClosed();
                ((TextView)findViewById(R.id.label_onAdClosed)).setTextColor(Color.BLACK);
            }

            @Override
            public void onAdFailedToLoad(int i) {
                super.onAdFailedToLoad(i);
                ((TextView)findViewById(R.id.label_onAdFailedToLoad)).setTextColor(Color.BLACK);
            }

            @Override
            public void onAdLeftApplication() {
                super.onAdLeftApplication();
                ((TextView)findViewById(R.id.label_onAdLeftApplication)).setTextColor(Color.BLACK);
            }

            @Override
            public void onAdOpened() {
                super.onAdOpened();
                ((TextView)findViewById(R.id.label_onAdOpened)).setTextColor(Color.BLACK);
            }

            @Override
            public void onAdLoaded() {
                super.onAdLoaded();
                ((TextView)findViewById(R.id.label_onAdLoaded)).setTextColor(Color.BLACK);
            }

            @Override
            public void onAdClicked() {
                super.onAdClicked();
                ((TextView)findViewById(R.id.label_onAdClicked)).setTextColor(Color.BLACK);
            }

            @Override
            public void onAdImpression() {
                super.onAdImpression();
                ((TextView)findViewById(R.id.label_onAdImpression)).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();
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);
    };

    private void resetEventsHighlightning() {
        ((TextView)findViewById(R.id.label_onAdClosed)).setTextColor(Color.LTGRAY);
        ((TextView)findViewById(R.id.label_onAdFailedToLoad)).setTextColor(Color.LTGRAY);
        ((TextView)findViewById(R.id.label_onAdLeftApplication)).setTextColor(Color.LTGRAY);
        ((TextView)findViewById(R.id.label_onAdOpened)).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_onAdImpression)).setTextColor(Color.LTGRAY);
    }

    // Called when leaving the activity
    @Override
    public void onPause() {
        mAdView.pause();
        super.onPause();
    }

    // Called when returning to the activity
    @Override
    public void onResume() {
        super.onResume();
        mAdView.resume();
    }

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