package com.zj365.health.adapter.binddevice

import android.view.View
import android.widget.ImageView
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.xty.base.adapter.BaseAdapter
import com.zj365.health.R
import com.xty.network.model.BindDevHistoryBean
import java.lang.StringBuilder


open class BindDeviceHistoryAdapter :
    BaseAdapter<BindDevHistoryBean>(R.layout.item_data_history) {

    var isSelect = false
    override fun convert(holder: BaseViewHolder, item: BindDevHistoryBean) {

        val selectView = holder.getView<ImageView>(R.id.ivSelected)

        if (isSelect) {
            selectView.visibility = View.VISIBLE
            selectView.isSelected = item.isSelect
        } else {
            item.isSelect = false
            selectView.visibility = View.GONE
            selectView.isSelected = false
        }

        val indexOf = data.indexOf(item)

        if (item.deviceType == 1){
            holder.setText(R.id.tvDataType,"血压: ")
            holder.setText(R.id.tvUnit,"mmHg")
        }else{
            holder.setText(R.id.tvDataType,"血糖: ")
            holder.setText(R.id.tvUnit,"mmol/L")
        }
        holder.setText(R.id.tvPosition, "${(indexOf + 1)}")
        holder.setText(R.id.tvUploadData, item.createTime)
        holder.setText(R.id.tvTestDate,item.time)
        holder.setText(R.id.tvDataValue,parseJson(item.info,item.deviceType))
    }

    private fun parseJson(data: String, type: Int): String {
        var builder = StringBuilder()
        var json = org.json.JSONObject(data)
        return if (type == 1) {
            if (json.has("highDbp")) {
                builder.append(json.get("highDbp"))
                builder.append("/")
            }
            if (json.has("lowDbp")) {
                builder.append(json.get("lowDbp"))
            }
            builder.toString()
        } else {
            if (json.has("bloodsugar")) {
                builder.append(json.get("bloodsugar"))
            }
            builder.toString()
        }
    }
}