/*
 *    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.os.Bundle
import android.util.Log
import android.view.View
import com.applovin.mediation.MaxAd
import com.applovin.mediation.MaxAdListener
import com.applovin.mediation.MaxError
import com.applovin.mediation.adapters.adview.utils.MaxMediationInterstitialUtils
import com.applovin.mediation.ads.MaxInterstitialAd
import com.adview.sdk.api.data.AdUnitFormat
import com.adview.sdk.api.mediation.MediationInterstitialAdUnit
import com.adview.mobile.adviewkotlindemo.activities.BaseAdActivity
import java.util.*

class AppLovinMaxDisplayInterstitialActivity : BaseAdActivity() {

    companion object {
        const val AD_UNIT_ID = "bcb16ea7ec27f20c" //max
        const val CONFIG_ID = "POSIDfg0muzt20z0y" //official line
    }

    private var maxInterstitialAd: MaxInterstitialAd? = null
    private var adUnit: MediationInterstitialAdUnit? = null


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

        createAd()
    }

    private fun createAd() {
        // load button
        binding.btnLoadAd.visibility = View.VISIBLE

        maxInterstitialAd = MaxInterstitialAd(AD_UNIT_ID, this)
        maxInterstitialAd?.setListener(object : MaxAdListener {
            override fun onAdLoaded(ad: MaxAd) {
                Log.d("AppLovinMaxInterstitial", "=== onAdLoaded() ===")
                
                // show button
                binding.btnShowAd.visibility = View.VISIBLE
                binding.btnShowAd.setOnClickListener {
                    maxInterstitialAd?.showAd(this@AppLovinMaxDisplayInterstitialActivity)
                    binding.btnShowAd.visibility = View.GONE
                }
            }

            override fun onAdDisplayed(ad: MaxAd) {
                Log.d("AppLovinMaxInterstitial", "=== onAdDisplayed() ===")

            }
            override fun onAdHidden(ad: MaxAd) {
                Log.d("AppLovinMaxInterstitial", "=== onAdHidden() ===")
                binding.btnLoadAd.isEnabled = true
            }
            override fun onAdClicked(ad: MaxAd) {
                Log.d("AppLovinMaxInterstitial", "=== onAdClicked() ===")
            }
            override fun onAdLoadFailed(adUnitId: String, error: MaxError) {
                Log.d("AppLovinMaxInterstitial", "=== onAdLoadFailed(): ${error.message} ===")
                binding.btnLoadAd.isEnabled = true
            }
            override fun onAdDisplayFailed(ad: MaxAd, error: MaxError) {
                binding.btnLoadAd.isEnabled = true
            }
        })

        val mediationUtils = MaxMediationInterstitialUtils(maxInterstitialAd)


        adUnit = MediationInterstitialAdUnit(
            this,
            CONFIG_ID,
            EnumSet.of(AdUnitFormat.BANNER, AdUnitFormat.VIDEO),
            mediationUtils
        )


        binding.btnLoadAd.setOnClickListener {
            binding.btnLoadAd.isEnabled = false // repeat avoid
            binding.btnShowAd.visibility = View.GONE
            adUnit?.fetchDemand {
                maxInterstitialAd?.loadAd()
            }
        }
    }


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

}
