package  com.zj365.health.adapter.bloodfat

import android.annotation.SuppressLint
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.xty.base.adapter.BaseAdapter
import com.xty.common.SizeUtil
import com.xty.network.model.BloodLipidAdjustBean
import com.zj365.health.R

class BloodLipidAdjustAdapter : BaseAdapter<BloodLipidAdjustBean>(R.layout.item_blood_lipid_records) {

    var isSelect = false

    val textColor by lazy {
        arrayOf(
            R.color.col_9BD,
            R.color.col_25C,
            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
        )
    }

    override fun convert(
        holder: BaseViewHolder,
        item: BloodLipidAdjustBean
    ) {
        holder.setGone(R.id.ivSelected, !isSelect)
        val view = holder.getView<ImageView>(R.id.ivSelected)
        view.isSelected = item.isSelected

        var result = ""
        val colorIndex = when (item.upType) {
            1 -> {
                0
            }

            2, 5 -> {
                1
            }

            else -> {
                2
            }
        }
        when (item.upType) {
            1 -> {
                result = "用户上传"
            }

            2 -> {
                result = "管家上传"
            }

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

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

            else -> {
                result = "家人上传"
            }
        }
        holder.setBackgroundResource(R.id.tvUploadStatus, bg[colorIndex])
        holder.setTextColor(
            R.id.tvUploadStatus,
            ContextCompat.getColor(context, textColor[colorIndex])
        )
        holder.getView<TextView>(R.id.tvUploadStatus).apply {
            text = result
            setPadding(
                SizeUtil.dp2px(context, 10f),
                SizeUtil.dp2px(context, 5f),
                SizeUtil.dp2px(context, 10f),
                SizeUtil.dp2px(context, 5f)
            )
        }

        holder.setText(
            R.id.tv_adjust_time,
            "校准时间:${item.createTime} "
        )
        holder.setText(
            R.id.tv_upload_person,
            "上传人：${item.upName}"
        )
        holder.setText(
            R.id.tv_measuring_time,
            "测量时间：${item.bpTime}"
        )
        holder.setText(
            R.id.tv_tc_exponent,
            "总胆固醇：${item.tc}mmol/L"
        )
        holder.setText(
            R.id.tv_tg_exponent,
            "甘油三酯：${item.tg}mmol/L"
        )
        holder.setText(
            R.id.tv_hdl_exponent,
            "高密度脂蛋白：${item.hdl}mmol/L"
        )
        holder.setText(
            R.id.tv_ldl_exponent,
            "低密度脂蛋白：${item.ldl}mmol/L"
        )
    }

    fun getSelectData(): MutableList<BloodLipidAdjustBean> {
        val result = mutableListOf<BloodLipidAdjustBean>()
        for (datum in data) {
            if (datum.isSelected) {
                result.add(datum)
            }
        }
        return result
    }

    @SuppressLint("NotifyDataSetChanged")
    fun reset() {
        for (datum in data) {
            datum.isSelected = false
        }
        notifyDataSetChanged()
    }
}
