package com.example.defenseapplication.login

import android.content.Intent
import android.os.Bundle
import android.os.CountDownTimer
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.example.defenseapplication.R
import com.example.defenseapplication.databinding.RegisterSuccessActivityBinding
import com.example.defenseapplication.home.HomeActivity
import com.gyf.immersionbar.ImmersionBar
//注册成功界面
class RegisterSuccessActivity : AppCompatActivity() {

    private lateinit var binding: RegisterSuccessActivityBinding
    private var countDownTimer: CountDownTimer? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = RegisterSuccessActivityBinding.inflate(layoutInflater)
        setContentView(binding.root)
        ImmersionBar.with(this)
            .statusBarDarkFont(true)
            .titleBar(binding.topContainer)
            .init()
        // 返回按钮
        binding.backIcon.setOnClickListener {
            finish()
        }
        // 点击按钮：取消倒计时并立即跳转
        binding.btnEnter.setOnClickListener {
            cancelTimer()
            jumpToPlatform()
        }
        // 启动3秒倒计时，自动跳转
        startAutoJumpTimer()
    }

    private fun startAutoJumpTimer() {
        countDownTimer = object : CountDownTimer(3000L, 1000L) {
            override fun onTick(millisUntilFinished: Long) {
                val seconds = (millisUntilFinished / 1000).toInt()
                binding.btnEnter.text = "进入平台(${seconds}S)"
            }

            override fun onFinish() {
                binding.btnEnter.text = "进入平台(0S)"
                jumpToPlatform()
            }
        }.start()
    }

    private fun cancelTimer() {
        countDownTimer?.cancel()
        countDownTimer = null
    }

    private fun jumpToPlatform() {
        startActivity(Intent(this, HomeActivity::class.java))
        finish()
    }

    override fun onDestroy() {
        super.onDestroy()
        cancelTimer()
    }
}