package com.zj365.health.adapter

import android.text.Html
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.entity.MultiItemEntity
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.tamsiree.rxkit.RxTimeTool
import com.xty.common.MyImageGetter
import com.xty.common.weight.VisceraView
import com.zj365.health.R
import com.zj365.health.model.StatusBean
import com.xty.network.model.ReportBean
import java.lang.StringBuilder
import java.text.SimpleDateFormat

class HealthTabAdapter<T: MultiItemEntity>: BaseMultiItemQuickAdapter<T,BaseViewHolder>() {
    val heartIcon by lazy {
        arrayOf(
            R.mipmap.ic_heart_4,
            R.mipmap.ic_heart_4,
            R.mipmap.ic_heart_1,
            R.mipmap.ic_heart_2,
            R.mipmap.ic_heart_3
        )
    }
    val liverIcon by lazy {
        arrayOf(
            R.mipmap.ic_liver_4,
            R.mipmap.ic_liver_4,
            R.mipmap.ic_liver_1,
            R.mipmap.ic_liver_2,
            R.mipmap.ic_liver_3,
        )
    }
    val spleenIcon by lazy {
        arrayOf(
            R.mipmap.ic_spleen_4,
            R.mipmap.ic_spleen_4,
            R.mipmap.ic_spleen_1,
            R.mipmap.ic_spleen_2,
            R.mipmap.ic_spleen_3,
        )
    }
    val lungIcon by lazy {
        arrayOf(
            R.mipmap.ic_lung_4,
            R.mipmap.ic_lung_4,
            R.mipmap.ic_lung_1,
            R.mipmap.ic_lung_2,
            R.mipmap.ic_lung_3
        )
    }
    val kidneyIcon by lazy {
        arrayOf(
            R.mipmap.ic_kidney_4,
            R.mipmap.ic_kidney_4,
            R.mipmap.ic_kidney_1,
            R.mipmap.ic_kidney_2,
            R.mipmap.ic_kidney_3
        )
    }
    val largeIcon by lazy {
        arrayOf(
            R.mipmap.ic_large_intestine_4,
            R.mipmap.ic_large_intestine_4,
            R.mipmap.ic_large_intestine_1,
            R.mipmap.ic_large_intestine_2,
            R.mipmap.ic_large_intestine_3,
        )
    }
    val smallIcon by lazy {
        arrayOf(
            R.mipmap.ic_small_intestine_4,
            R.mipmap.ic_small_intestine_4,
            R.mipmap.ic_small_intestine_1,
            R.mipmap.ic_small_intestine_2,
            R.mipmap.ic_small_intestine_3,
        )
    }
    val stomachIcon by lazy {
        arrayOf(
            R.mipmap.ic_stomach_4,
            R.mipmap.ic_stomach_4,
            R.mipmap.ic_stomach_1,
            R.mipmap.ic_stomach_2,
            R.mipmap.ic_stomach_3,
        )
    }
    val lymphIcon by lazy {
        arrayOf(
            R.mipmap.ic_lymph_4,
            R.mipmap.ic_lymph_4,
            R.mipmap.ic_lymph_1,
            R.mipmap.ic_lymph_2,
            R.mipmap.ic_lymph_3,
        )
    }
    val gallIcon by lazy {
        arrayOf(
            R.mipmap.ic_gallbladder_4,
            R.mipmap.ic_gallbladder_4,
            R.mipmap.ic_gallbladder_1,
            R.mipmap.ic_gallbladder_2,
            R.mipmap.ic_gallbladder_3,
        )
    }
    val bladderIcon by lazy {
        arrayOf(
            R.mipmap.ic_bladder_4,
            R.mipmap.ic_bladder_4,
            R.mipmap.ic_bladder_1,
            R.mipmap.ic_bladder_2,
            R.mipmap.ic_bladder_3,
        )
    }
    val pericardiumIcon by lazy {
        arrayOf(
            R.mipmap.ic_pericardium_4,
            R.mipmap.ic_pericardium_4,
            R.mipmap.ic_pericardium_1,
            R.mipmap.ic_pericardium_2,
            R.mipmap.ic_pericardium_3,
        )
    }
    val healthStr by lazy { context.resources.getStringArray(R.array.status_array) }
    val healthCol by lazy {
        arrayOf(
            R.color.col_c3d,
            R.color.col_c3d,
            R.color.col_009,
            R.color.col_e0b,
            R.color.col_ea7,
        )
    }
    var recycle:RecyclerView?=null
    init {
        addItemType(0, R.layout.item_status_0)
        addItemType(1,R.layout.item_status_1)
        addItemType(2,R.layout.item_status_2)
        addItemType(3,R.layout.item_status_2)
    }

