package com.keyboard.skinning.cool.activity

import android.content.Intent
import android.os.Bundle
import android.os.CountDownTimer
import android.widget.ProgressBar
import androidx.appcompat.app.AppCompatActivity
import com.keyboard.skinning.cool.R
import com.keyboard.skinning.cool.ad.AdManager
import com.keyboard.skinning.cool.tools.Helper
import kotlin.math.roundToInt

class WelcomeActivity : AppCompatActivity() {

    private lateinit var progressBar: ProgressBar
    private var countTime = 12000L
    private lateinit var timer: CountDownTimer

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_welcome)
        Helper.initFullScreen(this, false)
        AdManager.loadAllAd()
        init()
        findView()
    }

    private fun init() {

        timer = AdManager.showWelcomeAd(this@WelcomeActivity, countTime, { millisUntilFinished ->
            val percentage: Float = 100 - millisUntilFinished.toFloat() / countTime * 100
            val round = percentage.roundToInt()
            progressBar.progress = round
        }) {
            progressBar.progress = 100
            toHome()
        }
        timer.start()

    }


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


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

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


}