package com.smaato.sdk.demoapp.interstitial;

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.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.smaato.sdk.demoapp.AdConfig;
import com.smaato.sdk.demoapp.AdConfigAdapter;
import com.smaato.sdk.demoapp.R;

import java.util.ArrayList;
import java.util.List;

public class InterstitialAdsActivity extends AppCompatActivity {

    private static final String TEST_PUBLISHER_ID = "0";
    private static final String TEST_AD_SPACE_ID = "130512792";
    private static final int REQUEST_CODE_CREATE_INTERSTITIAL_AD_CONFIG = 1;
    private static final int REQUEST_CODE_EDIT_INTERSTITIAL_AD_CONFIG = 2;

    @NonNull
    private InterstitialAdConfigStorage interstitialAdConfigStorage;
    @NonNull
    private AdConfigAdapter adConfigAdapter;

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

        findViewById(R.id.create_interstitial_ad_config_button).setOnClickListener(v -> {
            Intent intent = new Intent(this, CreateInterstitialAdConfigActivity.class);
            startActivityForResult(intent, REQUEST_CODE_CREATE_INTERSTITIAL_AD_CONFIG);
        });

        interstitialAdConfigStorage = new InterstitialAdConfigStorage(this);

        List<AdConfig> adConfigs = createDefaultInterstitialItemsList();
        int numberOfDefaultItems = adConfigs.size();
        adConfigs.addAll(interstitialAdConfigStorage.get());

        RecyclerView recyclerView = findViewById(R.id.interstitial_recycler_view);
        adConfigAdapter = new AdConfigAdapter(adConfigs, numberOfDefaultItems);
        adConfigAdapter.setItemClickListener(new AdConfigAdapter.ItemClickListener() {
            @Override
            public void onItemClick(@NonNull AdConfig adConfig) {
                startActivity(ShowInterstitialAdActivity.createIntent(InterstitialAdsActivity.this, adConfig));
            }

            @Override
            public void onItemEditClick(@NonNull AdConfig adConfig) {
                Intent intent = new Intent(InterstitialAdsActivity.this, CreateInterstitialAdConfigActivity.class);
                intent.putExtra(CreateInterstitialAdConfigActivity.OLD_CONFIG, adConfig);
                startActivityForResult(intent, REQUEST_CODE_EDIT_INTERSTITIAL_AD_CONFIG);
            }

            @Override
            public void onItemDeleteClick(@NonNull AdConfig adConfig) {
                adConfigAdapter.remove(adConfig);
                interstitialAdConfigStorage.remove(adConfig);
            }
        });

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adConfigAdapter);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK && data != null) {
            if (requestCode == REQUEST_CODE_CREATE_INTERSTITIAL_AD_CONFIG) {
                AdConfig newConfig = data.getParcelableExtra(CreateInterstitialAdConfigActivity.NEW_CONFIG);
                interstitialAdConfigStorage.add(newConfig);
                adConfigAdapter.addItem(newConfig);
            } else if (requestCode == REQUEST_CODE_EDIT_INTERSTITIAL_AD_CONFIG) {
                AdConfig oldConfig = data.getParcelableExtra(CreateInterstitialAdConfigActivity.OLD_CONFIG);
                AdConfig newConfig = data.getParcelableExtra(CreateInterstitialAdConfigActivity.NEW_CONFIG);
                interstitialAdConfigStorage.updateItem(oldConfig, newConfig);
                adConfigAdapter.updateItem(oldConfig, newConfig);
            }
        }
    }

    @NonNull
    private List<AdConfig> createDefaultInterstitialItemsList() {
        AdConfig defaultInterstitial = new AdConfig(getString(R.string.multi_ad_format_interstitial), TEST_PUBLISHER_ID, TEST_AD_SPACE_ID);

        List<AdConfig> items = new ArrayList<>();
        items.add(defaultInterstitial);

        return items;
    }
}
