/*
 *    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.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.widget.FrameLayout;

import androidx.annotation.Nullable;

import com.adview.sdk.AdSize;
import com.adview.sdk.api.mediation.MediationBannerAdUnit;
import com.adview.mobile.javademo.activities.BaseAdActivity;
import com.applovin.mediation.MaxAd;
import com.applovin.mediation.MaxAdFormat;
import com.applovin.mediation.MaxAdViewAdListener;
import com.applovin.mediation.MaxError;
import com.applovin.mediation.adapters.adview.utils.MaxMediationBannerUtils;
import com.applovin.mediation.ads.MaxAdView;
import com.applovin.sdk.AppLovinSdkUtils;

public class AppLovinMaxDisplayBanner300x250Activity extends BaseAdActivity {

    private static final String TAG = "AppLovinMaxBanner";
    public static final String AD_UNIT_ID = "9b623a1f75be5eec"; //max unit
    private static final String CONFIG_ID = "POSIDn5n36e0fckrq"; //offical

    public static final int WIDTH = 300;
    public static final int HEIGHT = 250;

    private MaxAdView adView;
    private MediationBannerAdUnit adUnit;

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

    private void createAd() {
        adView = new MaxAdView(AD_UNIT_ID, MaxAdFormat.MREC, this);
        adView.setListener(new MaxAdViewAdListener() {
            @Override
            public void onAdLoadFailed(String adUnitId, MaxError error) {
                Log.d(TAG, "=== onAdLoadFailed(): " + error.getMessage() + " ===");
            }

            @Override
            public void onAdLoaded(MaxAd ad) {
                Log.d(TAG, "=== onAdLoaded() :" + ad.getRevenue() + "====");
            }

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

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

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

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

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

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

        adView.setBackgroundColor(Color.TRANSPARENT);
        adView.setLayoutParams(new FrameLayout.LayoutParams(
                AppLovinSdkUtils.dpToPx(this, WIDTH),
                AppLovinSdkUtils.dpToPx(this, HEIGHT),
                Gravity.CENTER
        ));

        adView.setLocalExtraParameter("CONFIG_ID", CONFIG_ID);
        adView.setLocalExtraParameter("WIDTH", WIDTH);
        adView.setLocalExtraParameter("HEIGHT", HEIGHT);

        getAdWrapperView().addView(adView);

        MaxMediationBannerUtils mediationUtils = new MaxMediationBannerUtils(adView);
        adUnit = new MediationBannerAdUnit(
                this,
                CONFIG_ID,
                new AdSize(WIDTH, HEIGHT),
                mediationUtils
        );

        adUnit.fetchDemand(resultCode -> {
            adView.loadAd();
        });
    }

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