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

import java.util.List;

/**
 * Adapter class for displaying a list of services in a RecyclerView.
 */
public class ServiceAdapter extends RecyclerView.Adapter<ServiceAdapter.ServiceViewHolder> implements BulkDeleteHandler.SelectableAdapter {

    private final List<ServiceDTO> serviceList;
    private final OnServiceClickListener clickListener;
    private final SelectionHelper selectionHelper;

    /**
     * Interface for handling clicks on service items.
     */
    public interface OnServiceClickListener {
        void onServiceClick(int position);
        void onSelectionChanged(int count);
    }

    public ServiceAdapter(List<ServiceDTO> serviceList, OnServiceClickListener clickListener) {
        this.serviceList = serviceList;
        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();
    }

    /**
     * ViewHolder class for service items.
     */
    public static class ServiceViewHolder extends RecyclerView.ViewHolder {
        final ItemServiceBinding binding;

        public ServiceViewHolder(@NonNull ItemServiceBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }

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

    //populate the row with service data
    @Override
    public void onBindViewHolder(@NonNull ServiceViewHolder holder, int position) {
        ServiceDTO service = serviceList.get(position);
        ItemServiceBinding binding = holder.binding;

        binding.tvServiceName.setText(service.getServiceName());
        binding.tvServiceDesc.setText(service.getServiceDesc());
        binding.tvServiceDuration.setText(service.getServiceDuration() != null ? service.getServiceDuration() + " mins" : "0 mins");
        binding.tvServicePrice.setText(service.getServicePrice() != null ? "$" + String.format("%.2f", service.getServicePrice()) : "$0.00");

        String key = String.valueOf(service.getServiceId());

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

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

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

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