package com.keyboard.keyboardtheme.skin.act

import android.content.Intent
import android.os.Bundle
import android.os.CountDownTimer
import android.widget.ProgressBar
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.ad.toponlibrary.AdManager
import com.keyboard.keyboardtheme.skin.R

import com.keyboard.keyboardtheme.skin.utils.Common
import kotlin.math.roundToInt

class ActSplashWel : AppCompatActivity() {

    private lateinit var progressBar: ProgressBar
    private var countTime = 2000L
    private lateinit var timer: CountDownTimer
    private lateinit var tvProgress:TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.act_splash_wel)
        Common.initFullScreen(this,true)
        progressBar = findViewById<ProgressBar>(R.id.progress)
        tvProgress = findViewById(R.id.tv_progress)
        init()

    }

    private fun init() {

        timer = AdManager.showWelcomeAd(this@ActSplashWel,countTime,{
            val percentage: Float = 100 - it.toFloat() / countTime * 100
            val round = percentage.roundToInt()
            progressBar.progress = round
            val format =getString(R.string.tv_wel_progress, round)
            tvProgress.text = format
        }){
            progressBar.progress = 100
            tvProgress.text = getString(R.string.tv_wel_progress, 100)
            toHome()
        }

        timer.start()

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

    override fun onDestroy() {
        super.onDestroy()
        timer.cancel()
    }


}