package com.keyboard.journey

import android.content.Intent
import android.os.Bundle
import android.os.CountDownTimer
import androidx.appcompat.app.AppCompatActivity
import com.gyf.immersionbar.ktx.immersionBar
import com.keyboard.journey.databinding.JourneyStartActivityBinding
import com.keyboard.journey.topon.AdManager
import org.libpag.PAGFile

class JourneyStartActivity : AppCompatActivity() {

        private val SPLASH_TIME_OUT: Long = 11000


    private lateinit var binding: JourneyStartActivityBinding
    private lateinit var timer:CountDownTimer


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = JourneyStartActivityBinding.inflate(layoutInflater)
        setContentView(binding.root)
        immersionBar {
            fullScreen(true)
            statusBarDarkFont(false)
        }

         timer = AdManager.showWelcomeAd(this@JourneyStartActivity, SPLASH_TIME_OUT) {
            startMain()
        }
        timer.start()
        loadingPlay()

    }



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

    private fun loadingPlay() {
        val pagFile = PAGFile.Load(assets, "loading.pag")
        binding.pagView.composition = pagFile
        binding.pagView.setRepeatCount(0)
        binding.pagView.play()
    }

    private fun loadingClose() {
        if (binding.pagView.isPlaying) {
            binding.pagView.stop()
        }
    }

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

}