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

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import es.verdnatura.R
import es.verdnatura.databinding.ItemArticleRowReubicationFragmentBinding
import es.verdnatura.presentation.common.OnPasillerosItemClickListener
import es.verdnatura.presentation.common.OnReubicationClickListener
import es.verdnatura.presentation.view.feature.pasillero.model.PasillerosItemVO
import es.verdnatura.presentation.view.feature.reubication.model.Reubication


class ReubicatorAdapter(
    private val items: List<Reubication>,
    private val onPasillerosItemClickListener: OnPasillerosItemClickListener,
    private val onReubicationClickListener: OnReubicationClickListener
) : RecyclerView.Adapter<ReubicatorAdapter.AjustesItemHolder>() {
    var context: Context? = null
    var position: Int = 0
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AjustesItemHolder {
        this.context = parent.context
        return AjustesItemHolder(

            ItemArticleRowReubicationFragmentBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false
            )
        )
    }

    override fun getItemCount() = items.size

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

    inner class AjustesItemHolder(
        val binding: ItemArticleRowReubicationFragmentBinding
    ) : RecyclerView.ViewHolder(binding.root) {
        fun bind(reubication: Reubication) {
            binding.apply {

                itemArticleItemFk.setOnClickListener {
                    onPasillerosItemClickListener.onPasillerosItemClickListener(
                        PasillerosItemVO(
                            title = binding.root.context.getString(R.string.titleItemConsult)
                        ), reubication.itemFk.toString()
                    )
                }
                contentLayout.setOnClickListener {
                    onReubicationClickListener.onReubicationClick(reubication)
                }
                val childLayoutManager =
                    LinearLayoutManager(context!!, RecyclerView.HORIZONTAL, false)

                itemArticlePlacements.apply {
                    layoutManager = childLayoutManager
                    adapter = reubication.carros.let {
                        ReubicatorPlacementAdapter(
                            it,
                            onPasillerosItemClickListener
                        )
                    }
                }


                //ASIGNAMOS VALOR A LA VSITA
                this.reubication = reubication

            }
        }
    }


}