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

class AppLovinMaxDisplayBanner300x250Activity : BaseAdActivity() {

    companion object {
        const val AD_UNIT_ID = "579d037ca3a82fd6" //max adunit
        const val CONFIG_ID = "POSIDn5n36e0fckrq" //official line

        const val WIDTH = 300
        const val HEIGHT = 250
    }

    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, MaxAdFormat.MREC, this)
        adView?.setListener(object : MaxAdViewAdListener {
            override fun onAdLoadFailed(adUnitId: String, error: MaxError) {
                Log.d("AppLovinMaxBanner", "=== onAdLoadFailed(): ${error.message} ===")
            }

            override fun onAdLoaded(ad: MaxAd) {
                Log.d("AppLovinMaxBanner", "=== onAdLoaded() :"
                        + ad?.revenue.toString() + "====")
            }
            override fun onAdHidden(ad: MaxAd) {
                Log.d("AppLovinMaxBanner", "=== onAdHidden() ===")
            }
            override fun onAdClicked(ad: MaxAd) {
                Log.d("AppLovinMaxBanner", "=== onAdClicked() ===")
            }
            override fun onAdExpanded(ad: MaxAd) {
                Log.d("AppLovinMaxBanner", "=== onAdExpanded() ===")
            }
            override fun onAdCollapsed(ad: MaxAd) {
                Log.d("AppLovinMaxBanner", "=== onAdCollapsed() ===")
            }
            override fun onAdDisplayed(ad: MaxAd) {
                Log.d("AppLovinMaxBanner", "=== onAdDisplayed() :"
                        +  ad?.revenue.toString() + "===")
            }
            override fun onAdDisplayFailed(ad: MaxAd, error: MaxError) {
                Log.d("AppLovinMaxBanner", "=== onAdDisplayFailed(): ${error.message} ===")
            }
        })
        adView?.setBackgroundColor(Color.TRANSPARENT)
        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 Local Parameter transfer width & 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()
    }

}
