package com.rdiot.yx485.adapter

import android.content.Context
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.rdiot.yx485.R
import com.rdiot.yx485.adapter.DeviceIconListAdapter.DeviceIconView
import com.rdiot.yx485.bean.ModelRepleBean.DataBean.DeviceInfosBean
import com.rdiot.yx485.databinding.ItemDeviceIconBinding
import com.rdiot.yx485.http.Url

class DeviceIconListAdapter(val list: List<DeviceInfosBean>?, context: Context) :
    RecyclerView.Adapter<DeviceIconView>() {

    private var mContext = context
    private var mList: List<DeviceInfosBean>? = list

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): DeviceIconView {
        val binding = ItemDeviceIconBinding.inflate(LayoutInflater.from(mContext), parent, false)
        return DeviceIconView(binding)
    }

    override fun onBindViewHolder(
        holder: DeviceIconView,
        position: Int
    ) {
        holder.setIsRecyclable(false)
        holder.bindView(position)
    }

    override fun getItemCount(): Int {
        return mList?.size ?: 0
    }

    inner class DeviceIconView(val binding: ItemDeviceIconBinding) :
        RecyclerView.ViewHolder(binding.root) {

        fun bindView(position: Int) {
            val device = mList?.get(position)
            device?.let {
                if (!TextUtils.isEmpty(it.device_icon)) {
                    Glide.with(mContext)
                        .load(Url.BASE_URL + it.device_icon)
                        .error(Glide.with(mContext).load(R.drawable.find_d))
                        .into(binding.ivDeviceIcon)
                }
            }

        }
    }
}