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.zj365.health.R
import com.xty.network.model.BloodHistoryNewBean

class BloodHistoryAdapter : BaseAdapter<BloodHistoryNewBean>(R.layout.item_xya_type_calibration) {

    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
        )
    }

    //上传类型1，用户上传，2，管家上传，3，物联网上传
    override fun convert(holder: BaseViewHolder, item: BloodHistoryNewBean) {
        holder.setGone(R.id.ivSelected, !isSelect)
        val view = holder.getView<ImageView>(R.id.ivSelected)
        view.isSelected = item.isSelected

        holder.setText(R.id.tvPosition, (holder.layoutPosition + 1).toString())

        var result = ""
        var upload = ""

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

            2, 5 -> {
                1
            }

            else -> {
                2
            }
        }
        holder.setBackgroundResource(R.id.tvUploadStatus, bg[colorIndex])
        holder.setTextColor(R.id.tvUploadStatus, ContextCompat.getColor(context,textColor[colorIndex]))
        holder.getView<TextView>(R.id.tvUploadStatus).apply {
            setPadding(
                SizeUtil.dp2px(context, 10f),
                SizeUtil.dp2px(context, 5f),
                SizeUtil.dp2px(context, 10f),
                SizeUtil.dp2px(context, 5f)
            )
        }

        when (item.upType) {
            1 -> {
                result = "用户上传"
                upload = "上传人："
            }

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

            3 -> {
                result = "物联网上传"
                upload = "设备号："
            }
            4->{
                result = "管理员上传"
                upload = "上传人："
            }
            else -> {
                result = "家人上传"
                upload = "上传人："
            }
        }

        val time =  if (item.updateTime.isNullOrEmpty()){
            item.createTime
        }else{
            item.updateTime
        }

        holder.setText(R.id.tvUpload, upload)
        holder.setText(R.id.tvStudyRecord, time)
            .setText(R.id.tvUploadPeople, item.upName)
            .setText(R.id.tvUploadStatus, result)
            .setText(R.id.tvDataInfo, item.bpTime)
            .setText(R.id.tvGdb, item.highDbp.toString() + "mmHg")
            .setText(R.id.tvDdb, item.lowDbp.toString() + "mmHg")
    }

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

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