package com.example.petstoremobile.utils;

import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;
import androidx.lifecycle.LiveData;

import java.util.List;

/**
 * A helper class to handle the UI and logic for bulk deletion across different fragments.
 * Now supports String keys to accommodate both simple and composite keys.
 */
public class BulkDeleteHandler {

    /**
     * Interface that adapters must implement to support bulk selection.
     */
    public interface SelectableAdapter {
        List<String> getSelectedKeys();
        void clearSelection();
    }

    /**
     * Functional interface for the API call execution.
     */
    public interface BulkDeleteOperation {
        LiveData<Resource<Void>> execute(List<String> keys);
    }

    private final Fragment fragment;
    private final View layoutBar;
    private final TextView tvCount;
    private final SelectableAdapter adapter;
    private final BulkDeleteOperation operation;
    private final Runnable onSuccess;
    private final String itemName;

    public BulkDeleteHandler(Fragment fragment, 
                             View layoutBar, 
                             TextView tvCount, 
                             Button btnDelete,
                             SelectableAdapter adapter, 
                             String itemName,
                             BulkDeleteOperation operation, 
                             Runnable onSuccess) {
        this.fragment = fragment;
        this.layoutBar = layoutBar;
        this.tvCount = tvCount;
        this.adapter = adapter;
        this.operation = operation;
        this.onSuccess = onSuccess;
        this.itemName = itemName;

        btnDelete.setOnClickListener(v -> confirmDelete());
    }

    /**
     * Updates the UI when the selection count changes.
     */
    public void onSelectionChanged(int selectedCount) {
        if (selectedCount > 0) {
            layoutBar.setVisibility(View.VISIBLE);
            tvCount.setText(selectedCount + " selected");
        } else {
            hideBar();
        }
    }

    /**
     * Hides the bulk delete bar and resets state.
     */
    public void hideBar() {
        if (layoutBar != null) {
            layoutBar.setVisibility(View.GONE);
        }
    }

    /**
     * Shows the confirmation dialog.
     */
    private void confirmDelete() {
        List<String> keys = adapter.getSelectedKeys();
        if (keys.isEmpty()) return;

        DialogUtils.showBulkDeleteConfirmDialog(fragment.requireContext(), keys.size(), () -> performDelete(keys));
    }

    /**
     * Executes the deletion via the provided operation.
     */
    private void performDelete(List<String> keys) {
        operation.execute(keys).observe(fragment.getViewLifecycleOwner(), resource -> {
            if (resource != null && resource.status != Resource.Status.LOADING) {
                if (resource.status == Resource.Status.SUCCESS) {
                    adapter.clearSelection();
                    hideBar();
                    onSuccess.run();
                    Toast.makeText(fragment.getContext(), keys.size() + " " + itemName + "(s) deleted", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(fragment.getContext(), "Delete failed: " + resource.message, Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}
