package com.themes.keyboard.black.activity

import android.content.Intent
import android.os.Bundle
import android.os.CountDownTimer
import android.widget.ProgressBar
import androidx.appcompat.app.AppCompatActivity
import com.themes.keyboard.black.R
import com.themes.keyboard.black.topon.AdManager
import com.themes.keyboard.black.up.Http.makeGetRequest
import com.themes.keyboard.black.up.SaveUtils.isPost
import com.themes.keyboard.black.utils.Common
import kotlin.math.roundToInt


class SplashActivi : AppCompatActivity() {

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

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_splash)
        Common.initFullScreen(this,true)
        init()
        findView()
        val post = isPost
        if (!post) {
            makeGetRequest(this@SplashActivi)
            isPost = true
        }
    }

    private fun init() {
        timer = AdManager.showWelcomeAd(this@SplashActivi,countTime,{
            val percentage: Float = 100 - it.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()
    }


}