package com.sunny.app.soft.timberkeyboardnew.ui.activity

import android.content.Intent
import android.os.CountDownTimer
import android.view.View
import com.sunny.app.soft.timberkeyboardnew.databinding.ActivityLoadingBinding
import com.sunny.app.soft.timberkeyboardnew.topon.AdManager
import kotlin.math.roundToInt

class LoadingActivity : BaseActivity() {

    private lateinit var binding: ActivityLoadingBinding

    private  var countDownTimer: CountDownTimer?= null

    private val time = 1000L

    override fun loadAd(): Boolean = false
    override fun setRootView(): View {
        binding = ActivityLoadingBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun onDestroy() {
        super.onDestroy()
        countDownTimer?.cancel()
    }

    override fun initView() {
        super.initView()

        countDownTimer =   AdManager.showWelcomeAd(this@LoadingActivity, time, { millisUntilFinished ->
            val percentage: Float = 100 - millisUntilFinished.toFloat() / time * 100
            val round = percentage.roundToInt()
            binding.progressbar.progress = round
            if (AdManager.place1LoadFail && AdManager.place2LoadFail && AdManager.place3LoadFail) {
                countDownTimer?.cancel()
                intoMainActivity()
            }
        }, {
            intoMainActivity()
        })
        countDownTimer?.start()
    }


    private fun intoMainActivity() {
        val intent = Intent(this, MainActivity::class.java)
        startActivity(intent)
        finish()
    }

}