package com.example.petstoremobile.adapters;

import android.view.LayoutInflater;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.petstoremobile.databinding.ItemPurchaseOrderBinding;
import com.example.petstoremobile.dtos.PurchaseOrderDTO;
import java.util.List;

public class PurchaseOrderAdapter extends RecyclerView.Adapter<PurchaseOrderAdapter.POViewHolder> {

    private List<PurchaseOrderDTO> list;
    private OnPurchaseOrderClickListener listener;

    public interface OnPurchaseOrderClickListener {
        void onPurchaseOrderClick(int position);
    }

    public PurchaseOrderAdapter(List<PurchaseOrderDTO> list, OnPurchaseOrderClickListener listener) {
        this.list = list;
        this.listener = listener;
    }

    public static class POViewHolder extends RecyclerView.ViewHolder {
        final ItemPurchaseOrderBinding binding;

        public POViewHolder(@NonNull ItemPurchaseOrderBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }

    @NonNull
    @Override
    public POViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemPurchaseOrderBinding binding = ItemPurchaseOrderBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        return new POViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull POViewHolder holder, int position) {
        PurchaseOrderDTO po = list.get(position);
        ItemPurchaseOrderBinding binding = holder.binding;

        binding.tvPOId.setText("PO #" + (po.getPurchaseOrderId() != null ? po.getPurchaseOrderId() : ""));
        binding.tvPOSupplier.setText("Supplier: " + (po.getSupplierName() != null ? po.getSupplierName() : ""));
        binding.tvPOStore.setText("Store: " + (po.getStoreName() != null ? po.getStoreName() : ""));
        binding.tvPODate.setText("Date: " + (po.getOrderDate() != null ? po.getOrderDate() : ""));

        holder.itemView.setOnClickListener(v -> listener.onPurchaseOrderClick(position));
    }

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