package com.zj365.mime.adapter

import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.tamsiree.rxkit.RxTimeTool
import com.xty.base.adapter.BaseAdapter
import com.zj365.mime.R
import com.xty.network.model.WatchData
import java.text.SimpleDateFormat

class DataAdapter: BaseAdapter<String>(R.layout.item_data) {

    private var watchData:WatchData?=null
    private val tittles = arrayOf(
        R.string.info_1, R.string.info_4, R.string.info_8,
        R.string.info_5, R.string.info_2, R.string.info_3,
    )

    private val units = arrayOf(
        R.string.info_1_unit, R.string.info_3_unit, R.string.info_1_unit,
        R.string.info_4_unit, R.string.info_2_unit, R.string.info_8_unit,
    )


    //背景图
    private val bgs = arrayOf(
        R.mipmap.bg_xl, R.mipmap.bg_oo, R.mipmap.bg_breath,
        R.mipmap.bg_temp, R.mipmap.bg_xya, R.mipmap.bg_bloodsugar,
    )

    //icon图
    private val icons = arrayOf(
        R.mipmap.ic_xl, R.mipmap.ic_oo, R.mipmap.ic_h_breath,
        R.mipmap.ic_temp,    R.mipmap.ic_xya, R.mipmap.ic_bloodsugar
    )

    val format= SimpleDateFormat("yyyy-MM-dd HH")
    override fun convert(holder: BaseViewHolder, item: String) {
        holder.setText(R.id.mHealthName,tittles[holder.layoutPosition])
        holder.setImageResource(R.id.mHealthIcon,icons[holder.layoutPosition])
        holder.setBackgroundResource(R.id.mParent,bgs[holder.layoutPosition])
        if(watchData==null){
            holder.setText(R.id.mHealthTime,"--")
            holder.setText(R.id.mHealthInfo,"--${context.getString(units[holder.layoutPosition])}")
        }else{
            holder.setText(R.id.mHealthTime,"--")
            holder.setText(R.id.mHealthInfo,"--${context.getString(units[holder.layoutPosition])}")
            holder.setVisible(R.id.mHealthStatus, true)

            when(holder.layoutPosition){
                0->{
                    watchData!!.hrData?.let {
                        holder.setText(R.id.mHealthInfo,"${if(it.dataValue=="0"||it.dataValue.isEmpty())"--" else it.dataValue}${context.getString(units[holder.layoutPosition])}")
                        holder.setText(R.id.mHealthTime,"${ RxTimeTool.milliseconds2String(it.time.toLong()*1000,format)}:00")
                        holder.setVisible(R.id.mHealthStatus, it.message.isNullOrEmpty())
                        var status =  if (it.message=="正常") R.mipmap.ic_health_normal else R.mipmap.ic_health_error
                        holder.setImageResource(R.id.mHealthStatus,status)

                    }

                }
                1->{
                    watchData!!.soData?.let {
                        holder.setText(R.id.mHealthInfo,"${if(it.dataValue=="0"||it.dataValue.isEmpty())"--" else it.dataValue}${context.getString(units[holder.layoutPosition])}")
                        holder.setText(R.id.mHealthTime,"${ RxTimeTool.milliseconds2String(it.time.toLong()*1000,format)}:00")
                        holder.setVisible(R.id.mHealthStatus, it.message.isNullOrEmpty())
                        var status =
                            if (it.message == "正常") R.mipmap.ic_health_normal else R.mipmap.ic_health_error
                        holder.setImageResource(R.id.mHealthStatus, status)

                    }
                }
                2->{
                    watchData!!.respData?.let {
                        holder.setText(R.id.mHealthInfo,"${if(it.dataValue=="0"||it.dataValue.isEmpty())"--" else it.dataValue}${context.getString(units[holder.layoutPosition])}")
                        holder.setText(R.id.mHealthTime,"${ RxTimeTool.milliseconds2String(it.time.toLong()*1000,format)}:00")
                        holder.setVisible(R.id.mHealthStatus, it.message.isNullOrEmpty())
                        var status =
                            if (it.message == "正常") R.mipmap.ic_health_normal else R.mipmap.ic_health_error
                        holder.setImageResource(R.id.mHealthStatus, status)

                    }
                }
                3-> {
                    watchData!!.tempData?.let {
                        holder.setText(R.id.mHealthInfo, "${if(it.dataValue=="0"||it.dataValue.isEmpty())"--" else it.dataValue}${context.getString(units[holder.layoutPosition])}")
                        holder.setText(
                            R.id.mHealthTime,
                            "${RxTimeTool.milliseconds2String(it.time.toLong()*1000, format)}:00"
                        )
                        holder.setVisible(R.id.mHealthStatus, it.message.isNullOrEmpty())
                        var status =
                            if (it.message == "正常") R.mipmap.ic_health_normal else R.mipmap.ic_health_error
                        holder.setImageResource(R.id.mHealthStatus, status)

                    }
                }
                4->{
                    watchData!!.dbpData?.let {
                        holder.setText(R.id.mHealthInfo, "${if(it.dataValue=="0"||it.dataValue.isEmpty())"--" else it.dataValue}${context.getString(units[holder.layoutPosition])}")
                        holder.setText(
                            R.id.mHealthTime,
                            "${RxTimeTool.milliseconds2String(it.time.toLong()*1000, format)}:00"
                        )
                        holder.setVisible(R.id.mHealthStatus, it.message.isNullOrEmpty())
                        var status =
                            if (it.message == "正常") R.mipmap.ic_health_normal else R.mipmap.ic_health_error
                        holder.setImageResource(R.id.mHealthStatus, status)

                    }
                }
                5->{
                    watchData!!.sugarData?.let {
                        holder.setText(R.id.mHealthInfo, "${if(it.dataValue=="0"||it.dataValue.isEmpty())"--" else it.dataValue}${context.getString(units[holder.layoutPosition])}")
                        holder.setText(
                            R.id.mHealthTime,
                            "${RxTimeTool.milliseconds2String(it.time.toLong()*1000, format)}:00"
                        )
                        holder.setVisible(R.id.mHealthStatus, it.message.isNullOrEmpty())
                        var status =
                            if (it.message == "正常") R.mipmap.ic_health_normal else R.mipmap.ic_health_error
                        holder.setImageResource(R.id.mHealthStatus, status)

                    }
                }
                }
            }
    }

    fun  setAdapterData(bean: WatchData){
        this.watchData=bean
        notifyDataSetChanged()

    }

}