package es.verdnatura.presentation.view.feature.collection.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import es.verdnatura.R
import es.verdnatura.databinding.ItemPlacementRowBinding
import es.verdnatura.presentation.common.OnPasillerosItemClickListener
import es.verdnatura.presentation.view.feature.pasillero.model.PasillerosItemVO
import es.verdnatura.presentation.view.feature.sacador.model.PlacementVO

class PlacementAdapter (
    private val items: List<PlacementVO>,
    private val onPasillerosItemClickListener: OnPasillerosItemClickListener
): RecyclerView.Adapter<PlacementAdapter.AjustesItemHolder> () {
    var context: Context? = null
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AjustesItemHolder {
        this.context = parent.context
        return AjustesItemHolder(
            ItemPlacementRowBinding.inflate(LayoutInflater.from(parent.context),parent,false)
        )
    }

    override fun getItemCount() =items.size

    override fun onBindViewHolder(holder: AjustesItemHolder, position: Int) {
        holder.bind(items[position])
    }

    inner class AjustesItemHolder(
        val binding: ItemPlacementRowBinding
    ) : RecyclerView.ViewHolder(binding.root){
        fun bind(placement: PlacementVO) {
            binding.apply {
                if (placement.placement.isNullOrEmpty())
                    placement.placement = placement.parking
                if (placement.visible.isNullOrEmpty())
                    placement.visible = "("+placement.stockTotal+")"
                this.item = placement

                itemRootLayout.setOnClickListener {
                    onPasillerosItemClickListener.onPasillerosItemClickListener(PasillerosItemVO(title = binding.root.context.getString(
                        R.string.titleUbicator)),placement.shelving)
                }
            }
        }
    }
}