package com.casic.br.view

import android.annotation.SuppressLint
import android.os.CountDownTimer
import android.view.ViewGroup
import com.casic.br.R
import com.gyf.immersionbar.ImmersionBar
import com.pengxh.kt.lite.base.KotlinBaseActivity
import com.pengxh.kt.lite.extensions.navigatePageTo
import com.qmuiteam.qmui.util.QMUIDisplayHelper
import kotlinx.android.synthetic.main.activity_splash.*

@SuppressLint("CustomSplashScreen")
class SplashScreenActivity : KotlinBaseActivity() {

    private lateinit var countDownTimer: CountDownTimer

    override fun initLayoutView(): Int = R.layout.activity_splash

    override fun setupTopBarLayout() {
        ImmersionBar.with(this).statusBarDarkFont(false).init()
        //根据不同设备状态栏高度设置"跳过"按钮的Margin值
        val statusBarHeight = QMUIDisplayHelper.getStatusBarHeight(this)
        val rightMargin = QMUIDisplayHelper.dp2px(this, 10)
        if (skipButton.layoutParams is ViewGroup.MarginLayoutParams) {
            val params = skipButton.layoutParams as ViewGroup.MarginLayoutParams
            params.setMargins(skipButton.width, statusBarHeight, rightMargin, 0)
            skipButton.requestLayout()
        }
    }

    override fun initData() {
        countDownTimer = object : CountDownTimer(4000, 1000) {
            override fun onFinish() {
                startMainActivity()
            }

            override fun onTick(millisUntilFinished: Long) {
                skipButton.text = String.format("跳过\u3000${(millisUntilFinished / 1000)}s")
            }
        }
    }

    override fun observeRequestState() {

    }

    override fun initEvent() {
        countDownTimer.start()
        skipButton.setChangeAlphaWhenPress(true)
        skipButton.setOnClickListener {
            countDownTimer.cancel()
            startMainActivity()
        }
    }

    fun startMainActivity() {
        navigatePageTo<MainActivity>()
        finish()
    }
}