/*
 *    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.os.Bundle
import android.util.Log
import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.LoadAdError
import com.google.android.gms.ads.rewarded.RewardedAd
import com.google.android.gms.ads.rewarded.RewardedAdLoadCallback
import com.adview.sdk.admob.AdMobMediationRewardedUtils
import com.adview.sdk.admob.AdviewRewardedAdapter
import com.adview.sdk.api.mediation.MediationRewardedVideoAdUnit
import com.adview.mobile.adviewkotlindemo.activities.BaseAdActivity

class AdMobVideoRewardedActivity : BaseAdActivity() {

    companion object {
        const val AD_UNIT_ID = "ca-app-pub-7283949697136772/2035607522" //admob app
        const val CONFIG_ID = "POSIDwog9g88wqy1g" //official line for video
    }

    private var rewardedAd: RewardedAd? = null
    private var adUnit: MediationRewardedVideoAdUnit? = null


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

        createAd()
    }

    private fun createAd() {
        val extras = Bundle()
        val request = AdRequest
            .Builder()
            .addNetworkExtrasBundle(AdviewRewardedAdapter::class.java, extras)
            .build()
        val mediationUtils = AdMobMediationRewardedUtils(extras)
        adUnit = MediationRewardedVideoAdUnit(
            this,
            CONFIG_ID,
            mediationUtils
        )

        adUnit?.fetchDemand {
            Log.d("Adview", "Fetch demand result: $it")

            RewardedAd.load(this, AD_UNIT_ID, request, object : RewardedAdLoadCallback() {
                override fun onAdLoaded(ad: RewardedAd) {
                    Log.d(TAG, "=== RewardedAd Ad was loaded.===")
                    rewardedAd = ad

                    rewardedAd?.setOnPaidEventListener {
                        Log.d(TAG, "[admob-app] User earned reward: $it ")
                    }
                    rewardedAd?.show(this@AdMobVideoRewardedActivity) {
                        Log.d(TAG, "=== User earned reward: ${it.amount} ${it.type} ===")
                    }
                }

                override fun onAdFailedToLoad(adError: LoadAdError) {
                    Log.e(TAG, " === Reward failed: ${adError.message} ===")
                    rewardedAd = null
                }
            })
        }

    }

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

}
