/*
 *    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.adviewkotlindemo.activities.ads.admob

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
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
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.adviewkotlindemo.activities.BaseAdActivity
import com.adview.mobile.adviewkotlindemo.activities.ads.inapp.InAppDisplayBanner320x50Activity
import com.adview.sdk.AdviewMobile

class AdMobDisplayBanner320x50Activity : BaseAdActivity() {

    companion object {
        const val AD_UNIT_ID = "ca-app-pub-7283949697136772/2157776897" //wilder 2026 for adview-admob
        const val CONFIG_ID = "POSID0wbfijwzm430"; //official
        const val WIDTH = 320
        const val HEIGHT = 50

    }
    private var configId = CONFIG_ID

    private var bannerView: AdView? = null
    private var adUnit: MediationBannerAdUnit? = null


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        createAd()
    }

    private fun createAd() {
        /** Google recommends put activity for mediation ad networks */
        bannerView = AdView(this)
        bannerView?.setAdSize(com.google.android.gms.ads.AdSize.BANNER)
        bannerView?.adUnitId = AD_UNIT_ID

        bannerView?.adListener = object : AdListener() {
            override fun onAdLoaded() {
                Log.d("AdMobBanner", "[admob-app] Won ad network: ${bannerView?.responseInfo?.mediationAdapterClassName}")
            }

            override fun onAdFailedToLoad(p0: LoadAdError) {
                Log.d("AdMobBanner", "[admob-app] Failed to load ad $p0")
            }

            override fun onAdImpression() {
                Log.d("AdMobBanner", "[admob-app] onAdImpression")
            }

            override fun onAdClicked() {
                Log.d("AdMobBanner", "[admob-app] onAdClicked")
            }

            override fun onAdClosed() {
                Log.d("AdMobBanner", "[admob-app] onAdClosed")
            }

            override fun onAdOpened() {
                Log.d("AdMobBanner", "[admob-app] onAdOpened")
            }
        }
        adWrapperView.addView(bannerView)

        val extras = Bundle()
        // admob fetch-ondemand through Local Parameter transfer Config ID
        extras.putString("adview_config_id", configId)
        // admob fetch-ondemand through Local Parameter transfer width & height
        extras.putInt("adview_width", WIDTH)
        extras.putInt("adview_height", HEIGHT)

        val request = AdRequest
            .Builder()
            .addNetworkExtrasBundle(AdviewBannerAdapter::class.java, extras)
            .build()
        val mediationUtils = AdMobMediationBannerUtils(extras, bannerView)

        adUnit = MediationBannerAdUnit(
            this,
            configId,
            AdSize(WIDTH, HEIGHT),
            mediationUtils
        )

        adUnit?.fetchDemand { _ ->
            bannerView?.loadAd(request)
        }

    }

    override fun onDestroy() {
        super.onDestroy()
        bannerView?.destroy()
        adUnit?.destroy()
    }

}
