/*
 *    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.admob;

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

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.adview.sdk.admob.AdMobMediationInterstitialUtils;
import com.adview.sdk.admob.AdviewInterstitialAdapter;
import com.adview.sdk.api.data.AdUnitFormat;
import com.adview.sdk.api.mediation.MediationInterstitialAdUnit;
import com.adview.mobile.javademo.activities.BaseAdActivity;
import com.google.android.gms.ads.AdError;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.FullScreenContentCallback;
import com.google.android.gms.ads.LoadAdError;
import com.google.android.gms.ads.interstitial.InterstitialAd;
import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback;

import java.util.EnumSet;

public class AdMobDisplayInterstitialActivity extends BaseAdActivity {

    private static final String AD_UNIT_ID = "ca-app-pub-7283949697136772/5248357557";
    private static final String CONFIG_ID = "POSIDfg0muzt20z0y"; //official line

    private MediationInterstitialAdUnit adUnit;
    private InterstitialAd interstitialAd;

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

        createAd();
    }

    private void createAd() {
        binding.btnLoadAd.setVisibility(View.VISIBLE);
        binding.btnLoadAd.setEnabled(false);
        binding.btnShowAd.setVisibility(View.GONE);

        Bundle extras = new Bundle();
        AdMobMediationInterstitialUtils mediationUtils = new AdMobMediationInterstitialUtils(extras);

        adUnit = new MediationInterstitialAdUnit(
                this,
                CONFIG_ID,
                EnumSet.of(AdUnitFormat.BANNER, AdUnitFormat.VIDEO),
                mediationUtils
        );

        Log.d("AdMobInterstitial", "Starting fetchDemand immediately...");
        adUnit.fetchDemand(result -> {
            Log.d("AdMobInterstitial", "Fetch demand result: " + result);

            binding.btnLoadAd.setEnabled(true);
            binding.btnLoadAd.setOnClickListener(v -> {
                Log.d("AdMobInterstitial", "Button clicked! Loading AdMob...");
                binding.btnLoadAd.setEnabled(false);

                AdRequest request = new AdRequest.Builder()
                        .addNetworkExtrasBundle(AdviewInterstitialAdapter.class, extras)
                        .build();

                InterstitialAd.load(
                        this,
                        AD_UNIT_ID,
                        request,
                        new InterstitialAdLoadCallback() {
                            @Override
                            public void onAdLoaded(@NonNull InterstitialAd interstitial) {
                                Log.d("AdMobInterstitial", "==== on InterstitialAd loaded ====");
                                interstitialAd = interstitial;
                                interstitialAd.setFullScreenContentCallback(createFullScreenContentCallback());

                                binding.btnShowAd.setVisibility(View.VISIBLE);
                                binding.btnShowAd.setOnClickListener(v1 -> {
                                    interstitialAd.show(AdMobDisplayInterstitialActivity.this);
                                    binding.btnShowAd.setVisibility(View.GONE);
                                });
                            }

                            @Override
                            public void onAdFailedToLoad(@NonNull LoadAdError error) {
                                Log.d("AdMobInterstitial", "=== Failed to load ad: " + error + " ===");
                                interstitialAd = null;
                                binding.btnLoadAd.setEnabled(true);
                            }
                        });
            });
        });
    }

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

    protected FullScreenContentCallback createFullScreenContentCallback() {
        return new FullScreenContentCallback() {
            @Override
            public void onAdClicked() {
                Log.d("AdMobInterstitial", "==== onAdClicked ===");
            }

            @Override
            public void onAdImpression() {
                Log.d("AdMobInterstitial", "==== onAdImpression ===");
            }

            @Override
            public void onAdShowedFullScreenContent() {
                Log.d("AdMobInterstitial", "==== onAdShowedFullScreenContent ===");
            }

            @Override
            public void onAdDismissedFullScreenContent() {
                Log.d("AdMobInterstitial", "==== onAdDismissedFullScreenContent ===");
                createAd();
            }

            @Override
            public void onAdFailedToShowFullScreenContent(@NonNull AdError p0) {
                Log.d("AdMobInterstitial", "==== onAdFailedToShowFullScreenContent ===");
                binding.btnLoadAd.setEnabled(true);
            }
        };
    }
}
