package com.zj365.health.adapter

import android.widget.TextView
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.xty.base.adapter.BaseAdapter
import com.xty.common.SizeUtil
import com.zj365.health.R
import com.xty.network.model.BodyInfo

class BodyInfoHistory(var isSelect: Boolean = false) :
    BaseAdapter<BodyInfo>(R.layout.item_body_history) {

    //key=id  value=position
    var selIds = mutableMapOf<Int, Int>()
    override fun convert(holder: BaseViewHolder, item: BodyInfo) {
        val colorIndex = when (item.upType) {
            1 -> {
                0
            }

            3, 4 -> {
                2
            }

            else -> {
                1
            }
        }

        if (!isSelect) {
            //状态还原
            item.isSelect = isSelect
            selIds.clear()
        } else {
            var txt = holder.getView<TextView>(R.id.mSelect)
            txt.isSelected = item.isSelect
            if (item.isSelect) {
                selIds[item.id] = holder.layoutPosition
            } else {
                if (selIds.containsKey(item.id)) {
                    selIds.remove(item.id)
                }
            }
        }
        holder.setGone(R.id.mSelect, !isSelect)
            .setText(R.id.mSelect, "序号：${holder.layoutPosition + 1}")

        holder.setText(R.id.mTime1, "测量时间：${item.time}")
            .setText(
                R.id.mTime2,
                "上传时间：${if (item.updateTime.isNullOrEmpty()) item.createTime else item.updateTime}"
            )
            .setText(R.id.mWeight, "体重：${item.weight}kg")
            .setText(R.id.mZf, "体脂肪：${item.bf}%")

        //上传者名称
        val uploadStatus = holder.getView<TextView>(R.id.mStatus)
        uploadStatus.apply {
            setBackgroundResource(bg[colorIndex])
            setTextColor(resources.getColor(textColor[colorIndex]))
            setPadding(
                SizeUtil.dp2px(context, 10f),
                SizeUtil.dp2px(context, 5f),
                SizeUtil.dp2px(context, 10f),
                SizeUtil.dp2px(context, 5f)
            )
            text = getDataType(item.upType)
        }
    }

    private fun getDataType(dataType: Int): String {
        val showName = when (dataType) {
            1 -> {
                "用户上传"
            }

            2 -> {
                "管家上传"
            }

            3 -> {
                "物联网上传"
            }

            4 -> {
                "管理员上传"
            }

            else -> {
                "家人上传"
            }
        }
        return showName
    }

    val textColor by lazy {
        arrayOf(
            R.color.col_9BD,
            R.color.col_9Bd,
            R.color.col_FDA481
        )
    }

    val bg by lazy {
        arrayOf(
            R.drawable.shape_xya_status_yellow,
            R.drawable.shape_xya_status,
            R.drawable.shape_xya_status_orange,
        )
    }
}