package com.keyboard.craft.activity

import android.content.Intent
import android.os.Bundle
import android.os.CountDownTimer
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.gyf.immersionbar.ktx.immersionBar
import com.keyboard.craft.ad.WelComManager
import com.keyboard.craft.databinding.UiSplashActivityBinding
import com.keyboard.craft.util.upload.Http.makeGetRequest
import com.keyboard.craft.util.upload.SaveUtils.isPost


class SplashActivity : AppCompatActivity() {

    private val SPLASH_TIME_OUT: Long = 12000

    private lateinit var binding: UiSplashActivityBinding
    private lateinit var timer: CountDownTimer


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = UiSplashActivityBinding.inflate(layoutInflater)
        setContentView(binding.root)
        immersionBar {
            fullScreen(true)
            statusBarDarkFont(true)
        }
        timer = WelComManager.initTimer(
            activity = this,
            countTime = SPLASH_TIME_OUT,
            countAction = { millisUntilFinished ->
                // 更新UI，比如显示剩余秒数
                val progressPercentage: Int = (100 * millisUntilFinished / SPLASH_TIME_OUT).toInt()
                val Percentage = 100 - progressPercentage
                binding.progressBar.progress = Percentage
            },
            goMainAction = {
                // 倒计时或广告关闭后跳转主界面
                binding.progressBar.progress = 100
                startMain()
            }
        )

        val post = isPost
        if (!post) {
            makeGetRequest(this)
            isPost = true
        }
    }

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

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

}