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

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.Gravity
import android.view.View
import android.widget.FrameLayout
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
import com.adview.sdk.AdSize
import com.adview.sdk.api.mediation.MediationBannerAdUnit
import com.adview.mobile.adviewkotlindemo.activities.BaseAdActivity

class AppLovinMaxDisplayBanner320x50Activity : BaseAdActivity() {

    companion object {
        const val AD_UNIT_ID = "38808397200a852d" // max banner
        const val CONFIG_ID = "POSID0wbfijwzm430"; //official line

        const val WIDTH = 320
        const val HEIGHT = 50
    }

    private var adView: MaxAdView? = null
    private var adUnit: MediationBannerAdUnit? = null


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



    private fun createAd() {
        adView = MaxAdView(AD_UNIT_ID, this)
        adView?.setListener(object : MaxAdViewAdListener {
            override fun onAdLoadFailed(adUnitId: String, error: MaxError) {
                Log.d("AppLovinMaxBanner", "[max-app] onAdLoadFailed(): ${error.message}")
            }

            override fun onAdLoaded(ad: MaxAd) {
                Log.d("AppLovinMaxBanner", "== [max-app] onAdLoaded(): revene = "
                        + ad?.revenue.toString()  + " ==")
            }
            override fun onAdHidden(ad: MaxAd) {
                Log.d("AppLovinMaxBanner", "======= [max-app]onAdHidden ======")
            }
            override fun onAdClicked(ad: MaxAd) {
                Log.d("AppLovinMaxBanner", "======= [max-app]onAdClicked ======")
            }
            override fun onAdExpanded(ad: MaxAd) {
                Log.d("AppLovinMaxBanner", "======= [max-app]onAdExpanded ======")
            }
            override fun onAdCollapsed(ad: MaxAd) {
                Log.d("AppLovinMaxBanner", "======= [max-app]onAdCollapsed ======")
            }
            override fun onAdDisplayed(ad: MaxAd) {
                Log.d("AppLovinMaxBanner", "======= [max-app]onAdDisplayed : revene = "
                        + ad?.revenue.toString() +"======")
            }
            override fun onAdDisplayFailed(ad: MaxAd, error: MaxError) {
                Log.d("AppLovinMaxBanner", "======= [max-app]onAdDisplayFailed : ${error.message}======")
            }
        })
        
        adView?.layoutParams = FrameLayout.LayoutParams(
            AppLovinSdkUtils.dpToPx(this, WIDTH),
            AppLovinSdkUtils.dpToPx(this, HEIGHT),
            Gravity.CENTER
        )

        // max fetch-ondemand through Local Parameter transfer Config ID
        adView?.setLocalExtraParameter("CONFIG_ID", CONFIG_ID)
        // max fetch-ondemand through pass width and height
        adView?.setLocalExtraParameter("WIDTH", WIDTH)
        adView?.setLocalExtraParameter("HEIGHT", HEIGHT)

        adWrapperView.addView(adView)

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

        adUnit?.fetchDemand {
            adView?.loadAd()
        }
    }

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

}
