package com.design.mischiefgo.ui

import android.content.Intent
import android.os.Bundle
import android.os.CountDownTimer
import android.os.Handler
import androidx.appcompat.app.AppCompatActivity
import com.ad.tradpluslibrary.TPAdManager
import com.design.mischiefgo.NewApp
import com.design.mischiefgo.databinding.ActivityAdShowBinding
import com.design.mischiefgo.ut.EvenTopUtils

class AdShowActivity : AppCompatActivity() {
    private lateinit var binding : ActivityAdShowBinding
    private var handler: Handler = Handler()
    private var countDownTimer: CountDownTimer? = null
    private var time=14000L

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        EvenTopUtils.setStatusBar(this.window)
        EvenTopUtils.setLightStatusBar(this.window, true)
        binding = ActivityAdShowBinding.inflate(layoutInflater)
        setContentView(binding.root)
        TPAdManager.init(
            this@AdShowActivity,
            NewApp.TAG,
            "17EDDAE88015DE3BF7D6D4E436B18111",
            "F9C9551815F75E1944178419F1517C12",
            "61BF5E6CA907277EBA5EFC3AFC429012",
            "85C9DD1C2A714986A3627007150E9312"
        ) {
            null
        }
        countDownTimer = TPAdManager.showWelcomeAd(
            this@AdShowActivity,
            time,
            { aLong ->
                val progressPercentage = (100 * aLong) / time
                val percentage = 100 - progressPercentage
                binding.loadingPb.progress = percentage.toInt()
            },
            {
                binding.loadingPb.progress = 100
                val intent= Intent(
                    this@AdShowActivity,
                    NavigationActivity::class.java
                )
                startActivity(intent)
                finish()
            }
        )
        countDownTimer?.start()
    }

    override fun onDestroy() {
        super.onDestroy()
        handler.removeCallbacksAndMessages(null)

        if (countDownTimer != null) {
            countDownTimer!!.cancel()
        }
    }
}