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 XZhiDataManageListAdapter :
    BaseAdapter<HealthDataBean>(R.layout.item_xzhi_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 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) {
            5 -> {
                showName = "甘油三酯："
            }
        }
        return showName
    }

    private fun getGMDZDBName(dataType: Int): String {
        var showName = ""
        when (dataType) {
            5 -> {
                showName = "高密度脂蛋白："
            }
        }
        return showName
    }

    private fun getDMDZDBName(dataType: Int): String {
        var showName = ""
        when (dataType) {
            5 -> {
                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 tvGMDZDBName = holder.getView<TextView>(R.id.tvGMDZDBName)
        //高密度脂蛋白值
        val tvGMDZDBValue = holder.getView<TextView>(R.id.tvGMDZDBValue)
        //高密度脂蛋白单位
        val tvGMDZDBUnit = holder.getView<TextView>(R.id.tvGMDZDBUnit)
        //低密度脂蛋白名称
        val tvDMDZDBName = holder.getView<TextView>(R.id.tvDMDZDBName)
        //低密度脂蛋白值
        val tvDMDZDBValue = holder.getView<TextView>(R.id.tvDMDZDBValue)
        //低密度脂蛋白单位
        val tvDMDZDBUnit = holder.getView<TextView>(R.id.tvDMDZDBUnit)
        //测量时间
        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) {
            5 -> {
                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.tc)) "0" else item.tc
                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.tg)) "0" else item.tg
                tvGYSZUnit.text = getUnit(item.dataType!!)
                tvGMDZDBName.text = getGMDZDBName(item.dataType!!)
                tvGMDZDBValue.text = if (TextUtils.isEmpty(item.hdl)) "0" else item.hdl
                tvGMDZDBUnit.text = getUnit(item.dataType!!)
                tvDMDZDBName.text = getDMDZDBName(item.dataType!!)
                tvDMDZDBValue.text = if (TextUtils.isEmpty(item.ldl)) "0" else item.ldl
                tvDMDZDBUnit.text = getUnit(item.dataType!!)
            }
        }
        //是否选中
        if (item.upType != 3) {
            selected.isVisible = isSelect
        } else {
            selected.isVisible = false
        }
        selected.isSelected = item.isSelected
    }

    fun getSelectData(): MutableList<HealthDataBean> {
        val 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()
    }
}