package xn.xxp.home.adapter

import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import http.vo.response.LabBulletinBoardVo
import http.vo.response.LabWarnVo
import xn.xxp.R
import xn.xxp.mqtt.entity.BulletinBoardEntity
import xn.xxp.widget.ItemBoardView

public class HomeBoardAdapter :
    BaseQuickAdapter<LabBulletinBoardVo, BaseViewHolder>(R.layout.item_home_board) {
    private val mValueMap = HashMap<String, BoardValue>()
    private val mWarningSet = HashSet<String>()
    private fun goneSensor(funNum: String?): Boolean {
        return SensorEnum.FIRE.funNum == funNum
    }

    override fun setNewInstance(list: MutableList<LabBulletinBoardVo>?) {
        val newlyList = list?.filter { !goneSensor(it.funNum) }

        if (null != newlyList) {
            mValueMap.clear()
            newlyList.forEach {
                mValueMap[it.deviceNo] = BoardValue(it.deviceValue, it.unit, it.formatWaring())
            }
        }
        super.setNewInstance(newlyList?.toMutableList())
    }

    fun updateSensorValue(data: BulletinBoardEntity.Data?) {
        val newlyList = data?.functionStatuses?.filter { !goneSensor(it.funNum) }

        var valueChanged = mValueMap.size != newlyList?.size
        newlyList?.forEach { item ->
            val oldValue = mValueMap[item.deviceNo]
            if (!valueChanged) {
                valueChanged = oldValue?.value != item.deviceValue || oldValue?.unit != item.unit
            }
            mValueMap[item.deviceNo] = BoardValue(item.deviceValue, item.unit, item.formatWaring())
        }

        if (valueChanged) {
            notifyDataSetChanged()
        }
    }

    fun updateWaringValue(data: List<LabWarnVo>?) {
        val newlyList =
            data?.filter { null != it.firstTrigger() && !goneSensor(it.firstTrigger().deviceNo) }
                ?.map { it.firstTrigger() }

        var valueChanged = mWarningSet.size != newlyList?.size
        if (!valueChanged) {
            val changedItem = newlyList?.find { !mWarningSet.contains(it.deviceNo) }
            valueChanged = null != changedItem
        }

        mWarningSet.clear()
        newlyList?.forEach { item ->
            mWarningSet.add(item.deviceNo)
        }

        if (valueChanged) {
            notifyDataSetChanged()
        }
    }

    override fun convert(holder: BaseViewHolder, item: LabBulletinBoardVo) {
        val itemView = holder.itemView as ItemBoardView
        val itemVal = mValueMap[item.deviceNo]
        itemView.setSensorInfo(item.attributeName, item.icon)
        if ("烟感" == item.attributeName) {
            if (itemVal?.value == "0") {
                itemView.setSensorValue(
                    "正常",
                    "",
                    mWarningSet.contains(item.deviceNo),
                    item.online
                )
            } else {
                itemView.setSensorValue(
                    "异常",
                    "",
                    mWarningSet.contains(item.deviceNo),
                    item.online
                )
            }
        } else {
            itemView.setSensorValue(
                itemVal?.value,
                itemVal?.unit,
                mWarningSet.contains(item.deviceNo),
                item.online
            )
        }

    }

    private data class BoardValue(val value: String?, val unit: String?, val formatVal: String?)
}