package com.zj365.health.adapter

import android.os.Bundle
import android.text.TextUtils
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.xty.base.adapter.BaseAdapter
import com.xty.common.SizeUtil
import com.zj365.health.R
import com.xty.network.model.MedicalDataBean


class DataManageTijianAdapter : BaseAdapter<MedicalDataBean>(R.layout.item_datamantge_tijian) {
    var isSelect = false
    val bundle by lazy { Bundle() }
    override fun convert(holder: BaseViewHolder, item: MedicalDataBean) {

        holder.getView<ImageView>(R.id.ivSelected).isSelected = item.isSelectd
        holder.setGone(R.id.ivSelected, !isSelect)

        if(item.dataType == 9){  //住院记录

          val startTime =  if (item.startTime !=null && item.startTime.length>9){
                item.startTime.subSequence(0,10)
            }else{
                ""
          }
            val endTime = if (item.startTime !=null && item.endTime.length>9){
                item.endTime.subSequence(0,10)
            }else{
                ""
            }
            holder.setText(R.id.tv_time, "住院时间: ${startTime}至${endTime}")
            holder.setText(
                R.id.tv_jigou,
                "住院医院: ${item.hospital?:""}"
            )
            holder.setText(R.id.tv_title, item.hospitalExplain)
        }else{  //体检记录
            item.time?.replace("00:00:00", "")?.let {
                holder.setText(R.id.tv_time, "体检时间: ${it.trim()}")
            }
            holder.setText(
                R.id.tv_jigou,
                "体检机构: ${if (TextUtils.isEmpty(item.orgName)) "" else item.orgName}"
            )
            holder.setText(R.id.tv_title, item.name)
        }

        holder.setText(R.id.uploadStatus, getDataType(item.upType!!))

        val colorIndex = when (item.upType) {
            1 -> {
                0
            }

            2,5-> {
                1
            }

            else -> {
                2
            }
        }
        holder.setBackgroundResource(R.id.uploadStatus, bg[colorIndex])
        holder.getView<TextView>(R.id.uploadStatus).apply {
            setTextColor(resources.getColor(textColor[colorIndex]))
            setPadding(
                SizeUtil.dp2px(context, 10f),
                SizeUtil.dp2px(context, 5f),
                SizeUtil.dp2px(context, 10f),
                SizeUtil.dp2px(context, 5f)
            )
        }
        var result = mutableListOf<String>()
        var result1 = mutableListOf<String>()
        val examinationList = item.examinationList
        val medicationList = item.medicationList
        val hospitalList = item.hospitalList
        examinationList?.forEach {
            result.add(it.imageUrl!!)
        }
        medicationList?.forEach {
            result1.add(it.imageUrl!!)
        }
        hospitalList?.forEach {
            result.add(it.imageUrl!!)
        }
        if (result.size > 4) {
            result = result.subList(0, 4)
        }
        if (result1.size > 4) {
            result1 = result1.subList(0, 4)
        }
        val adapter1 = PictureShowAdapter()
        val view = holder.getView<RecyclerView>(R.id.rvList)
        view.layoutManager = GridLayoutManager(context, 4)
        view.adapter = adapter1
        view.setOnTouchListener { v, event ->
            holder.getView<LinearLayout>(R.id.root).onTouchEvent(event)
        }
        adapter1.setNewInstance(result)

    }

    fun getSelectData(): MutableList<MedicalDataBean> {
        var result = mutableListOf<MedicalDataBean>()
        for (datum in data) {
            if (datum.isSelectd) {
                result.add(datum)
            }
        }
        return result
    }

    fun getDataType(dataType: Int): String {
        var showName = when (dataType) {
            1 -> {
                "用户上传"
            }

            2 -> {
                "管家上传"
            }

            3 -> {
                "物联网上传"
            }

            4 -> {
                "管理员上传"
            }

            else -> {
                "家人上传"
            }
        }
        return showName
    }

    fun reset() {
        for (datum in data) {
            datum.isSelectd = false
        }
        notifyDataSetChanged()
    }

    val textColor by lazy { arrayOf(R.color.col_9BD, R.color.col_9Bd, R.color.col_FDA481) }
    val bg by lazy {
        arrayOf(
            R.drawable.shape_xya_status_yellow,
            R.drawable.shape_xya_status,
            R.drawable.shape_xya_status_orange,
        )
    }

}