package com.example.tyre.maintenance.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.example.tyre.BR;
import com.example.tyre.R;
import com.example.tyre.databinding.ItemOrderInfoBinding;
import com.example.tyre.maintenance.been.Order;

import java.util.List;

public class OrderAdapter extends RecyclerView.Adapter<OrderAdapter.MyViewHolder> {

    private List<Order> list;
    private Context context;
    private LayoutInflater inflater;
    private SelectTagAdapterListener listener;

    public OrderAdapter(Context context, SelectTagAdapterListener listener) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        this.listener = listener;
    }

    public void setList(List<Order> list) {
        this.list = list;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemOrderInfoBinding binding =
                DataBindingUtil.inflate(inflater, R.layout.item_order_info, parent, false);
        return new MyViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        var tag = list.get(position);
        holder.binding.setVariable(BR.item, tag);
        holder.binding.itemOrderLayout.setOnClickListener(v -> {
            listener.onTagClick(position);
        });


    }


    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public interface SelectTagAdapterListener {
         void onTagClick(int position);
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        private ItemOrderInfoBinding binding;

        public MyViewHolder(@NonNull ItemOrderInfoBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

    }
}
