/*
 *    Copyright 2018-2019 adview.com, Inc.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.adview.mobile.javademo.activities.ads.inapp;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;

import com.adview.mobile.javademo.activities.ads.admob.AdMobDisplayInterstitialActivity;
import com.adview.sdk.api.data.AdUnitFormat;
import com.adview.sdk.api.exceptions.AdException;
import com.adview.sdk.api.rendering.InterstitialAdUnit;
import com.adview.sdk.api.rendering.listeners.InterstitialAdUnitListener;
import com.adview.mobile.javademo.activities.BaseAdActivity;

import java.util.EnumSet;

public class InAppDisplayInterstitialActivity extends BaseAdActivity {

    private static final String CONFIG_ID = "POSIDfg0muzt20z0y"; //official line

    private InterstitialAdUnit adUnit;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding.btnLoadAd.setVisibility(View.VISIBLE);
        binding.btnLoadAd.setEnabled(true);
        binding.btnShowAd.setVisibility(View.GONE);

        binding.btnLoadAd.setOnClickListener(v -> {
            binding.btnLoadAd.setEnabled(false);
            createAd();
        });
    }

    private void createAd() {
        if (adUnit != null) {
            adUnit.destroy();
        }

        adUnit = new InterstitialAdUnit(this, CONFIG_ID,
                EnumSet.of(AdUnitFormat.BANNER, AdUnitFormat.VIDEO));
        adUnit.setInterstitialAdUnitListener(new InterstitialAdUnitListener() {
            @Override
            public void onAdLoaded(InterstitialAdUnit interstitialAdUnit) {

                binding.btnLoadAd.setEnabled(false);
                binding.btnShowAd.setVisibility(View.VISIBLE);

                binding.btnShowAd.setOnClickListener(v1 -> {
                    adUnit.show();
                    binding.btnShowAd.setVisibility(View.GONE);
                });

            }

            @Override
            public void onAdDisplayed(InterstitialAdUnit interstitialAdUnit) {}

            @Override
            public void onAdFailed(InterstitialAdUnit interstitialAdUnit, AdException e) {}

            @Override
            public void onAdClicked(InterstitialAdUnit interstitialAdUnit) {
                Log.e("InAppDisplayInterstitial", "=== Ad clicked ===");
            }

            @Override
            public void onAdClosed(InterstitialAdUnit interstitialAdUnit) {
                Log.e("InAppDisplayInterstitial", "=== Ad closed ===");
                binding.btnLoadAd.setEnabled(true);
            }
        });
        adUnit.loadAd();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (adUnit != null) {
            adUnit.destroy();
        }
    }
}
