package com.zj365.health.act.datamanage

import android.annotation.SuppressLint
import android.text.TextUtils
import android.widget.ImageView
import android.widget.TextView
import androidx.core.view.isVisible
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.HealthDataBean

class TZhongDataManageListAdapter:
    BaseAdapter<HealthDataBean>(R.layout.item_tzhong_data_manager_list) {
    var isSelect = false
    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,
        )
    }

    private fun getHeightUnit(dataType: Int): String {
        var showName = ""
        when (dataType) {
            3 -> {
                showName = "cm"
            }
        }
        return showName
    }

    private fun getUnit(dataType: Int): String {
        var showName = ""
        when (dataType) {
            1 -> {
                showName = "mmol/L"
            }

            2 -> {
                showName = "mmHg"
            }

            3 -> {
                showName = "kg"
            }

            4 -> {
                showName = "μmol/L"
            }

            5 -> {
                showName = "mmol/l"
            }
        }
        return showName
    }

    private fun getDataName(dataType: Int): String {
        var showName = ""
        when (dataType) {
            1 -> {
                showName = "血糖："
            }

            2 -> {
                showName = "血压："
            }

            3 -> {
                showName = "体重："
            }

            4 -> {
                showName = "尿酸："
            }

            5 -> {
                showName = "总胆固醇："
            }
        }
        return showName
    }

    private fun getGYSZName(dataType: Int): String {
        var showName = ""
        when (dataType) {
            3 -> {
                showName = "身高："
            }
        }
        return showName
    }

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

            2 -> {
                "管家上传"
            }

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

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

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

    @SuppressLint("SetTextI18n")
    override fun convert(holder: BaseViewHolder, item: HealthDataBean) {
        val colorIndex = when (item.upType) {
            1 -> {
                0
            }

            3, 4 -> {
                2
            }

            else -> {
                1
            }
        }
        //序号
        val tvPosition = holder.getView<TextView>(R.id.tvPosition)
        //胆固醇名称
        val tvDGCName = holder.getView<TextView>(R.id.tvDataType)
        //胆固醇值
        val tvDGCValue = holder.getView<TextView>(R.id.tvDataValue)
        //胆固醇单位
        val tvDGCUnit = holder.getView<TextView>(R.id.tvUnit)
        //上传者名称
        val uploadStatus = holder.getView<TextView>(R.id.uploadStatus)
        //甘油三酯名称
        val tvGYSZName = holder.getView<TextView>(R.id.tvGYSZName)
        //甘油三酯值
        val tvGYSZValue = holder.getView<TextView>(R.id.tvGYSZValue)
        //甘油三酯单位
        val tvGYSZUnit = holder.getView<TextView>(R.id.tvGYSZUnit)
        //测量时间
        val tvTime = holder.getView<TextView>(R.id.tvTestDate)
        //上传时间
        val tvUploadTime = holder.getView<TextView>(R.id.tvUploadData)
        //是否选中
        val selected = holder.getView<ImageView>(R.id.ivSelected)
        when(item.dataType) {
            3 -> {
                holder.setVisible(R.id.llBmiLayout,true)
                holder.setText(R.id.tvBmiValue,"${item.bmiInfo.bmi}")

                tvUploadTime.text = if (TextUtils.isEmpty(item.updateTime)) {
                    item.createTime
                } else {
                    item.updateTime
                }
                tvTime.text = item.time
                tvPosition.text = (holder.layoutPosition + 1).toString()
                tvDGCName.text = getDataName(item.dataType!!)
                tvDGCValue.text = if (TextUtils.isEmpty(item.dataValue)) "0" else item.dataValue
                tvDGCUnit.text = getUnit(item.dataType!!)
                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!!)
                }
                tvGYSZName.text = getGYSZName(item.dataType!!)
                tvGYSZValue.text = if (TextUtils.isEmpty(item.height)) "0" else item.height
                tvGYSZUnit.text = getHeightUnit(item.dataType!!)
            }
        }
        selected.isVisible = isSelect
        //是否选中
        selected.isSelected = item.isSelected
    }

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

    fun reset() {
        for (datum in data) {
            datum.isSelected = false
        }
        notifyDataSetChanged()
    }
}