package com.dlc.eboard.ui.person

import android.os.Bundle
import android.view.View
import androidx.viewbinding.ViewBinding
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.dlc.eboard.LabApp
import com.dlc.eboard.R
import com.dlc.eboard.common.CommonUtils
import com.dlc.eboard.databinding.ActivityLaboratoryPersonBinding
import com.dlc.eboard.ui.common.BaseCountDownActivity
import com.dlc.eboard.ui.common.TabFragmentPagerAdapter
import com.dlc.eboard.ui.widget.NavViewCompat
import com.dlc.eboard.ui.widget.TitleBar
import com.rc.core.ui.fragment.RcBaseFragment
import com.rc.core.util.DeviceUtils
import com.rc.httpcore.vo.response.LabPersonVo

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

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

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

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

        initTableViews()
    }

    private fun initTableViews() {
        val labConfig = LabApp.sLabConfig
        if (null == labConfig) {
            showToast("暂无实验室信息，即将重新获取")
            CommonUtils.restartApp(this)
            return
        }

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

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

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

    override fun enabledBackCountDown() = true

    override fun enabledLogoutCountDown() = true

}

/**
 * 实验室人员Adapter
 */
@Deprecated("废弃代码")
private class LaboratoryPersonAdapter :
    BaseQuickAdapter<LabPersonVo, BaseViewHolder>(R.layout.item_laboratory_person),
    LoadMoreModule {

    override fun convert(holder: BaseViewHolder, item: LabPersonVo) {
        // 头像
        Glide.with(context)
            .load(item.avatar)
            .placeholder(R.mipmap.icon_sign_in_avatar)
            .error(R.mipmap.icon_sign_in_avatar)
            .transform(RoundedCorners(DeviceUtils.dip2px(context, 6f)))
            .into(holder.getView(R.id.pPhoto))

        holder.setText(R.id.pName, "姓名: ${item.userName ?: ""}")
            .setText(R.id.pCardNumber, "工号: ${item.jobNumber ?: ""}")
            .setText(R.id.pSignTime, "签到时间: ${item.signTime ?: ""}")
    }

}