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.ItemSupplierBinding;
import com.example.petstoremobile.dtos.SupplierDTO;
import com.example.petstoremobile.utils.BulkDeleteHandler;
import com.example.petstoremobile.utils.SelectionHelper;

import java.util.List;

public class SupplierAdapter extends RecyclerView.Adapter<SupplierAdapter.SupplierViewHolder> implements BulkDeleteHandler.SelectableAdapter {

    private final List<SupplierDTO> supplierList;
    private final OnSupplierClickListener supplierClickListener;
    private final SelectionHelper selectionHelper;

    // Interface for supplier click on recycler view
    public interface OnSupplierClickListener {
        void onSupplierClick(int position);
        void onSelectionChanged(int count);
    }

    //Constructor
    public SupplierAdapter(List<SupplierDTO> supplierList, OnSupplierClickListener supplierClickListener) {
        this.supplierList  = supplierList;
        this.supplierClickListener = supplierClickListener;
        this.selectionHelper = new SelectionHelper(new SelectionHelper.SelectionListener() {
            @Override
            public void onSelectionChanged(int count) {
                supplierClickListener.onSelectionChanged(count);
            }

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

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

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

    // Get the controls of each row in recycler view
    public static class SupplierViewHolder extends RecyclerView.ViewHolder {
        final ItemSupplierBinding binding;

        public SupplierViewHolder(@NonNull ItemSupplierBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }

    // Create a new row view
    @NonNull
    @Override
    public SupplierViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemSupplierBinding binding = ItemSupplierBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        return new SupplierViewHolder(binding);
    }

    //populate the row with supplier data
    @Override
    public void onBindViewHolder(@NonNull SupplierViewHolder holder, int position) {
        SupplierDTO supplier = supplierList.get(position);
        ItemSupplierBinding binding = holder.binding;

        binding.tvSupCompany.setText(supplier.getSupCompany());
        binding.tvSupContactName.setText(supplier.getSupContactFirstName() + " " + supplier.getSupContactLastName());
        binding.tvSupEmail.setText(supplier.getSupEmail());
        binding.tvSupPhone.setText(supplier.getSupPhone());

        String key = String.valueOf(supplier.getSupId());

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

        //when a row is clicked, open the detail view
        holder.itemView.setOnClickListener(v -> {
            if (selectionHelper.isInSelectionMode()) {
                selectionHelper.toggleSelection(key);
                notifyItemChanged(position);
            } else {
                supplierClickListener.onSupplierClick(position);
            }
        });

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

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