    override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
        super.onAttachedToRecyclerView(recyclerView)
        recycle=recyclerView
    }
    override fun convert(holder: BaseViewHolder, item: T) {

        when( item.itemType){
            0->{
               var bean= item as StatusBean
                holder.setText(R.id.mTvStatus,healthStr[bean.bean!!.status.result+1])
                    .setTextColorRes(R.id.mTvStatus,healthCol[bean.bean!!.status.result+1])

                var times=bean.bean!!.status.createTime
                var timeMill= RxTimeTool.string2Milliseconds("${times.substring(0,10)} 00:00:00")
                var yestDay=
                    RxTimeTool.milliseconds2String(timeMill-(24*60*60*1000), SimpleDateFormat("MM-dd"))
                    holder.setText(R.id.mTvTime,"时间：${yestDay} 11:00 - ${times.substring(5,10)} 11:00")
            }
            1->{
                var bean=item as ReportBean
                setStatus(holder,bean.organ)
            }
            2->{
                var bean= item as StatusBean
                var sb = StringBuilder()
                if( !bean.bean!!.analysis.TcmAnalysis.isNullOrEmpty()){
                    "中医分析"
                    bean.bean!!.analysis.TcmAnalysis?.forEach {
                        sb.append(it.dictName).append("\n")
                        sb.append(it.content).append("\n")
                    }

                }else{
                    sb.append( "暂无数据")
                }

                holder.setText(R.id.mTitle, "中医分析")


                var tv = holder.getView<TextView>(R.id.mContent)
                var getter = MyImageGetter(context, tv)
                tv.text = Html.fromHtml(sb.toString().trim(), getter, null)


            }
            3->{
                var bean= item as StatusBean
                var sb = StringBuilder()
                if( !bean.bean!!.analysis.WmAnalysis.isNullOrEmpty()){
                    bean.bean!!.analysis.WmAnalysis?.forEachIndexed { index, bean ->
                        if(bean.dictName!="考虑症状"){
                            sb.append(String.format(context.getString(R.string.analysis_desc),bean.dictName,bean.name)).append("\n")
                            sb.append(String.format(context.getString(R.string.analysis_name),bean.name)).append("\n")
                            sb.append(bean.content).append("\n")
                        }
                    }
                    bean.bean!!.analysis.WmAnalysis?.forEachIndexed { index, bean ->
                        if(bean.dictName=="考虑症状"){
                            sb.append(String.format(context.getString(R.string.analysis_agree),"${bean.dictName}${bean.name}？")).append("\n")
                            sb.append(bean.content).append("\n")
                        }

                    }
                }else{
                    sb.append( "身体健康，请继续保持")
                }

                holder.setText(R.id.mTitle, "西医分析")
                var tv = holder.getView<TextView>(R.id.mContent)
                var getter = MyImageGetter(context, tv)
                tv.text = Html.fromHtml(sb.toString().trim(), getter, null)

            }
        }

        if(holder.layoutPosition==3){
            holder.itemView.post {
                recycle?.let {
                    var needHeight = it.height
                    if (holder.itemView.height < needHeight) {
                        val layoutParams = holder.itemView.layoutParams as ViewGroup.LayoutParams
                        layoutParams.height = needHeight
                        holder.itemView.layoutParams = layoutParams
                    }
                }
            }
        }
    }

    private fun setStatus(holder: BaseViewHolder,organ:ReportBean.Organ) {
        holder.getView<VisceraView>(R.id.mHeart).setInfo(getHealthStatus (organ.heart.model ) , heartIcon)
        holder.getView<VisceraView>(R.id.mStomach).setInfo(getHealthStatus (organ.stomach.model ) , stomachIcon)
        holder.getView<VisceraView>(R.id.mLung).setInfo(getHealthStatus (organ.lungs.model ) , lungIcon)
        holder.getView<VisceraView>(R.id.mKidney).setInfo(getHealthStatus (organ.kidney.model ) , kidneyIcon)
        holder.getView<VisceraView>(R.id.mLiver).setInfo(getHealthStatus (organ.liver.model )  ,liverIcon)
        holder.getView<VisceraView>(R.id.mSpleen).setInfo(getHealthStatus (organ.spleen.model ) , spleenIcon)

        holder.getView<VisceraView>(R.id.mLarge).setInfo(getHealthStatus (organ.largeIntestine.model) , largeIcon)
        holder.getView<VisceraView>(R.id.mSmall).setInfo(getHealthStatus (organ.smallIntestine.model) , smallIcon)
        holder.getView<VisceraView>(R.id.mLymph).setInfo(getHealthStatus (organ.lymph.model ) , lymphIcon)
        holder.getView<VisceraView>(R.id.mGallbladder).setInfo(getHealthStatus (organ.cholecyst.model) , gallIcon)
        holder.getView<VisceraView>(R.id.mBladder).setInfo(getHealthStatus (organ.bladder.model ) , bladderIcon)
        holder.getView<VisceraView>(R.id.mPericardium).setInfo(getHealthStatus (organ.pericardium.model ) , pericardiumIcon)
    }
    //获取健康状体
    private fun getHealthStatus(bean: ReportBean.ChildStatus?):Int {
        return if(bean==null){
            0
        }else{
            if(bean.sumNoEnough==-1){
               0
            }else{
                bean.idHealth+1
            }
        }
    }

}