package com.zj365.health.act.datamanage

import android.os.Bundle
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.zj365.health.adapter.PictureShowAdapter
import com.xty.network.model.MedicalDataBean


class DataManageMedicalAdapter : BaseAdapter<MedicalDataBean>(R.layout.item_datamantge_medical) {
    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)
        holder.setText(R.id.tvTime, item.createTime)
        holder.setText(R.id.tv_medical_name,"药品名称: ${item.medicineName?:""}")

        holder.setText(R.id.uploadStatus, getDataType(item.upType!!))
        var colorIndex = 0
        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
        examinationList?.forEach {
            result.add(it.imageUrl!!)
        }
        medicationList?.forEach {
            result1.add(it.imageUrl!!)
        }
        if (result.size>4){
            result = result.subList(0,4)
        }
        if (result1.size>4){
            result1 = result1.subList(0,4)
        }
//        var adapter1  = PictureShowAdapter()
//        val view = holder.getView<RecyclerView>(R.id.rvList)
//        view.layoutManager = GridLayoutManager(context,4)
//        view.adapter = adapter1
//
//        adapter1.setOnItemClickListener { adapter, view, position ->
//            bundle.clear()
//            bundle.putString("data", GsonUtils.toJson(item))
//            RouteManager.goAct(ARouterUrl.DATAMANAGE_DETAIL_ACT,bundle)
//        }
//        adapter1.setNewInstance(result)

        var adapter2  = PictureShowAdapter()

        val view1 = holder.getView<RecyclerView>(R.id.rvMedicalList)
        view1.layoutManager = GridLayoutManager(context,4)
        view1.adapter = adapter2
        view1.setOnTouchListener { v, event ->
            holder.getView<LinearLayout>(R.id.root).onTouchEvent(event)
        }
        adapter2.setNewInstance(result1)
    }

    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{
        val 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,
        )
    }

}