package com.input.board.appskey.keyboard.viewUi

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.ImageView
import android.widget.ProgressBar
import com.applovin.mediation.MaxAd
import com.applovin.mediation.MaxAdListener
import com.applovin.mediation.MaxError
import com.applovin.mediation.ads.MaxInterstitialAd
import com.input.board.appskey.keyboard.R
import com.input.board.appskey.keyboard.customer.IntersitialAd
import java.util.Timer
import java.util.TimerTask

class NovaActivity : AppCompatActivity() ,MaxAdListener{

    private lateinit var progressBar: ProgressBar
    private lateinit var novaIm: ImageView
    private var tim = 15
    private lateinit var myTimer: Timer

    private  var isShowInterAd = false

    private lateinit var mAds:List<MaxInterstitialAd>
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_nova)
        mAds = IntersitialAd.loadInterAd(this)
        findView()
        myTimer = Timer()
        count()
    }

    private fun findView() {
        novaIm = findViewById(R.id.nova_picture)
        progressBar = findViewById<ProgressBar>(R.id.nova_progress)
    }

    private fun count(){
        myTimer.schedule(object :TimerTask(){
            override fun run() {
                tim -= 1
                IntersitialAd.showInterAd(mAds)?.let {
                    isShowInterAd = true
                    myTimer.cancel()
                    it.showAd()
                    it.setListener(this@NovaActivity)
                }
                if(tim == 0){
                    myTimer.cancel()
                    if(!isShowInterAd){
                        toHome()
                    }

                }
            }

        },0L,1000L)
    }

    private fun toHome(){
        startActivity(Intent(this,HomeActivity::class.java))
    }

    override fun onAdLoaded(p0: MaxAd) {

    }

    override fun onAdDisplayed(p0: MaxAd) {
        IntersitialAd.loadInterAd(this)
    }

    override fun onAdHidden(p0: MaxAd) {
        toHome()
    }

    override fun onAdClicked(p0: MaxAd) {

    }

    override fun onAdLoadFailed(p0: String, p1: MaxError) {

    }

    override fun onAdDisplayFailed(p0: MaxAd, p1: MaxError) {

    }


}