package  com.zj365.health.adapter.bloodfat

import android.graphics.Typeface
import android.text.SpannableString
import android.text.Spanned
import android.text.style.AbsoluteSizeSpan
import android.text.style.ForegroundColorSpan
import android.text.style.StyleSpan
import androidx.core.content.ContextCompat
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.xty.base.adapter.BaseAdapter
import com.xty.common.Dateutils
import com.xty.network.model.BloodFatBean
import com.xty.network.model.BloodFatHeadInfo
import com.xty.network.model.BloodFatType
import com.zj365.health.R

class BloodFatAdapter : BaseAdapter<BloodFatHeadInfo.BloodFat>(R.layout.item_blood_fat) {
    override fun convert(holder: BaseViewHolder, item: BloodFatHeadInfo.BloodFat) {

        holder.setText(R.id.tv_blood_value, item.value.toString())
        holder.setText(R.id.tvTestTime, "mmol/L ${Dateutils.formatTimeStr(item.time,Dateutils.HH_MM_Format)}")
        when (item.type) {
            BloodFatType.Tc -> {
                if (item.value != 0f){
                    if (item.value in 5.23..5.69) {
                        showException(holder, false)
                    } else {
                        showException(holder, true)
                    }
                }else{
                    showException(holder, false)
                }

                holder.setText(R.id.tv_blood_name,"总胆固醇(TC)")
            }

            BloodFatType.Tg -> {
                if(item.value != 0f){
                    if (item.value in 0.45..1.69) {
                        showException(holder, false)
                    } else {
                        showException(holder, true)
                    }
                }else{
                    showException(holder, false)

                }

                holder.setText(R.id.tv_blood_name,"甘油三酯 (TG)")
            }

            BloodFatType.Hdl -> {

                if(item.value != 0f){
                    if (item.value in 0.7..2.0) {
                        showException(holder, false)
                    } else {
                        showException(holder, true)
                    }
                }else{
                    showException(holder, false)
                }

                holder.setText(R.id.tv_blood_name,"高密度脂蛋白(HDL)")
            }

            BloodFatType.Ldl -> {
                if(item.value!= 0f){
                    if (item.value < 3.12) {
                        showException(holder, false)
                    } else {
                        showException(holder, true)
                    }
                }else{
                    showException(holder, false)
                }

                holder.setText(R.id.tv_blood_name,"低密度脂蛋白(LDL)")
            }

            else -> {
                holder.setText(R.id.tv_firstValue, setSpan(0))
                holder.setVisible(R.id.iv_first, false)

                holder.setText(R.id.tv_secondValue, setSpan(0))
                holder.setVisible(R.id.iv_second, false)

                holder.setText(R.id.tv_blood_name,"总胆固醇(TC)")

            }
        }



        if (data.size - 1 == getItemPosition(item)) {
            holder.setGone(R.id.v_line, true)
        } else {
            holder.setGone(R.id.v_line, true)
        }
    }

    private fun showException(holder: BaseViewHolder, isException: Boolean) {
        if (isException) {
            holder.setText(R.id.tv_firstValue, setSpan(0))
            holder.setVisible(R.id.iv_first, false)

            holder.setText(R.id.tv_secondValue, setSpan(1))
            holder.setVisible(R.id.iv_second, true)
        } else {
            holder.setText(R.id.tv_firstValue, setSpan(1))
            holder.setVisible(R.id.iv_first, true)

            holder.setText(R.id.tv_secondValue, setSpan(0))
            holder.setVisible(R.id.iv_second, false)
        }

    }

    private fun setSpan(time: Int): SpannableString {
        var statuSpan = SpannableString("${time}次")

        statuSpan.setSpan(
            ForegroundColorSpan(
                //设置字体颜色
                ContextCompat.getColor(
                    context, R.color.black
                )
            ), 0, statuSpan.length - 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
        )
        //设置字体大小(绝对值,单位：像素),第二个参数boolean dip，如果为true，表示前面的字体大小单位为dip，否则为像素
        statuSpan.setSpan(
            AbsoluteSizeSpan(24, true),
            0, statuSpan.length - 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
        )
        //设置字体样式: NORMAL正常，BOLD粗体，ITALIC斜体，BOLD_ITALIC粗斜体
        statuSpan.setSpan(
            StyleSpan(Typeface.BOLD),
            0, statuSpan.length - 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
        );

        return statuSpan
    }
}