package com.zj365.health.act.datamanage

import android.annotation.SuppressLint
import android.widget.ImageView
import android.widget.TextView
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 DataManageShowAdapter : BaseAdapter<HealthDataBean>(R.layout.item_data_manager_data) {
    var isSelect = false
    private val textColor by lazy {
        arrayOf(
            R.color.col_9BD,
            R.color.col_9Bd,
            R.color.col_481
        )
    }
    private 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 getDataType(dataType: Int): String {
        val showName = when (dataType) {
            1 -> {
                "用户上传"
            }

            2 -> {
                "管家上传"
            }

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

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

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

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

            3, 4 -> {
                2
            }

            else -> {
                1
            }
        }
        holder.setGone(R.id.ivSelected, !isSelect)
        val view = holder.getView<ImageView>(R.id.ivSelected)
        view.isSelected = item.isSelected

//        if (isSelect){
//            holder.getView<LinearLayout>(R.id.root).setOnClickListener {
//                view.isSelected = !view.isSelected
//                item.isSelected = view.isSelected
//            }
//        }
        var result = item.dataValue
        if (item.dataValue!!.contains(",")) {
            result = item.dataValue?.replace(",", "/")
        } else {
            try {
                result = String.format("%.2f", result?.toDouble())
            } catch (e: Exception) {

            }
        }
        var time: String? = ""
        time = if (item.updateTime == null) {
            item.createTime
        } else {
            item.updateTime

        }
        holder.setText(R.id.tvPosition, (holder.layoutPosition + 1).toString())
            .setText(R.id.tvDataType, getDataName(item.dataType!!))
            .setText(R.id.tvDataValue, result)
            .setText(R.id.uploadStatus, getDataType(item.upType!!))
            .setText(R.id.tvTestDate, item.time)
            .setText(R.id.tvUploadData, time)
            .setText(R.id.tvUnit, getUnit(item.dataType!!))

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

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

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