package com.dlc.eboard.ui.lib

import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import com.dlc.eboard.R
import com.dlc.eboard.common.CommonUtils
import com.dlc.eboard.databinding.ActivityLabDetailBinding
import com.dlc.eboard.sp.DeviceSp
import com.dlc.eboard.ui.common.BaseCountDownActivity
import com.dlc.eboard.ui.common.SimpleFlipperAdapter
import com.dlc.eboard.ui.home.SubSafeAdapter
import com.dlc.eboard.ui.widget.NavViewCompat
import com.dlc.eboard.ui.widget.TitleBar
import com.rc.core.ui.widget.decoration.GridSpacingItemDecoration
import com.rc.core.util.DeviceUtils
import com.rc.httpcore.vo.response.LaboratoryVo

/**
 * 信息牌
 *
 * @author ReiChin_
 */
open class LabDetailActivity :
    BaseCountDownActivity<ActivityLabDetailBinding>() {

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

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

    override fun initData() {
        val laboratoryVo = DeviceSp.loadLaboratoryVo(this)
        if (null == laboratoryVo) {
            showToast("暂无实验室信息，即将重新获取")
            CommonUtils.restartApp(this)
            return
        }

        // 实验室负责人
        viewBinding.adminUser.text = laboratoryVo.adminUserDesc
        // 安全责任人
        viewBinding.safeUser.text = laboratoryVo.safeUserDesc

        initSubSafeVies(laboratoryVo)
    }

    protected open fun initSubSafeVies(laboratoryVo: LaboratoryVo) {
        // 底部信息栏
        if (laboratoryVo.specialClassify.isNullOrEmpty()) {
            viewBinding.specialFlipper.visibility = View.GONE
        } else {
            viewBinding.specialFlipper.visibility = View.VISIBLE
            val adapter = SimpleFlipperAdapter(data = laboratoryVo.specialClassify)
            viewBinding.specialFlipper.adapter = adapter
            viewBinding.specialFlipper.flipInterval = 5000
            viewBinding.specialFlipper.setInAnimation(this, R.animator.anim_in)
            viewBinding.specialFlipper.setOutAnimation(this, R.animator.anim_out)
            viewBinding.specialFlipper.startFlipping()
        }

        viewBinding.content.layoutManager = GridLayoutManager(this, 2)
        val decor = GridSpacingItemDecoration(
            2,
            DeviceUtils.dip2px(this, 35f),
            DeviceUtils.dip2px(this, 20f), false
        )
        viewBinding.content.addItemDecoration(decor)
        val adapter = SubSafeAdapter(this,R.style.SafetyBoardContent)
        viewBinding.content.adapter = adapter

        // 主要危险类别、风险防控措施、灭火要点、安全警示标示
        adapter.setNewInstance(laboratoryVo.safeClassifyList)
        adapter.setEmptyView(R.layout.view_list_empty)
    }

    override fun enabledBackCountDown() = true

    override fun enabledLogoutCountDown() = true

}