package com.quyunshuo.module.home.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.recyclerview.widget.RecyclerView
import com.quyunshuo.androidbaseframemvvm.base.addressenum.PayEnum
import com.quyunshuo.androidbaseframemvvm.common.bean.ProductDataBean
import com.quyunshuo.androidbaseframemvvm.common.bean.ShoppingCartBean
import com.quyunshuo.androidbaseframemvvm.common.listener.AdapterClickListener
import com.quyunshuo.androidbaseframemvvm.common.listener.ShoppingAdapterClickListener
import com.quyunshuo.androidbaseframemvvm.common.util.UiUtil
import com.quyunshuo.androidbaseframemvvm.common.util.XLogUtil
import com.quyunshuo.module.home.R
import com.quyunshuo.module.home.databinding.HomeItemShoppingCartBinding

/**
 * 购物车内容列表
 */
class ShoppingCartAdapter(var productList:List<ShoppingCartBean>): RecyclerView.Adapter<ShoppingCartAdapter.MyViewHolder>() {
    private val TAG = "HomeProductAdapter"
    private val _position = MutableLiveData<Int>()
    val clickPosition:LiveData<Int> =  _position
    var listener: ShoppingAdapterClickListener? = null

    inner class MyViewHolder(binding: HomeItemShoppingCartBinding): RecyclerView.ViewHolder(binding.root){
        public val mBinding = binding
        fun bind(payTypeBean: ShoppingCartBean) {
            mBinding.run {
                val valueOf = PayEnum.getEnumByValue(payTypeBean.nameChinese)
                tvName.setText(UiUtil.getResId(payTypeBean.nameId, R.string::class.java))
                tvPrice.text = "单价："+payTypeBean.price
                tvDelete.setOnClickListener{
                    listener?.onClickListener(it, this@MyViewHolder.absoluteAdapterPosition,"",tvCount)
                }
                tvAdd.setOnClickListener{
                    tvCount.text = (tvCount.text.toString().toInt()+1).toString()
                    listener?.onClickListener(it, this@MyViewHolder.absoluteAdapterPosition,tvCount.text.toString(),tvCount)
                }
                tvSubtract.setOnClickListener {
                    tvCount.text = (tvCount.text.toString().toInt()-1).toString()
                    listener?.onClickListener(it, this@MyViewHolder.absoluteAdapterPosition,tvCount.text.toString(),tvCount)
                }
                tvCount.text = payTypeBean.count.toString()
            }
        }


    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val projectVH = MyViewHolder(
            HomeItemShoppingCartBinding.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[position])

    }
    fun setOnclickListener(listener:ShoppingAdapterClickListener){
        this.listener = listener
    }

    fun setData(productList:List<ShoppingCartBean>){
        this.productList=productList
    }}