package com.example.petstoremobile.adapters;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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

import com.example.petstoremobile.databinding.ItemProductSupplierBinding;
import com.example.petstoremobile.dtos.ProductSupplierDTO;
import com.example.petstoremobile.utils.BulkDeleteHandler;
import com.example.petstoremobile.utils.SelectionHelper;

import java.util.List;

public class ProductSupplierAdapter extends RecyclerView.Adapter<ProductSupplierAdapter.PSViewHolder> implements BulkDeleteHandler.SelectableAdapter {

    private final List<ProductSupplierDTO> list;
    private final OnProductSupplierClickListener listener;
    private final SelectionHelper selectionHelper;

    public interface OnProductSupplierClickListener {
        void onProductSupplierClick(int position);
        void onSelectionChanged(int count);
    }

    public ProductSupplierAdapter(List<ProductSupplierDTO> list, OnProductSupplierClickListener listener) {
        this.list = list;
        this.listener = listener;
        this.selectionHelper = new SelectionHelper(new SelectionHelper.SelectionListener() {
            @Override
            public void onSelectionChanged(int count) {
                listener.onSelectionChanged(count);
            }

            @Override
            public void onSelectionModeToggle(boolean selectionMode) {
                notifyDataSetChanged();
            }
        });
    }

    @Override
    public List<String> getSelectedKeys() {
        return selectionHelper.getSelectedKeys();
    }

    @Override
    public void clearSelection() {
        selectionHelper.clearSelection();
    }

    public static class PSViewHolder extends RecyclerView.ViewHolder {
        final ItemProductSupplierBinding binding;

        public PSViewHolder(@NonNull ItemProductSupplierBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }

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

    @Override
    public void onBindViewHolder(@NonNull PSViewHolder holder, int position) {
        ProductSupplierDTO ps = list.get(position);
        ItemProductSupplierBinding binding = holder.binding;

        binding.tvPSProductName.setText(ps.getProductName() != null ? ps.getProductName() : "");
        binding.tvPSSupplierName.setText("Supplier: " + (ps.getSupplierName() != null ? ps.getSupplierName() : ""));
        binding.tvPSCost.setText(ps.getCost() != null ? "Cost: $" + ps.getCost() : "");

        String key = ps.getProductId() + "-" + ps.getSupplierId();

        // Bulk delete selection mode
        if (selectionHelper.isInSelectionMode()) {
            binding.cbSelectProductSupplier.setVisibility(View.VISIBLE);
            binding.cbSelectProductSupplier.setChecked(selectionHelper.isSelected(key));
        } else {
            binding.cbSelectProductSupplier.setVisibility(View.GONE);
            binding.cbSelectProductSupplier.setChecked(false);
        }

        holder.itemView.setOnClickListener(v -> {
            if (selectionHelper.isInSelectionMode()) {
                selectionHelper.toggleSelection(key);
                notifyItemChanged(position);
            } else {
                listener.onProductSupplierClick(position);
            }
        });

        holder.itemView.setOnLongClickListener(v -> {
            if (!selectionHelper.isInSelectionMode()) {
                selectionHelper.startSelection(key);
            }
            return true;
        });
    }

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