package com.zj365.health.adapter

import android.annotation.SuppressLint
import android.text.Html
import android.widget.TextView
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.tamsiree.rxkit.RxTimeTool
import com.xty.base.adapter.BaseAdapter
import com.xty.common.MyImageGetter
import com.xty.common.TimeUtils
import com.xty.common.weight.SleepInfoView
import com.zj365.health.R
import com.zj365.health.model.MyReportBean
import com.xty.network.model.SleepBean
import java.text.SimpleDateFormat

class ReportInfoAdapter(var sleepBean: SleepBean? = null) :
    BaseAdapter<MyReportBean>(R.layout.item_report_info) {

    @SuppressLint("SimpleDateFormat")
    override fun convert(holder: BaseViewHolder, item: MyReportBean) {
        holder.setText(R.id.mTitle, item.name)
        if (item.type != -1) {
            val tv = holder.getView<TextView>(R.id.mContent)
            val getter = MyImageGetter(context, tv)
            tv.text = Html.fromHtml(item.content, getter, null)
            holder.getView<TextView>(R.id.mExpans).tag = tv
            if (item.isLaunch) {
                tv.maxLines = Integer.MAX_VALUE
                tv.invalidate()
                holder.setText(R.id.mExpans, "收起")
            } else {
                tv.maxLines = 3
                tv.invalidate()
                holder.setText(R.id.mExpans, "展开")
            }

            holder.setGone(R.id.mContent, false)
            holder.setGone(R.id.mLinSleep, true)
            holder.setGone(R.id.mExpans, false)
        } else {
            holder.setGone(R.id.mContent, true)
            holder.setGone(R.id.mLinSleep, false)
            holder.setGone(R.id.mExpans, true)
            holder.setGone(R.id.mSleepShow, sleepBean == null)
            sleepBean?.let {
                it.model?.let { model ->
                    if (model.startTime == null || model.startTime == 0) {
                        holder.setGone(R.id.mLinSleep, false)
                        return@let
                    }
                    val allTime = model.deepSleepTotal + model.lightSleepTotal
                    val allTimeStr = TimeUtils.secToHour(allTime!!.toLong())
                    val lightSleep = model.lightSleepTotal
//                        max(model.deepSleepTotal, model.lightSleepTotal)
                    val deepSleep = model.deepSleepTotal
//                        min(model.deepSleepTotal, model.lightSleepTotal)


                    holder.setText(R.id.mV1, "${TimeUtils.secToHour(deepSleep.toLong())}")
                    holder.setText(R.id.mV2, "${TimeUtils.secToHour(lightSleep.toLong())}")
                    holder.setText(R.id.mV3, allTimeStr)
                    holder.setText(R.id.mV4, model.soberSleepCount.toString())
//                    holder.setText(
//                        R.id.mV5,
//                        RxTimeTool.milliseconds2String(
//                            model.startTime * 1000.toLong(),
//                            SimpleDateFormat("HH:mm")
//                        )
//                    )
//                    holder.setText(
//                        R.id.mV6,
//                        RxTimeTool.milliseconds2String(
//                            model.endTime * 1000.toLong(),
//                            SimpleDateFormat("HH:mm")
//                        )
//                    )

                    var previousTime = 0L //上一个数据的结束时间
                    var allSleepTime = 0
                    val listTime = mutableListOf<SleepBean.Value>()
                    it.value.forEachIndexed { index, value ->
                        if (index == 0) {
                            listTime.add(value)
                        } else {
                            if (previousTime + 1 == getSecondTime(value.sleepStartTime)) {  //中间无清醒
                                listTime.add(value)
                            } else {  // 清醒了
                                //计算清醒的数据 并插入（原始数据中并没有清醒数据 所以这里需要计算并插入清醒数据）
                                val bean = SleepBean.Value(
                                    (getSecondTime(value.sleepStartTime) - previousTime).toInt() - 1,
                                    previousTime + 1,
                                    999
                                )

                                //还要加清醒时间的时间段
                                allSleepTime += (getSecondTime(value.sleepStartTime) - previousTime).toInt() - 1 //单位 秒

                                listTime.add(bean)
                                //添加原来的数据
                                listTime.add(value)
                            }

                        }
                        previousTime =
                            getSecondTime(value.sleepStartTime) + value.sleepLen //上一条数据的睡眠结束时间
                        allSleepTime += value.sleepLen //单位 秒

                    }
                    holder.setGone(R.id.mSleepShow, listTime.isEmpty())
                    holder.getView<SleepInfoView>(R.id.mSleepShow)
                        .setData(allSleepTime, listTime)

                    holder.setText(
                        R.id.mStart,
                        "入睡${
                            RxTimeTool.milliseconds2String(
                                model.startTime * 1000.toLong(),
                                SimpleDateFormat("HH:mm")
                            )
                        }"
                    )
                    holder.setText(
                        R.id.mEnd,
                        "起床${
                            RxTimeTool.milliseconds2String(
                                model.endTime * 1000.toLong(),
                                SimpleDateFormat("HH:mm")
                            )
                        }"
                    )
                }
            }

        }

    }

    /**
     * 修正数据  不要毫秒 要的秒
     */
    private fun getSecondTime(time: Long): Long {
        if (time > 9999999999) {
            return time / 1000
        }
        return time
    }
}