package com.zj365.health.adapter

import android.annotation.SuppressLint
import android.graphics.Typeface
import android.text.SpannableString
import android.text.TextUtils
import android.view.View
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.core.view.isVisible
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.xty.base.adapter.BaseAdapter
import com.zj365.health.R
import com.xty.common.model.ReportBean
import com.xty.common.util.SpannableUtils
import com.xty.common.weight.MyProgressCircleView
import com.zj365.health.vm.ReportVm

class ReportAnalysisAdapter : BaseAdapter<ReportBean>(R.layout.child_report_2) {

    @SuppressLint("SetTextI18n")
    override fun convert(holder: BaseViewHolder, item: ReportBean) {
        holder.setText(R.id.mTitle, item.title)
        val circle = holder.getView<MyProgressCircleView>(R.id.mCircle)
        circle.setData(item)
        circle.isVisible = item.isShow

        val one = holder.getView<TextView>(R.id.tvOne)
        val two = holder.getView<TextView>(R.id.tvTwo)
        val line1 = holder.getView<View>(R.id.vLine1)

        val three = holder.getView<TextView>(R.id.tvThree)
        val four = holder.getView<TextView>(R.id.tvFour)
        val five = holder.getView<TextView>(R.id.tvFive)
        val line2 = holder.getView<View>(R.id.vLine2)

        val six = holder.getView<TextView>(R.id.tvSix)
        val seven = holder.getView<TextView>(R.id.tvSeven)
        val line3 = holder.getView<View>(R.id.vLine3)
        when (item.title) {
            "心率" -> {
                holder.setGone(R.id.tvInto, false)
                val countStr = if (TextUtils.isEmpty(item.synthesizeList?.count)) {
                    0
                } else {
                    item.synthesizeList?.count?.toFloat()?.toInt()
                }
                val multiple = countStr!!.times(10)
                val hour = multiple.div(60)//小时
                val minute = (multiple % 60)//分钟
                val durationStr = if (hour > 0) {
                    if (minute > 0) {
                        "时长:${hour}小时${minute}分钟"
                    } else {
                        "时长:${hour}小时"
                    }
                } else {
                    if (minute > 0) {
                        "时长:0小时${minute}分钟"
                    } else {
                        "时长:0小时"
                    }
                }
                one.text = "累计:${countStr}次"
                two.text = durationStr
                line1.isVisible = true
                three.text = "平均:${item.synthesizeList?.avg}次/分"
                four.text = "最低:${item.synthesizeList?.min}次/分"
                five.text = "最高:${item.synthesizeList?.max}次/分"
                line2.isVisible = true
                six.isVisible = false
                seven.isVisible = false
                line3.isVisible = false
            }

            "血氧" -> {
                holder.setGone(R.id.tvInto, false)
                val countStr = if (TextUtils.isEmpty(item.synthesizeList?.count)) {
                    0
                } else {
                    item.synthesizeList?.count?.toFloat()?.toInt()
                }
                val multiple = countStr!!.times(10)
                val hour = multiple.div(60)//小时
                val minute = (multiple % 60)//分钟
                val durationStr = if (hour > 0) {
                    if (minute > 0) {
                        "时长:${hour}小时${minute}分钟"
                    } else {
                        "时长:${hour}小时"
                    }
                } else {
                    if (minute > 0) {
                        "时长:0小时${minute}分钟"
                    } else {
                        "时长:0小时"
                    }
                }
                one.text = "累计:${countStr}次"
                two.text = durationStr
                line1.isVisible = true
                three.text = "平均:${item.synthesizeList?.avg}%"
                four.text = "最低:${item.synthesizeList?.min}%"
                five.text = "最高:${item.synthesizeList?.max}%"
                line2.isVisible = true
                six.isVisible = false
                seven.isVisible = false
                line3.isVisible = false
            }

            "呼吸率" -> {
                holder.setGone(R.id.tvInto, false)
                val countStr = if (TextUtils.isEmpty(item.synthesizeList?.count)) {
                    0
                } else {
                    item.synthesizeList?.count?.toFloat()?.toInt()
                }
                val multiple = countStr!!.times(10)
                val hour = multiple.div(60)//小时
                val minute = (multiple % 60)//分钟
                val durationStr = if (hour > 0) {
                    if (minute > 0) {
                        "时长:${hour}小时${minute}分钟"
                    } else {
                        "时长:${hour}小时"
                    }
                } else {
                    if (minute > 0) {
                        "时长:0小时${minute}分钟"
                    } else {
                        "时长:0小时"
                    }
                }
                one.text = "累计:${countStr}次"
                two.text = durationStr
                line1.isVisible = true
                three.text = "平均:${item.synthesizeList?.avg}次/分"
                four.text = "最低:${item.synthesizeList?.min}次/分"
                five.text = "最高:${item.synthesizeList?.max}次/分"
                line2.isVisible = true
                six.isVisible = false
                seven.isVisible = false
                line3.isVisible = false
            }

            "体温" -> {
                holder.setGone(R.id.tvInto, false)
                val countStr = if (TextUtils.isEmpty(item.synthesizeList?.count)) {
                    0
                } else {
                    item.synthesizeList?.count?.toFloat()?.toInt()
                }
                val multiple = countStr!!.times(10)
                val hour = multiple.div(60)//小时
                val minute = (multiple % 60)//分钟
                val durationStr = if (hour > 0) {
                    if (minute > 0) {
                        "时长:${hour}小时${minute}分钟"
                    } else {
                        "时长:${hour}小时"
                    }
                } else {
                    if (minute > 0) {
                        "时长:0小时${minute}分钟"
                    } else {
                        "时长:0小时"
                    }
                }
                one.text = "累计:${countStr}次"
                two.text = durationStr
                line1.isVisible = true
                three.text = "平均:${item.synthesizeList?.avg}℃"
                four.text = "最低:${item.synthesizeList?.min}℃"
                five.text = "最高:${item.synthesizeList?.max}℃"
                line2.isVisible = true
                six.isVisible = false
                seven.isVisible = false
                line3.isVisible = false
            }

            "血压" -> {
                holder.setGone(R.id.tvInto, false)
                val countStr = if (TextUtils.isEmpty(item.synthesizeList?.count)) {
                    0
                } else {
                    item.synthesizeList?.count?.toFloat()?.toInt()
                }
                val multiple = countStr!!.times(10)
                val hour = multiple.div(60)//小时
                val minute = (multiple % 60)//分钟
                val durationStr = if (hour > 0) {
                    if (minute > 0) {
                        "时长:${hour}小时${minute}分钟"
                    } else {
                        "时长:${hour}小时"
                    }
                } else {
                    if (minute > 0) {
                        "时长:0小时${minute}分钟"
                    } else {
                        "时长:0小时"
                    }
                }
                one.text = "累计:${countStr}次"
                two.text = durationStr
                line1.isVisible = true
                three.text = setStrSpan(
                    "平均:${item.synthesizeList?.avg}mmHg",
                    8,
                    ContextCompat.getColor(
                        context, R.color.col_7c7
                    ),
                    Typeface.NORMAL,
                    "平均:${item.synthesizeList?.avg}".length,
                    "平均:${item.synthesizeList?.avg}mmHg".length
                )
//                three.text = "平均:${item.synthesizeList?.avg}mmHg"
                four.text = setStrSpan(
                    "最低:${item.synthesizeList?.min}mmHg",
                    8,
                    ContextCompat.getColor(
                        context, R.color.col_7c7
                    ),
                    Typeface.NORMAL,
                    "最低:${item.synthesizeList?.min}".length,
                    "最低:${item.synthesizeList?.min}mmHg".length
                )
//                four.text = "最低:${item.synthesizeList?.min}mmHg"
                five.text = setStrSpan(
                    "最高:${item.synthesizeList?.max}mmHg",
                    8,
                    ContextCompat.getColor(
                        context, R.color.col_7c7
                    ),
                    Typeface.NORMAL,
                    "最高:${item.synthesizeList?.max}".length,
                    "最高:${item.synthesizeList?.max}mmHg".length
                )
//                five.text = "最高:${item.synthesizeList?.max}mmHg"
                line2.isVisible = true
                six.isVisible = false
                seven.isVisible = false
                line3.isVisible = false
            }

            "血糖" -> {
                holder.setGone(R.id.tvInto, true)
                val countStr = if (TextUtils.isEmpty(item.synthesizeList?.count)) {
                    "0"
                } else {
                    item.synthesizeList?.count?.toFloat()?.toInt()

                }
                one.text = "累计:${countStr}次"
                two.text = "时长:${item.synthesizeList?.count?.toFloat()?.toInt()}小时"
                line1.isVisible = true
                three.text = setStrSpan(
                    "平均:${item.synthesizeList?.avg}mmol/L",
                    10,
                    ContextCompat.getColor(
                        context, R.color.col_7c7
                    ),
                    Typeface.NORMAL,
                    "平均:${item.synthesizeList?.avg}".length,
                    "平均:${item.synthesizeList?.avg}mmol/L".length
                )
//                three.text = "平均:${item.synthesizeList?.avg}mmol/L"
                four.text = setStrSpan(
                    "最低:${item.synthesizeList?.min}mmol/L",
                    10,
                    ContextCompat.getColor(
                        context, R.color.col_7c7
                    ),
                    Typeface.NORMAL,
                    "最低:${item.synthesizeList?.min}".length,
                    "最低:${item.synthesizeList?.min}mmol/L".length
                )
//                four.text = "最低:${item.synthesizeList?.min}mmol/L"
                five.text = setStrSpan(
                    "最高:${item.synthesizeList?.max}mmol/L",
                    10,
                    ContextCompat.getColor(
                        context, R.color.col_7c7
                    ),
                    Typeface.NORMAL,
                    "最高:${item.synthesizeList?.max}".length,
                    "最高:${item.synthesizeList?.max}mmol/L".length
                )
//                five.text = "最高:${item.synthesizeList?.max}mmol/L"
                line2.isVisible = true
                six.isVisible = false
                seven.isVisible = false
                line3.isVisible = false
            }

            "血脂" ->{

                holder.setGone(R.id.tvInto, false)
                val countStr = if (TextUtils.isEmpty(item.synthesizeList?.count)) {
                    "0"
                } else {
                    item.synthesizeList?.count?.toFloat()?.toInt()
                }
                one.text = "累计:${countStr}次"
                two.text = "时长:${item.synthesizeList?.count?.toInt()}天"
                line1.isVisible = false
                three.isVisible = false
                four.isVisible = false
                five.isVisible = false
//                five.text = "最高:${item.synthesizeList?.max}μmol/L"
                line2.isVisible = true
                six.isVisible = false
                seven.isVisible = false
                line3.isVisible = false
            }

            "尿酸" -> {
                holder.setGone(R.id.tvInto, false)
                val countStr = if (TextUtils.isEmpty(item.synthesizeList?.count)) {
                    "0"
                } else {
                    item.synthesizeList?.count?.toFloat()?.toInt()
                }
                one.text = "累计:${countStr}次"
                two.text = "时长:${item.synthesizeList?.count?.toInt()}天"
                line1.isVisible = true
                three.text = setStrSpan(
                    "平均:${item.synthesizeList?.avg}μmol/L",
                    10,
                    ContextCompat.getColor(
                        context, R.color.col_7c7
                    ),
                    Typeface.NORMAL,
                    "平均:${item.synthesizeList?.avg}".length,
                    "平均:${item.synthesizeList?.avg}μmol/L".length
                )
//                three.text = "平均:${item.synthesizeList?.avg}μmol/L"
                four.text = setStrSpan(
                    "最低:${item.synthesizeList?.min}μmol/L",
                    10,
                    ContextCompat.getColor(
                        context, R.color.col_7c7
                    ),
                    Typeface.NORMAL,
                    "最低:${item.synthesizeList?.min}".length,
                    "最低:${item.synthesizeList?.min}μmol/L".length
                )
//                four.text = "最低:${item.synthesizeList?.min}μmol/L"
                five.text = setStrSpan(
                    "最高:${item.synthesizeList?.max}μmol/L",
                    10,
                    ContextCompat.getColor(
                        context, R.color.col_7c7
                    ),
                    Typeface.NORMAL,
                    "最高:${item.synthesizeList?.max}".length,
                    "最高:${item.synthesizeList?.max}μmol/L".length
                )
//                five.text = "最高:${item.synthesizeList?.max}μmol/L"
                line2.isVisible = true
                six.isVisible = false
                seven.isVisible = false
                line3.isVisible = false
            }

            "运动" -> {
                holder.setGone(R.id.tvInto, true)
                val countStr = if (TextUtils.isEmpty(item.synthesizeList?.count)) {
                    "0"
                } else {
                    item.synthesizeList?.count?.toFloat()?.toInt()
                }
                one.text = "累计:${countStr}天"
                two.isVisible = false
                line1.isVisible = true
                three.text = "平均:${item.synthesizeList?.avg}步"
                four.text = "最低:${item.synthesizeList?.min}步"
                five.text = "最高:${item.synthesizeList?.max}步"
                line2.isVisible = true
                six.isVisible = false
                seven.isVisible = false
                line3.isVisible = false
            }

            "睡眠" -> {
                holder.setGone(R.id.tvInto, true)
                val countStr = if (TextUtils.isEmpty(item.synthesizeList?.count)) {
                    0
                } else {
                    item.synthesizeList?.count?.toFloat()?.toInt()
                }
                val avgTime = if (TextUtils.isEmpty(item.synthesizeList?.avgTime)) {
                    0
                } else {
                    item.synthesizeList?.avgTime?.toFloat()?.toInt()?.times(countStr!!)
                }
                val hour = avgTime!!.div(60)//小时
                val minute = (avgTime % 60)//分钟
                val durationStr = if (hour > 0) {
                    if (minute > 0) {
                        "时长:${hour}小时${minute}分钟"
                    } else {
                        "时长:${hour}小时"
                    }
                } else {
                    if (minute > 0) {
                        "时长:0小时${minute}分钟"
                    } else {
                        "时长:0小时"
                    }
                }
                val sleepDuration = item.synthesizeList?.sleepDuration
                if (sleepDuration == ReportVm.ReportInfoType.Hour.reportType ||
                    sleepDuration == ReportVm.ReportInfoType.Day.reportType
                ) {
                    one.text = "累计:${countStr}天"
                    two.text = durationStr
                    line1.isVisible = true
                    three.text = "深睡:${
                        "%.1f".format(
                            item.synthesizeList?.avgDeep?.toFloat()?.div(60)
                        )
                    }小时"
                    four.text = "浅睡:${
                        "%.1f".format(
                            item.synthesizeList?.avgLight?.toFloat()?.div(60)
                        )
                    }小时"
                    five.isVisible = false
                    line2.isVisible = true
                    six.isVisible = false
//                    six.text =
//                        "浅睡:${
//                            "%.1f".format(
//                                item.synthesizeList?.avgLight?.toFloat()?.div(60)
//                            )
//                        }小时"
                    seven.isVisible = false
                    line3.isVisible = false
                } else {
                    one.text = "累计:${countStr}天"
                    two.text = durationStr
                    line1.isVisible = true
                    three.text = "平均:${"%.1f".format(item.synthesizeList?.avgTime?.toFloat()?.div(60))}小时"
                    four.text = "最短:${"%.1f".format(item.synthesizeList?.minTime?.toFloat()?.div(60))}小时"
                    five.text = "最长:${"%.1f".format(item.synthesizeList?.maxTime?.toFloat()?.div(60))}小时"
                    line2.isVisible = true
                    six.text =
                        "平均深睡:${"%.1f".format(item.synthesizeList?.avgDeep?.toFloat()?.div(60))}小时"
                    seven.text =
                        "平均浅睡:${"%.1f".format(item.synthesizeList?.avgLight?.toFloat()?.div(60))}小时"
                    line3.isVisible = true
                }
            }

            else -> {
                holder.setGone(R.id.tvInto, false)
            }
        }
    }

    /**
     *  设置字体span
     *  @param text 内容
     *  @param spanSize 字体大小
     *  @param fontColor 字体颜色
     *  @param startIndex 开始位置
     *  @param endIndex 结束位置
     */
    private fun setStrSpan(
        text: String,
        spanSize: Int,
        fontColor: Int,
        styleSpan: Int,
        startIndex: Int,
        endIndex: Int
    ): SpannableString {
        return SpannableUtils.setStrSpan(text, spanSize, fontColor, styleSpan, startIndex, endIndex)
    }
}