/*
 *    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.Gravity;
import android.view.View;
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.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 AppLovinMaxDisplayBanner320x50Activity extends BaseAdActivity {

    private static final String TAG = "AppLovinMaxBanner";
    public static final String AD_UNIT_ID = "83ba07c4bff59a5d"; //max unit
    public static final String CONFIG_ID = "POSID0wbfijwzm430"; //offical

    public static final int WIDTH = 320;
    public static final int HEIGHT = 50;

    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, this);
        adView.setListener(new MaxAdViewAdListener() {
            @Override
            public void onAdLoadFailed(String adUnitId, MaxError error) {
                Log.d(TAG, "[max-app] onAdLoadFailed(): " + error.getMessage());
            }

            @Override
            public void onAdLoaded(MaxAd ad) {
                Log.d(TAG, "== [max-app] onAdLoaded(): revenue = " + ad.getRevenue() + " ==");
            }

            @Override
            public void onAdHidden(MaxAd ad) {
                Log.d(TAG, "======= [max-app]onAdHidden ======");
            }

            @Override
            public void onAdClicked(MaxAd ad) {
                Log.d(TAG, "======= [max-app]onAdClicked ======");
            }

            @Override
            public void onAdExpanded(MaxAd ad) {
                Log.d(TAG, "======= [max-app]onAdExpanded ======");
            }

            @Override
            public void onAdCollapsed(MaxAd ad) {
                Log.d(TAG, "======= [max-app]onAdCollapsed ======");
            }

            @Override
            public void onAdDisplayed(MaxAd ad) {
                Log.d(TAG, "======= [max-app]onAdDisplayed : revenue = " + ad.getRevenue() + "======");
            }

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

        int widthPx = AppLovinSdkUtils.dpToPx(this, WIDTH);
        int heightPx = AppLovinSdkUtils.dpToPx(this, HEIGHT);
        adView.setLayoutParams(new FrameLayout.LayoutParams(widthPx, heightPx, 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();
        }
    }
}
