package com.zj365.dc.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Typeface
import android.text.SpannableString
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.core.view.isVisible
import com.blankj.utilcode.util.SizeUtils
import com.tamsiree.rxkit.RxTimeTool
import com.xty.common.util.SpannableUtils
import com.xty.network.model.HealthMonitoringListBean
import com.zj365.dc.R
import java.text.SimpleDateFormat

/**
 * 自定义Adapter
 */
class HealthMonitoringAdapter(
    val context: Context,
    var mainBean: MutableList<HealthMonitoringListBean> = mutableListOf()
) : BaseAdapter() {

    //器官名
    private val units = arrayOf(
        R.string.info_1_unit, R.string.info_3_unit, R.string.info_7_unit,
        R.string.info_4_unit, R.string.info_9_unit, R.string.info_6_unit,
        R.string.info_2_unit, R.string.info_8_unit, R.string.info_5_unit,
        R.string.info_10_unit, R.string.info_6_unit
    )

    override fun getCount(): Int {
        return mainBean.size
    }

    override fun getItem(position: Int): HealthMonitoringListBean {
        return mainBean[position]
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    @SuppressLint("SetTextI18n")
    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View? {
        var convertView = convertView
        lateinit var holder: ViewHolder
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_health, parent, false)
            convertView?.apply {
                holder = ViewHolder()
                holder.mHealthName = findViewById(R.id.mHealthName)
                holder.mHealthStatus = findViewById(R.id.mHealthStatus)
                holder.mHealthInfo = findViewById(R.id.mHealthInfo)
                holder.mHealthTime = findViewById(R.id.mHealthTime)
                holder.mHealthIcon = findViewById(R.id.mHealthIcon)
                holder.mBackground = findViewById(R.id.ivBg)
                holder.mTvCalorie = findViewById(R.id.tvCalorie)
                holder.mView = convertView
                tag = holder
            }
        } else {
            holder = convertView.tag as ViewHolder
        }

        holder.apply {
            val healthMonitoringListBean = mainBean[position]
            when (healthMonitoringListBean.buttonStatus) {
                1 -> {
                    mHealthStatus.setImageResource(R.mipmap.ic_health_normal)
                    mHealthStatus.isVisible = true
                }
                2 -> {
                    mHealthStatus.setImageResource(R.mipmap.ic_health_error)
                    mHealthStatus.isVisible = true
                }
                3 -> {
                    mHealthStatus.setImageResource(R.mipmap.ic_health_fire)
                    mHealthStatus.isVisible = true
                }
                else -> {
                    mHealthStatus.isVisible = false
                }
            }
            mHealthName.text = healthMonitoringListBean.buttonName
            mHealthInfo.text = if (healthMonitoringListBean.buttonContent.isNullOrEmpty()) {
                ""
            } else if (
                healthMonitoringListBean.buttonContent == "建模中" ||
                healthMonitoringListBean.buttonContent == "未开通" ||
                healthMonitoringListBean.buttonContent == "续费"
            ) {
                val buttonContent = healthMonitoringListBean.buttonContent
                setStrSpan(
                    buttonContent,
                    14,
                    ContextCompat.getColor(
                        context, R.color.col_313
                    ),
                    Typeface.BOLD,
                    0,
                    buttonContent.length
                )
            } else {
                if (healthMonitoringListBean.type == 5) {//睡眠没有单位，设置睡眠span
                    if (healthMonitoringListBean.buttonContent == "0小时00分") {
                        "-小时-分"
                    } else {
                        healthMonitoringListBean.buttonContent
                    }
                } else {
                    val buttonContent = healthMonitoringListBean.buttonContent
                    setStrSpan(
                        buttonContent + context.getString(units[healthMonitoringListBean.type]),
                        14,
                        ContextCompat.getColor(
                            context, R.color.col_313
                        ),
                        Typeface.BOLD,
                        0,
                        "$buttonContent${context.getString(units[healthMonitoringListBean.type])}".length -
                                context.getString(units[healthMonitoringListBean.type]).length
                    )
                }
            }

            val calorie = healthMonitoringListBean.calorie
            if (!TextUtils.isEmpty(calorie) && healthMonitoringListBean.type == 4) {
                val calorieStr = "卡路里:${calorie}Kcal"
                mTvCalorie.text = calorieStr/*setStrSpan(
                    calorieStr,
                    11,
                    ContextCompat.getColor(
                        context, R.color.col_7c7
                    ),
                    Typeface.BOLD,
                    0,
                    calorieStr.length - 3
                )*/
            } else {
                if (
                    healthMonitoringListBean.buttonContent == "建模中" ||
                    healthMonitoringListBean.buttonContent == "未开通" ||
                    healthMonitoringListBean.buttonContent == "续费" ||
                    healthMonitoringListBean.buttonContent == "敬请期待"
                ){
                    mTvCalorie.visibility = View.GONE
                }else{
                    mTvCalorie.visibility = View.VISIBLE
                    if (TextUtils.isEmpty(calorie)){
                        val calorieStr = "监测:--次"
                        mTvCalorie.text = calorieStr
                    }else{
                        val calorieStr = "监测:${calorie}次"
                        mTvCalorie.text = calorieStr
                    }
                }


            }

            mHealthTime.text = if (TextUtils.isEmpty(healthMonitoringListBean.buttonTime)) {
                ""
            } else {
                val buttonTime = healthMonitoringListBean.buttonTime
                if (healthMonitoringListBean.buttonTime == "免费体验100天") {
                    setStrSpan(
                        buttonTime,
                        12,
                        ContextCompat.getColor(
                            context, R.color.col_02c
                        ),
                        Typeface.BOLD,
                        0,
                        buttonTime.length
                    )
                } else {
                    buttonTime
                }
            }
//            mView.setBackgroundResource(healthMonitoringListBean.buttonBg)
//            mBackground.setImageResource(healthMonitoringListBean.buttonBg) //颜色都是白色
            mHealthIcon.setImageResource(healthMonitoringListBean.buttonIcon)
        }
        return convertView
    }


    /**
     *  设置字体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)
    }

    private fun milliseconds2String(strTime: String): String {
        return RxTimeTool.milliseconds2String(
            strTime.toLong() * 1000,
            SimpleDateFormat("yyyy-MM-dd HH:mm")
        )
    }

    inner class ViewHolder {
        lateinit var mView: View
        lateinit var mHealthName: TextView
        lateinit var mHealthStatus: ImageView
        lateinit var mHealthInfo: TextView
        lateinit var mHealthTime: TextView
        lateinit var mHealthIcon: ImageView
        lateinit var mBackground: ImageView
        lateinit var mTvCalorie: TextView
    }
}
