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

import android.os.Bundle
import android.util.Log
import com.adview.sdk.api.exceptions.AdException
import com.adview.sdk.api.rendering.RewardedAdUnit
import com.adview.sdk.api.rendering.listeners.RewardedAdUnitListener
import com.adview.mobile.adviewkotlindemo.activities.BaseAdActivity
import com.adview.sdk.rendering.interstitial.rewarded.Reward

class InAppVideoRewardedActivity : BaseAdActivity() {

    companion object {

        const val CONFIG_ID = "POSIDwog9g88wqy1g" //official line  for video
    }

    private var adUnit: RewardedAdUnit? = null

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

        createAd()
    }

    private fun createAd() {
        adUnit?.destroy()

        adUnit = RewardedAdUnit(this, CONFIG_ID)
        adUnit?.setRewardedAdUnitListener(object : RewardedAdUnitListener {
            override fun onAdLoaded(rewardedAdUnit: RewardedAdUnit?) {
                adUnit?.show()
            }

            override fun onAdDisplayed(rewardedAdUnit: RewardedAdUnit?) {}
            override fun onAdFailed(rewardedAdUnit: RewardedAdUnit?, exception: AdException?) {}
            override fun onAdClicked(rewardedAdUnit: RewardedAdUnit?) {}
            override fun onAdClosed(rewardedAdUnit: RewardedAdUnit?) {}
            override fun onUserEarnedReward(rewardedAdUnit: RewardedAdUnit?, reward: Reward?) {
                Log.d("InAppVideoRewarded", "User earned reward: $reward")
            }
        })
        adUnit?.loadAd()
    }


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

}
