package com.apps.pixwall.view

import android.content.Intent
import android.os.Bundle
import android.os.CountDownTimer
import android.os.Handler
import androidx.appcompat.app.AppCompatActivity
import com.apps.pixwall.databinding.ActivityLoadBinding

class LoadActivity : AppCompatActivity() {
    lateinit var binding: ActivityLoadBinding
    private var handler: Handler = Handler()
    private var countDownTimer: CountDownTimer? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityLoadBinding.inflate(layoutInflater)
        setContentView(binding.root)
        handler.postDelayed({
            val intent= Intent(
                this@LoadActivity,
                MainActivity::class.java
            )
            startActivity(intent)
            finish()
        }, 4000)
        countDownTimer = object : CountDownTimer(4000, 100) {
            override fun onTick(millisUntilFinished: Long) {
                val progress = ((4000 - millisUntilFinished) / 4000f * 100).toInt()
                binding.progressbar.progress = progress
            }

            override fun onFinish() {
                binding.progressbar.progress = 100
            }
        }.start()
    }

    override fun onDestroy() {
        super.onDestroy()
        handler.removeCallbacksAndMessages(null)

        if (countDownTimer != null) {
            countDownTimer!!.cancel()
        }
    }
}