/*
 *    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.MaxError
import com.applovin.mediation.MaxReward
import com.applovin.mediation.MaxRewardedAdListener
import com.applovin.mediation.adapters.adview.utils.MaxMediationRewardedUtils
import com.applovin.mediation.ads.MaxRewardedAd
import com.adview.sdk.api.mediation.MediationRewardedVideoAdUnit
import com.adview.mobile.adviewkotlindemo.activities.BaseAdActivity

class AppLovinMaxVideoRewardedActivity : BaseAdActivity() {

    companion object {
        const val AD_UNIT_ID = "a27b889b2b72e38a" //max id
        const val CONFIG_ID = "POSIDwog9g88wqy1g" //official line for video
    }

    private var maxRewardedAd: MaxRewardedAd? = null
    private var adUnit: MediationRewardedVideoAdUnit? = null


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

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

        maxRewardedAd = MaxRewardedAd.getInstance(AD_UNIT_ID, this)
        maxRewardedAd?.setListener(object : MaxRewardedAdListener {
            override fun onAdLoaded(ad: MaxAd) {
                Log.d("AdExample", "=== RewardedAd Ad was loaded: "
                        + ad?.revenue.toString() +
                        " ===== ")

                // 加载成功后显示展示按钮并设置点击监听
                binding.btnShowAd.visibility = View.VISIBLE
                binding.btnShowAd.setOnClickListener {
                    maxRewardedAd?.showAd()
                    binding.btnShowAd.visibility = View.GONE
                }
            }

            override fun onAdDisplayed(ad: MaxAd) {
                Log.d("AdExample", "=== RewardedAd Ad was displayed.==")
            }
            override fun onAdHidden(ad: MaxAd) {
                Log.d("AdExample", "=== RewardedAd Ad was hidden.==")
                binding.btnLoadAd.isEnabled = true // 广告关闭后允许重新加载
            }
            override fun onAdClicked(ad: MaxAd) {
                Log.d("AdExample", "=== RewardedAd Ad was clicked.==")
            }
            override fun onAdLoadFailed(adUnitId: String, error: MaxError) {
                Log.e("AdExample", "=== Reward failed: ${error.message} ===")
                binding.btnLoadAd.isEnabled = true // 失败后允许重新加载
            }
            override fun onAdDisplayFailed(ad: MaxAd, error: MaxError) {
                Log.e("AdExample", "=== Reward display failed: ${error.message} ===")
                binding.btnLoadAd.isEnabled = true
            }
            override fun onUserRewarded(ad: MaxAd, reward: MaxReward) {
                Log.d("AdExample", "=== User earned reward: ${reward.label}  ${reward.amount} ===")
            }
        })

        val mediationUtils = MaxMediationRewardedUtils(maxRewardedAd)
        adUnit = MediationRewardedVideoAdUnit(
            this,
            CONFIG_ID,
            mediationUtils
        )

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


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

}
