/*
 *    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 android.view.View
import com.adview.sdk.api.data.AdUnitFormat
import com.adview.sdk.api.exceptions.AdException
import com.adview.sdk.api.rendering.InterstitialAdUnit
import com.adview.sdk.api.rendering.listeners.InterstitialAdUnitListener
import com.adview.mobile.adviewkotlindemo.activities.BaseAdActivity
import java.util.*

class InAppDisplayInterstitialActivity : BaseAdActivity() {

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

    private var adUnit: InterstitialAdUnit? = null


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

        createAd()
    }

    private fun createAd() {

        // button load
        binding.btnLoadAd.visibility = View.VISIBLE
        binding.btnLoadAd.isEnabled = true
        binding.btnShowAd.visibility = View.GONE

        binding.btnLoadAd.setOnClickListener {
            binding.btnLoadAd.isEnabled = false
            adUnit?.loadAd()
        }

        adUnit?.destroy()

        adUnit = InterstitialAdUnit(this, CONFIG_ID,
            EnumSet.of(AdUnitFormat.BANNER, AdUnitFormat.VIDEO))
        adUnit?.setInterstitialAdUnitListener(object : InterstitialAdUnitListener {
            override fun onAdLoaded(interstitialAdUnit: InterstitialAdUnit?) {
                binding.btnLoadAd.isEnabled = true
                binding.btnShowAd.visibility = View.VISIBLE
                binding.btnShowAd.setOnClickListener {
                    binding.btnLoadAd.isEnabled = false
                    binding.btnShowAd.visibility = View.GONE
                    adUnit?.show()
                }
            }

            override fun onAdDisplayed(interstitialAdUnit: InterstitialAdUnit?) {}
            override fun onAdFailed(interstitialAdUnit: InterstitialAdUnit?, e: AdException?) {
                binding.btnLoadAd.isEnabled = true
            }
            override fun onAdClicked(interstitialAdUnit: InterstitialAdUnit?) {
                Log.e("InAppDisplayInterstitial", "=== Ad clicked ===")
            }
            override fun onAdClosed(interstitialAdUnit: InterstitialAdUnit?) {
                Log.e("InAppDisplayInterstitial", "=== Ad closed ===")
                binding.btnLoadAd.isEnabled = true
            }
        })

    }


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

}
