package com.example.petstoremobile.adapters;

import android.graphics.Color;
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.ItemInventoryBinding;
import com.example.petstoremobile.dtos.InventoryDTO;
import com.example.petstoremobile.utils.BulkDeleteHandler;
import com.example.petstoremobile.utils.SelectionHelper;

import java.util.List;

public class InventoryAdapter extends RecyclerView.Adapter<InventoryAdapter.InventoryViewHolder> implements BulkDeleteHandler.SelectableAdapter {

    private final List<InventoryDTO> inventoryList;
    private final OnInventoryClickListener clickListener;
    private final SelectionHelper selectionHelper;

    public interface OnInventoryClickListener {
        void onInventoryClick(int position);

        void onSelectionChanged(int selectedCount);
    }

    public InventoryAdapter(List<InventoryDTO> inventoryList, OnInventoryClickListener clickListener) {
        this.inventoryList = inventoryList;
        this.clickListener = clickListener;
        this.selectionHelper = new SelectionHelper(new SelectionHelper.SelectionListener() {
            @Override
            public void onSelectionChanged(int count) {
                clickListener.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 InventoryViewHolder extends RecyclerView.ViewHolder {
        final ItemInventoryBinding binding;

        public InventoryViewHolder(@NonNull ItemInventoryBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }

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

    @Override
    public void onBindViewHolder(@NonNull InventoryViewHolder holder, int position) {
        InventoryDTO inv = inventoryList.get(position);
        ItemInventoryBinding binding = holder.binding;

        // Column: Product Name
        binding.tvProductName.setText(inv.getProductName() != null ? inv.getProductName() : "—");

        // Column: Store Name
        binding.tvInventoryStore.setText("Store: " + (inv.getStoreName() != null ? inv.getStoreName() : "—"));

        // Column: Quantity
        int qty = inv.getQuantity() != null ? inv.getQuantity() : 0;
        binding.tvQuantity.setText("Stock: " + qty);

        // Low stock = red, normal = green (like desktop reorder concept)
        if (qty <= 5) {
            binding.tvQuantity.setTextColor(Color.parseColor("#F44336"));
        } else {
            binding.tvQuantity.setTextColor(Color.parseColor("#4CAF50"));
        }

        String key = String.valueOf(inv.getInventoryId());

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

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

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

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