package com.sketch.ar.artdrawing

import android.content.Intent
import android.os.Bundle
import android.os.CountDownTimer
import android.widget.ProgressBar
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.ad.toponlibrary.AdManager
import com.sketch.ar.artdrawing.main.MainCategoryActivity


class FirstOpenActivity : AppCompatActivity() {
    private val countTime = 12000L
    private var countDownTimer: CountDownTimer? = null

    private lateinit var textView: TextView
    private lateinit var welpb: ProgressBar
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_first_open)
        textView = findViewById<TextView>(R.id.text_progress)
        welpb = findViewById<ProgressBar>(R.id.load_pb)

        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        countDownTimer =
            AdManager.showWelcomeAd(this@FirstOpenActivity, countTime, { millisUntilFinished ->
                val progressPercentage: Int = ((100 * millisUntilFinished) / countTime).toInt()
                val progressInt = 100 - progressPercentage
                textView.text = getString(R.string.welcome_text_progress, progressInt)
                welpb.progress = progressInt
            }) {
                toMainAct()
            }

        countDownTimer?.start()

    }

    private fun toMainAct() {
        textView.text = getString(R.string.welcome_text_progress, 100)
        welpb.progress = 100
        startActivity(Intent(this, MainCategoryActivity::class.java))
        finish()
        countDownTimer?.cancel()
    }

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