package xn.xxp.home.sign

import android.os.Build
import android.os.Bundle
import androidx.lifecycle.ViewModelProvider
import core.ui.activity.BaseCountDownActivity
import http.vo.response.SignFaceVo
import xn.xxp.R
import xn.xxp.databinding.ActivitySignInBinding
import xn.xxp.home.auth.SignType
import xn.xxp.widget.ITitleBar
import xn.xxp.widget.NavViewCompat

/**
 * 签到
 *
 * @author ReiChin_
 */
open class SignInActivity : BaseCountDownActivity<ActivitySignInBinding>(), ICountDown {

    override fun createViewBinding() = ActivitySignInBinding.inflate(layoutInflater)


    private val mSignType by lazy {
        intent.getIntExtra("sign_type", SignType.ACCESS.code)
    }

    private val viewModel by lazy {
        ViewModelProvider(this, ViewModelProvider.NewInstanceFactory()).get(
            SignViewModel::class.java
        )
    }

    override fun initViews(savedInstanceState: Bundle?) {
        super.initViews(savedInstanceState)
        viewModel.setSignType(mSignType)

        val signFace = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { // API 33
            intent.getParcelableExtra("sign_face", SignFaceVo::class.java)
        } else {
            @Suppress("DEPRECATION")
            intent.getParcelableExtra("sign_face") as? SignFaceVo
        }
        if (null != signFace) {
            viewModel.setSignFace(signFace)
        }
        showDefaultFragment()
    }

    override fun getMTitleBar(): ITitleBar {
        return binding.titleBar
    }

    override fun getMNavView(): NavViewCompat {
        return binding.navView
    }

    protected open fun showDefaultFragment() {
        val transaction = supportFragmentManager.beginTransaction()
        transaction.add(
            R.id.container,
            SafetyCheckFragment(),
            SafetyCheckFragment::class.simpleName
        )
        transaction.commitAllowingStateLoss()
    }

    override fun enabledBackCountDown() = true
    override fun onStartCountDown() {
    }

    override fun onStopCountDown() {
    }
}