package com.dlc.eboard.ui.sign

import android.os.Bundle
import androidx.lifecycle.ViewModelProvider
import com.dlc.eboard.R
import com.dlc.eboard.common.SignType
import com.dlc.eboard.databinding.ActivitySignInBinding
import com.dlc.eboard.ui.common.BaseCountDownActivity
import com.dlc.eboard.ui.widget.NavViewCompat
import com.dlc.eboard.ui.widget.TitleBar

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

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

    override val mTitleBar: TitleBar by lazy { viewBinding.titleBar }
    override val mNavView: NavViewCompat by lazy { viewBinding.navView }

    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)
        viewModel.setSignFace(intent.getParcelableExtra("sign_face"))
        showDefaultFragment()
    }

    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() {
        startCountDown()
    }

    override fun onStopCountDown() {
        stopCountDown()
    }

}