package xn.xxp.main.person

import android.os.Bundle
import android.view.View
import androidx.viewbinding.ViewBinding
import core.ui.activity.BaseCountDownActivity
import core.ui.fragment.RcBaseFragment
import xn.xxp.app.LabApp
import xn.xxp.databinding.ActivityLaboratoryPersonBinding
import xn.xxp.room.RoomTool
import xn.xxp.room.bean.LabConfig
import xn.xxp.utils.TabFragmentPagerAdapter
import xn.xxp.widget.ITitleBar
import xn.xxp.widget.NavViewCompat

/**
 * 人员信息
 *
 * @author ReiChin_
 */
open class LaboratoryPersonActivity :
    BaseCountDownActivity<ActivityLaboratoryPersonBinding>() {

    lateinit var labConfig: LabConfig

    override fun createViewBinding() = ActivityLaboratoryPersonBinding.inflate(layoutInflater)
    override fun getMTitleBar(): ITitleBar {
        return binding.titleBar
    }

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

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

        initTableViews()
    }

    private fun initTableViews() {
        labConfig = RoomTool.getInstance().labConfigDao().labConfig

        val tabArray: MutableList<out RcBaseFragment<out ViewBinding>>
        if ("1" == LabApp.userVo.pageType) {
            binding.tabLabel.visibility = View.VISIBLE
            tabArray = mutableListOf(
                LabAccessPersonFragment.newInstance(
                    LabAccessPersonFragment.TYPE_EXPERIMENT,
                    labConfig.labId.toString()
                ),
                LabDutyPersonFragment.newInstance(
                    LabDutyPersonFragment.TYPE_DUTY,
                    labConfig.labId.toString()
                ),
                LabDutyPersonFragment.newInstance(
                    LabDutyPersonFragment.TYPE_PATROL,
                    labConfig.labId.toString()
                ),
                LabAccessPersonFragment.newInstance(
                    LabAccessPersonFragment.TYPE_ACCESS,
                    labConfig.labId.toString()
                )
            )
        } else {
            binding.tabLabel.visibility = View.GONE
            tabArray = mutableListOf(
                LabAccessPersonFragment.newInstance(
                    LabAccessPersonFragment.TYPE_EXPERIMENT,
                    labConfig.labId.toString()
                )
            )
        }

        val adapter = TabFragmentPagerAdapter(supportFragmentManager, tabArray)
        binding.viewPager.adapter = adapter
        binding.viewPager.offscreenPageLimit = adapter.count

        // 实验人员
        binding.experimentLabel.setOnClickListener {
            binding.viewPager.currentItem = 0
        }
        // 值班人员
        binding.dutyLabel.setOnClickListener {
            binding.viewPager.currentItem = 1
        }
        // 巡查人员
        binding.patrolLabel.setOnClickListener {
            binding.viewPager.currentItem = 2
        }
        // 准入人员
        binding.accessLabel.setOnClickListener {
            binding.viewPager.currentItem = 3
        }
    }

    override fun enabledBackCountDown() = true
}