package com.smaato.sdk.demoapp.banner;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SwitchCompat;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.smaato.sdk.banner.ad.AutoReloadInterval;
import com.smaato.sdk.banner.widget.BannerError;
import com.smaato.sdk.banner.widget.BannerView;
import com.smaato.sdk.demoapp.R;

import static com.smaato.sdk.demoapp.utils.SizeUtils.dpsToPixels;

public class ShowBannerAdsActivity extends AppCompatActivity {

    private static final String TAG = "ShowBannerAdsActivity";

    private static final String KEY_BANNER_AD_CONFIG = "banner_ad_config";
    @NonNull
    private TextView onAdLoadedView;
    @NonNull
    private TextView onAdFailedToLoadView;
    @NonNull
    private TextView onAdClickedView;
    @NonNull
    private TextView onAdTtlExpiredView;

    @NonNull
    public static Intent createIntent(@NonNull Activity context, @NonNull BannerAdConfig bannerAdConfig) {
        Intent intent = new Intent(context, ShowBannerAdsActivity.class);
        intent.putExtra(KEY_BANNER_AD_CONFIG, bannerAdConfig);
        return intent;
    }

    private BannerAdConfig bannerAdConfig;
    private TextView bannerViewStub;
    private BannerView bannerView;
    private SwitchCompat autoReloadSwitch;
    private Button btnShow;

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

        bannerAdConfig = getIntent().getParcelableExtra(KEY_BANNER_AD_CONFIG);

        bannerViewStub = findViewById(R.id.banner_view_stub);
        bannerView = findViewById(R.id.banner_view);
        autoReloadSwitch = findViewById(R.id.switch_auto_reload);
        Button btnLoad = findViewById(R.id.button_load);
        btnShow = findViewById(R.id.button_show);

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

        onAdLoadedView = findViewById(R.id.on_ad_loaded_view);
        onAdFailedToLoadView = findViewById(R.id.on_ad_failed_to_load_view);
        onAdClickedView = findViewById(R.id.on_ad_clicked_view);
        onAdTtlExpiredView = findViewById(R.id.on_ad_ttl_expired_view);

        resetBannerViewUi();

        if (savedInstanceState == null) {
            autoReloadSwitch.setChecked(true);
        }

        autoReloadSwitch.setOnCheckedChangeListener(autoReloadSwitchListener);
        btnLoad.setOnClickListener(btnLoadClickListener);
        btnShow.setOnClickListener(btnShowClickListener);

        bannerViewStub.setLayoutParams(getBannerViewStubLayoutParams());
    }

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

    private void resetBannerViewUi() {
        bannerView.setVisibility(View.INVISIBLE);
        bannerViewStub.setVisibility(View.VISIBLE);
        bannerViewStub.setText(R.string.tap_load_to_load_ad_block);
        btnShow.setEnabled(false);
        onAdLoadedView.setAlpha(0.2f);
        onAdFailedToLoadView.setAlpha(0.2f);
        onAdClickedView.setAlpha(0.2f);
        onAdTtlExpiredView.setAlpha(0.2f);
    }

    private final CompoundButton.OnCheckedChangeListener autoReloadSwitchListener = (view, isChecked) -> {
        Log.d(TAG, "autoReloadSwitchListener: isChecked = " + isChecked);
        bannerView.setAutoReloadInterval(isChecked ? AutoReloadInterval.DEFAULT : AutoReloadInterval.DISABLED);
    };

    private final View.OnClickListener btnLoadClickListener = v -> {
        resetBannerViewUi();

        bannerView.setEventListener(new BannerView.EventListener() {
            @Override
            public void onAdLoaded(@NonNull BannerView bannerView) {
                Log.d(TAG, "onAdLoaded");
                btnShow.setEnabled(true);
                bannerViewStub.setText(R.string.ad_loaded_tap_show);
                onAdLoadedView.setAlpha(1f);
            }

            @Override
            public void onAdFailedToLoad(@NonNull BannerView bannerView, @NonNull BannerError bannerError) {
                Log.d(TAG, "onAdFailedToLoad: " + bannerError);
                bannerViewStub.setText(R.string.load_ad_error_message);
                onAdFailedToLoadView.setAlpha(1f);
            }

            @Override
            public void onAdClicked(@NonNull BannerView bannerView) {
                Log.d(TAG, "onAdClicked");
                onAdClickedView.setAlpha(1f);
            }

            @Override
            public void onAdTTLExpired(@NonNull BannerView bannerView) {
                Log.d(TAG, "onTtlExpired");
                onAdTtlExpiredView.setAlpha(1f);
            }
        });

        bannerView.setAutoReloadInterval(
                autoReloadSwitch.isChecked() ? AutoReloadInterval.DEFAULT : AutoReloadInterval.DISABLED
        );

        bannerViewStub.setText(R.string.loading_progress_message);

        bannerView.loadAd(bannerAdConfig.publisherId, bannerAdConfig.adSpaceId, bannerAdConfig.bannerAdSize);
    };

    private final View.OnClickListener btnShowClickListener = v -> {
        bannerView.setVisibility(View.VISIBLE);
        bannerViewStub.setVisibility(View.GONE);
    };

    @NonNull
    private FrameLayout.LayoutParams getBannerViewStubLayoutParams() {
        int width, height;

        switch (bannerAdConfig.bannerAdSize) {
            case XX_LARGE_320x50:
                width = 320;
                height = 50;
                break;
            case MEDIUM_RECTANGLE_300x250:
                width = 300;
                height = 250;
                break;
            case LEADERBOARD_728x90:
                width = 728;
                height = 90;
                break;
            case SKYSCRAPER_120x600:
                width = 120;
                height = 600;
                break;
            default:
                throw new RuntimeException("unexpected bannerAdSize: " + bannerAdConfig.bannerAdSize);
        }

        return new FrameLayout.LayoutParams(
                dpsToPixels(this, width),
                dpsToPixels(this, height),
                Gravity.CENTER_HORIZONTAL
        );
    }
}
