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

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

import androidx.annotation.Nullable;

import com.adview.sdk.api.data.AdUnitFormat;
import com.adview.sdk.api.mediation.MediationInterstitialAdUnit;
import com.adview.mobile.javademo.activities.BaseAdActivity;
import com.applovin.mediation.MaxAd;
import com.applovin.mediation.MaxAdListener;
import com.applovin.mediation.MaxError;
import com.applovin.mediation.adapters.adview.utils.MaxMediationInterstitialUtils;
import com.applovin.mediation.ads.MaxInterstitialAd;

import java.util.EnumSet;

public class AppLovinMaxDisplayInterstitialActivity extends BaseAdActivity {

    private static final String TAG = "AppLovinMaxInterstitial";
    public static final String AD_UNIT_ID = "7b60b8766d865726"; //max unit
    public static final String CONFIG_ID = "POSIDfg0muzt20z0y"; //official line

    private MaxInterstitialAd maxInterstitialAd;
    private MediationInterstitialAdUnit adUnit;

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

    private void createAd() {
        binding.btnLoadAd.setVisibility(View.VISIBLE);

        maxInterstitialAd = new MaxInterstitialAd(AD_UNIT_ID, this);
        maxInterstitialAd.setListener(new MaxAdListener() {
            @Override
            public void onAdLoaded(MaxAd ad) {
                Log.d(TAG, "=== onAdLoaded() ===");
                binding.btnShowAd.setVisibility(View.VISIBLE);
                binding.btnShowAd.setOnClickListener(v -> {
                    maxInterstitialAd.showAd(AppLovinMaxDisplayInterstitialActivity.this);
                    binding.btnShowAd.setVisibility(View.GONE);
                });
            }

            @Override
            public void onAdDisplayed(MaxAd ad) {
                Log.d(TAG, "=== onAdDisplayed() ===");
            }

            @Override
            public void onAdHidden(MaxAd ad) {
                Log.d(TAG, "=== onAdHidden() ===");
                binding.btnLoadAd.setEnabled(true);
            }

            @Override
            public void onAdClicked(MaxAd ad) {
                Log.d(TAG, "=== onAdClicked() ===");
            }

            @Override
            public void onAdLoadFailed(String adUnitId, MaxError error) {
                Log.d(TAG, "=== onAdLoadFailed(): " + error.getMessage() + " ===");
                binding.btnLoadAd.setEnabled(true);
            }

            @Override
            public void onAdDisplayFailed(MaxAd ad, MaxError error) {
                Log.d(TAG, "=== onAdDisplayFailed(): " + error.getMessage() + " ===");
                binding.btnLoadAd.setEnabled(true);
            }
        });

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

        binding.btnLoadAd.setOnClickListener(v -> {
            binding.btnLoadAd.setEnabled(false);
            binding.btnShowAd.setVisibility(View.GONE);
            adUnit.fetchDemand(resultCode -> {
                maxInterstitialAd.loadAd();
            });
        });
    }

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