package com.example.cuida.ui.medication;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.cuida.R;
import com.example.cuida.data.model.Comprimido;
import java.util.List;

public class ComprimidoRecyclerAdapter extends RecyclerView.Adapter<ComprimidoRecyclerAdapter.ViewHolder> {

    private List<Comprimido> pills;
    private OnItemClickListener listener;

    public interface OnItemClickListener {
        void onItemClick(Comprimido comprimido);
    }

    public ComprimidoRecyclerAdapter(List<Comprimido> pills, OnItemClickListener listener) {
        this.pills = pills;
        this.listener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_comprimido_search, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Comprimido pill = pills.get(position);
        holder.textName.setText(pill.nome);
        holder.itemView.setOnClickListener(v -> listener.onItemClick(pill));
    }

    @Override
    public int getItemCount() {
        return pills.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView textName;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            textName = itemView.findViewById(R.id.text_pill_name);
        }
    }
}
