/*
 *    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 androidx.annotation.Nullable;

import com.adview.sdk.AdSize;
import com.adview.sdk.admob.AdMobMediationBannerUtils;
import com.adview.sdk.admob.AdviewBannerAdapter;
import com.adview.sdk.api.mediation.MediationBannerAdUnit;
import com.adview.mobile.javademo.activities.BaseAdActivity;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.LoadAdError;

public class AdMobDisplayBanner300x250Activity extends BaseAdActivity {

    private static final String AD_UNIT_ID = "ca-app-pub-7283949697136772/2157776897";
    private static final String CONFIG_ID = "POSIDn5n36e0fckrq"; //official
    private static final int WIDTH = 300;
    private static final int HEIGHT = 250;

    private AdView bannerView;
    private MediationBannerAdUnit adUnit;

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

        createAd();
    }

    private void createAd() {
        bannerView = new AdView(this);
        bannerView.setAdSize(com.google.android.gms.ads.AdSize.MEDIUM_RECTANGLE);
        bannerView.setAdUnitId(AD_UNIT_ID);

        bannerView.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                Log.d("AdMobBanner", "[admob-app] Won ad network: " + (bannerView.getResponseInfo() != null ? bannerView.getResponseInfo().getMediationAdapterClassName() : "unknown"));
            }

            @Override
            public void onAdFailedToLoad(LoadAdError loadAdError) {
                Log.d("AdMobBanner", "[admob-app] Failed to load ad " + loadAdError);
            }

            @Override
            public void onAdImpression() {
                Log.d("AdMobBanner", "[admob-app] onAdImpression");
            }

            @Override
            public void onAdClicked() {
                Log.d("AdMobBanner", "[admob-app] onAdClicked");
            }

            @Override
            public void onAdClosed() {
                Log.d("AdMobBanner", "[admob-app] onAdClosed");
            }

            @Override
            public void onAdOpened() {
                Log.d("AdMobBanner", "[admob-app] onAdOpened");
            }
        });
        getAdWrapperView().addView(bannerView);

        Bundle extras = new Bundle();
        extras.putString("adview_config_id", CONFIG_ID);
        extras.putInt("adview_width", WIDTH);
        extras.putInt("adview_height", HEIGHT);

        AdRequest request = new AdRequest.Builder()
                .addNetworkExtrasBundle(AdviewBannerAdapter.class, extras)
                .build();
        AdMobMediationBannerUtils mediationUtils = new AdMobMediationBannerUtils(extras, bannerView);

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

        adUnit.fetchDemand(resultCode -> bannerView.loadAd(request));
    }

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