package com.zj365.health.adapter

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.UricCalibrationListBean
import com.zj365.health.R

class UricCalibrationListAdapter : BaseAdapter<UricCalibrationListBean>(
    R.layout.item_uric_calibration_list
) {

    var isSelect = false

    val textColor by lazy {
        arrayOf(
            R.color.col_9BD,
            R.color.col_02c,
            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: UricCalibrationListBean) {
        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.tvStatus, bg[colorIndex])
        holder.setTextColor(
            R.id.tvStatus,
            ContextCompat.getColor(context, textColor[colorIndex])
        )
        holder.getView<TextView>(R.id.tvStatus).apply {
            text = result
            setPadding(
                SizeUtil.dp2px(context, 10f),
                SizeUtil.dp2px(context, 5f),
                SizeUtil.dp2px(context, 10f),
                SizeUtil.dp2px(context, 5f)
            )
        }

        holder.setText(R.id.tvCalibrationTime, "校准时间：${item.createTime}")
        holder.setText(R.id.tvUploadingName, "上传人：${item.upName}")
        holder.setText(R.id.tvMeasurementTime, "测量时间：${item.bpTime}")

        holder.setText(R.id.tvMeasurementValue, "测量值：${item.uaValue.toFloat().toInt()}μmol/L")
    }

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

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