package com.module.backstage.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.recyclerview.widget.RecyclerView
import com.module.backstage.R
import com.module.backstage.databinding.BackstageItemPriceBinding
import com.quyunshuo.androidbaseframemvvm.common.listener.AdapterClickListener
import com.quyunshuo.androidbaseframemvvm.common.bean.ProductDataBean
import com.quyunshuo.androidbaseframemvvm.common.util.UiUtil

class PriceAdapter(var productList:ArrayList<ProductDataBean>): RecyclerView.Adapter<PriceAdapter.MyViewHolder>() {
    private val TAG = "HomeProductAdapter"
    private val _position = MutableLiveData<Int>()
    val clickPosition:LiveData<Int> =  _position
    private var itemListener: AdapterClickListener? = null
    init {

    }
    fun setItemListener(itemListener: AdapterClickListener?) {
        this.itemListener = itemListener
    }

    inner class MyViewHolder(binding: BackstageItemPriceBinding): RecyclerView.ViewHolder(binding.root){
        val mBinding = binding

        fun bind(productDataBean: ProductDataBean) {
            mBinding.run {
                ivSugar.setImageResource(UiUtil.getResId(productDataBean.imgID, R.drawable::class.java))
                tvName.setText(UiUtil.getResId(productDataBean.nameId, R.string::class.java))
                etSugarprice.setText(""+productDataBean.price)

            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val projectVH = MyViewHolder(
            BackstageItemPriceBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        )
        return projectVH
    }

    override fun getItemCount(): Int {
        return productList.size
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        holder.bind(productList.get(position))
        itemListener?.onClickListener(holder.itemView, position,"")

    }
}