package com.adpro.pixelshow.look

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.adpro.pixelshow.IAppLifecycle
import com.adpro.pixelshow.databinding.ActivityInitBinding
import com.adpro.pixelshow.util.TopBarUtils

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

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityInitBinding.inflate(layoutInflater)
        setContentView(binding.root)
        TPAdManager.init(
            this@InitActivity,
            IAppLifecycle.TAG,
            "746406D3D50E38A478DE9B06A397AB11",
            "19E48CAEDA743B891D65A26DA3CBE612",
            "9B349314944F58680891D1E522009012",
            "089E972A6D8EF6C2A4F4B0725BA1C312"
        ) {
            null
        }


        TopBarUtils.setStatusBar(this.window)
        TopBarUtils.setLightStatusBar(this.window, true)

        countDownTimer = TPAdManager.showWelcomeAd(
            this@InitActivity,
            time,
            { aLong ->
                val progressPercentage = (100 * aLong) / time
                val percentage = 100 - progressPercentage
                binding.progressbar.progress = percentage.toInt()
            },
            {
                binding.progressbar.progress = 100
                val intent = Intent(
                    this@InitActivity,
                    FirstSelActivity::class.java
                )
                startActivity(intent)
                finish()
            }
        )
        countDownTimer?.start()
    }

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

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