package com.zj365.health.act.datamanage

import android.annotation.SuppressLint
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.view.isVisible
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.GsonUtils
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.xty.base.adapter.BaseAdapter
import com.xty.common.SizeUtil
import com.xty.common.arouter.ARouterUrl
import com.xty.common.arouter.RouteManager
import com.zj365.health.R
import com.zj365.health.adapter.PictureShowAdapter
import com.xty.network.model.MedicalDataBean

class YShiDataManageListAdapter :
    BaseAdapter<MedicalDataBean>(R.layout.item_yshi_data_manage_list) {

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

    var isSelect = false

    @SuppressLint("SetTextI18n")
    override fun convert(holder: BaseViewHolder, item: MedicalDataBean) {
        val colorIndex = when (item.upType) {
            1 -> {
                0
            }

            3, 4 -> {
                2
            }

            else -> {
                1
            }
        }
        //上传时间
        val uploadTime = holder.getView<TextView>(R.id.uploadTime)
        //上传者名称
        val uploadStatus = holder.getView<TextView>(R.id.uploadStatus)
        //用餐时间
        val eatTime = holder.getView<TextView>(R.id.eatTime)
        //用餐说明
        val eatExplain = holder.getView<TextView>(R.id.eatExplain)
        //图片列表布局容器
        val imgListLayout = holder.getView<LinearLayout>(R.id.llImgList)
        //图片列表
        val imgList = holder.getView<RecyclerView>(R.id.rvImgList)
        //是否选中
        val selected = holder.getView<ImageView>(R.id.ivSelected)

        uploadTime.text = "上传时间：${item.createTime}"
        val mealTypeStr = if (TextUtils.isEmpty(item.mealType)) {
            "0"
        } else {
            item.mealType!!
        }
        val timeStr = if (TextUtils.isEmpty(item.time)) {
            ""
        } else {
            item.time!!.substring(0, item.time!!.length - 3)
        }
        eatTime.text = "用餐时间：${timeStr} | ${getEatType(mealTypeStr.toInt())}"
        val mealExplainStr = if (TextUtils.isEmpty(item.mealExplain)) {
            "无"
        } else {
            item.mealExplain
        }
        eatExplain.text = "用餐说明：${mealExplainStr}"
        uploadStatus.apply {
            setBackgroundResource(bg[colorIndex])
            setTextColor(resources.getColor(textColor[colorIndex]))
            setPadding(
                SizeUtil.dp2px(context, 10f),
                SizeUtil.dp2px(context, 5f),
                SizeUtil.dp2px(context, 10f),
                SizeUtil.dp2px(context, 5f)
            )
            text = getDataType(item.upType!!)
        }
        //是否选中
        if (item.upType != 3) {
            selected.isVisible = isSelect
        } else {
            selected.isVisible = false
        }
        selected.isSelected = item.isSelectd

        //列表配置
        val mealList = item.mealList
        if (mealList != null && mealList.size > 0) {
            imgListLayout.visibility = View.VISIBLE
            val result = mutableListOf<String>()
            val adapter = PictureShowAdapter()
            imgList.layoutManager = GridLayoutManager(context, 4)
            imgList.adapter = adapter
            mealList.forEachIndexed { index, data ->
                if (index < 4) {
                    result.add(data.imageUrl.toString())
                }
            }
            adapter.setNewInstance(result)

            adapter.setOnItemClickListener { _, _, _ ->
                val bundle = Bundle()
                bundle.putString("data", GsonUtils.toJson(item))
                RouteManager.goAct(ARouterUrl.DATAMANAGE_YSHI_DETAILS_ACT, bundle)
            }
        } else {
            imgListLayout.visibility = View.GONE
        }
    }

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

            2 -> {
                "管家上传"
            }

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

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

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

    private fun getEatType(mealType: Int): String {
        val mealTypeStr = when (mealType) {
            1 -> {
                "早餐"
            }

            2 -> {
                "午餐"
            }

            3 -> {
                "晚餐"
            }

            4 -> {
                "早餐加餐"
            }

            5 -> {
                "午餐加餐"
            }

            6 -> {
                "晚餐加餐"
            }

            else -> {
                "其它"
            }
        }
        return mealTypeStr
    }

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

    @SuppressLint("NotifyDataSetChanged")
    fun reset() {
        for (datum in data) {
            datum.isSelectd = false
        }
        notifyDataSetChanged()
    }
}