package com.smaato.sdk.demoapp.banner;

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.banner.ad.BannerAdSize;
import com.smaato.sdk.demoapp.AdConfig;
import com.smaato.sdk.demoapp.AdConfigAdapter;
import com.smaato.sdk.demoapp.R;
import com.smaato.sdk.demoapp.interstitial.CreateInterstitialAdConfigActivity;

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

public class BannerAdsActivity extends AppCompatActivity {

    private static final String TEST_PUBLISHER_ID = "0";
    private static final String TEST_AD_SPACE_ID = "0";

    private static final int REQUEST_CODE_CREATE_BANNER_AD_CONFIG = 1;
    private static final int REQUEST_CODE_EDIT_BANNER_AD_CONFIG = 2;

    @NonNull
    private AdConfigAdapter adConfigAdapter;
    @NonNull
    private BannerAdConfigStorage bannerAdConfigStorage;

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

        findViewById(R.id.create_custom_banner_ad_config_button).setOnClickListener(v -> {
            Intent intent = new Intent(this, CreateBannerAdConfigActivity.class);
            startActivityForResult(intent, REQUEST_CODE_CREATE_BANNER_AD_CONFIG);
        });

        bannerAdConfigStorage = new BannerAdConfigStorage(this);

        List<AdConfig> adConfigs = createDefaultBannerItemsList();
        int numberOfDefaultItems = adConfigs.size();
        adConfigs.addAll(bannerAdConfigStorage.get());

        adConfigAdapter = new AdConfigAdapter(adConfigs, numberOfDefaultItems);
        adConfigAdapter.setItemClickListener(new AdConfigAdapter.ItemClickListener() {
            @Override
            public void onItemClick(@NonNull AdConfig adConfig) {
                startActivity(ShowBannerAdsActivity.createIntent(BannerAdsActivity.this, (BannerAdConfig) adConfig));
            }

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

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

        RecyclerView recyclerView = findViewById(R.id.banner_recycler_view);
        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_BANNER_AD_CONFIG) {
                BannerAdConfig newConfig = data.getParcelableExtra(CreateBannerAdConfigActivity.NEW_CONFIG);
                bannerAdConfigStorage.add(newConfig);
                adConfigAdapter.addItem(newConfig);
            } else if (requestCode == REQUEST_CODE_EDIT_BANNER_AD_CONFIG) {
                BannerAdConfig oldConfig = data.getParcelableExtra(CreateInterstitialAdConfigActivity.OLD_CONFIG);
                BannerAdConfig newConfig = data.getParcelableExtra(CreateInterstitialAdConfigActivity.NEW_CONFIG);
                bannerAdConfigStorage.updateItem(oldConfig, newConfig);
                adConfigAdapter.updateItem(oldConfig, newConfig);
            }
        }
    }

    @NonNull
    private List<AdConfig> createDefaultBannerItemsList() {
        BannerAdConfig xxLarge = new BannerAdConfig(getString(R.string.xx_large), TEST_PUBLISHER_ID, TEST_AD_SPACE_ID, BannerAdSize.XX_LARGE_320x50);
        BannerAdConfig mediumRectangle = new BannerAdConfig(getString(R.string.medium_rectancle), TEST_PUBLISHER_ID, TEST_AD_SPACE_ID, BannerAdSize.MEDIUM_RECTANGLE_300x250);
        BannerAdConfig leaderBoard = new BannerAdConfig(getString(R.string.leaderBoard), TEST_PUBLISHER_ID, TEST_AD_SPACE_ID, BannerAdSize.LEADERBOARD_728x90);
        BannerAdConfig skyScraper = new BannerAdConfig(getString(R.string.skyScraper), TEST_PUBLISHER_ID, TEST_AD_SPACE_ID, BannerAdSize.SKYSCRAPER_120x600);

        List<AdConfig> items = new ArrayList<>();
        items.add(xxLarge);
        items.add(mediumRectangle);
        items.add(leaderBoard);
        items.add(skyScraper);

        return items;
    }
}
