package com.smaato.sdk.demoapp.interstitial;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;

import com.smaato.sdk.demoapp.AdConfig;
import com.smaato.sdk.demoapp.R;
import com.smaato.sdk.interstitial.EventListener;
import com.smaato.sdk.interstitial.Interstitial;
import com.smaato.sdk.interstitial.InterstitialAd;
import com.smaato.sdk.interstitial.InterstitialError;
import com.smaato.sdk.interstitial.InterstitialRequestError;

public class ShowInterstitialAdActivity extends AppCompatActivity {

    private static final String INTERSTITIAL_AD_CONFIG_KEY = "interstitialAdConfig";

    @NonNull
    private TextView interstitialInfoView;
    @NonNull
    private TextView onAdLoadedView;
    @NonNull
    private TextView onAdFailedToLoadView;
    @NonNull
    private TextView onAdErrorView;
    @NonNull
    private TextView onAdOpenedView;
    @NonNull
    private TextView onAdClosedView;
    @NonNull
    private TextView onAdClickedView;
    @NonNull
    private TextView onAdImpressionView;
    @NonNull
    private TextView onAdTtlExpiredView;
    @NonNull
    private Button showButton;
    @Nullable
    private InterstitialAd smaInterstitialAd;

    @NonNull
    public static Intent createIntent(@NonNull Context context, @NonNull AdConfig adConfig) {
        Intent intent = new Intent(context, ShowInterstitialAdActivity.class);
        intent.putExtra(INTERSTITIAL_AD_CONFIG_KEY, adConfig);

        return intent;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_interstitial_ads);
        AdConfig adConfig = getIntent().getParcelableExtra(INTERSTITIAL_AD_CONFIG_KEY);

        Button loadButton = findViewById(R.id.button_load);
        showButton = findViewById(R.id.button_show);
        interstitialInfoView = findViewById(R.id.interstitial_info_view);
        interstitialInfoView.setText(getString(R.string.interstitial_ad_tap_to_load));

        ((TextView) findViewById(R.id.publisher_id)).setText(adConfig.publisherId);
        ((TextView) findViewById(R.id.adspace_id)).setText(adConfig.adSpaceId);

        onAdLoadedView = findViewById(R.id.on_ad_loaded_view);
        onAdFailedToLoadView = findViewById(R.id.on_ad_failed_to_load_view);
        onAdErrorView = findViewById(R.id.on_ad_error_view);
        onAdOpenedView = findViewById(R.id.on_ad_opened_view);
        onAdClosedView = findViewById(R.id.on_ad_closed_view);
        onAdClickedView = findViewById(R.id.on_ad_clicked_view);
        onAdImpressionView = findViewById(R.id.on_ad_impression_view);
        onAdTtlExpiredView = findViewById(R.id.on_ad_ttl_expired_view);

        loadButton.setOnClickListener(v -> {
            if (adConfig.publisherId != null && adConfig.adSpaceId != null) {
                resetCallbackTextViews();
                interstitialInfoView.setText(getString(R.string.interstitial_ad_loading));
                Interstitial.loadAd(adConfig.publisherId, adConfig.adSpaceId, createInterstitialEventListener());
            }
        });

        showButton.setOnClickListener(v -> {
            if (smaInterstitialAd != null && smaInterstitialAd.isAvailableForPresentation()) {
                smaInterstitialAd.showAd(ShowInterstitialAdActivity.this);
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @NonNull
    private EventListener createInterstitialEventListener() {
        return new EventListener() {
            @Override
            public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
                onAdLoadedView.setAlpha(1f);
                smaInterstitialAd = interstitialAd;
                showButton.setEnabled(true);
                interstitialInfoView.setText(getString(R.string.interstitial_ad_loaded));
            }

            @Override
            public void onAdFailedToLoad(@NonNull InterstitialRequestError interstitialRequestError) {
                onAdFailedToLoadView.setAlpha(1f);
                interstitialInfoView.setText(getString(R.string.interstitial_ad_loading_failed));
            }

            @Override
            public void onAdError(@NonNull InterstitialAd interstitialAd, @NonNull InterstitialError interstitialError) {
                onAdErrorView.setAlpha(1f);
            }

            @Override
            public void onAdOpened(@NonNull InterstitialAd interstitialAd) {
                onAdOpenedView.setAlpha(1f);
                interstitialInfoView.setText(getString(R.string.interstitial_ad_load_new));
                showButton.setEnabled(false);
            }

            @Override
            public void onAdClosed(@NonNull InterstitialAd interstitialAd) {
                onAdClosedView.setAlpha(1f);
            }

            @Override
            public void onAdClicked(@NonNull InterstitialAd interstitialAd) {
                onAdClickedView.setAlpha(1f);
            }

            @Override
            public void onAdImpression(@NonNull InterstitialAd interstitialAd) {
                onAdImpressionView.setAlpha(1f);
            }

            @Override
            public void onAdTTLExpired(@NonNull InterstitialAd interstitialAd) {
                onAdTtlExpiredView.setAlpha(1f);
                interstitialInfoView.setText(getString(R.string.interstitial_ad_expired));
                showButton.setEnabled(false);
            }
        };
    }

    private void resetCallbackTextViews() {
        onAdLoadedView.setAlpha(0.2f);
        onAdFailedToLoadView.setAlpha(0.2f);
        onAdErrorView.setAlpha(0.2f);
        onAdOpenedView.setAlpha(0.2f);
        onAdClosedView.setAlpha(0.2f);
        onAdClickedView.setAlpha(0.2f);
        onAdImpressionView.setAlpha(0.2f);
        onAdTtlExpiredView.setAlpha(0.2f);
    }
}
