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.ItemAdoptionBinding;
import com.example.petstoremobile.dtos.AdoptionDTO;
import com.example.petstoremobile.utils.BulkDeleteHandler;
import com.example.petstoremobile.utils.SelectionHelper;
import java.util.List;

public class AdoptionAdapter extends RecyclerView.Adapter<AdoptionAdapter.AdoptionViewHolder> implements BulkDeleteHandler.SelectableAdapter {

    private List<AdoptionDTO> adoptionList;
    private OnAdoptionClickListener listener;
    private final SelectionHelper selectionHelper;

    public interface OnAdoptionClickListener {
        void onAdoptionClick(int position);
        void onSelectionChanged(int count);
    }

    public AdoptionAdapter(List<AdoptionDTO> adoptionList, OnAdoptionClickListener listener) {
        this.adoptionList = adoptionList;
        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 AdoptionViewHolder extends RecyclerView.ViewHolder {
        final ItemAdoptionBinding binding;

        public AdoptionViewHolder(@NonNull ItemAdoptionBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }

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

    @Override
    public void onBindViewHolder(@NonNull AdoptionViewHolder holder, int position) {
        AdoptionDTO a = adoptionList.get(position);
        ItemAdoptionBinding binding = holder.binding;

        binding.tvAdoptionCustomerName.setText(a.getCustomerName() != null ? a.getCustomerName() : "");
        binding.tvAdoptionPetName.setText("Pet: " + (a.getPetName() != null ? a.getPetName() : ""));
        binding.tvAdoptionStaffName.setText("Staff: " + (a.getEmployeeName() != null ? a.getEmployeeName() : "N/A"));
        binding.tvAdoptionDate.setText("Date: " + (a.getAdoptionDate() != null ? a.getAdoptionDate() : ""));
        binding.tvAdoptionFee.setText(a.getAdoptionFee() != null ? "$" + a.getAdoptionFee() : "");


        String status = a.getAdoptionStatus() != null ? a.getAdoptionStatus() : "";
        binding.tvAdoptionStatus.setText(status);

        switch (status) {
            case "Completed":
                binding.tvAdoptionStatus.setBackgroundColor(Color.parseColor("#4CAF50"));
                break;
            case "Pending":
                binding.tvAdoptionStatus.setBackgroundColor(Color.parseColor("#FF9800"));
                break;
            case "Cancelled":
                binding.tvAdoptionStatus.setBackgroundColor(Color.parseColor("#F44336"));
                break;
            default:
                binding.tvAdoptionStatus.setBackgroundColor(Color.parseColor("#9E9E9E"));
                break;
        }

        String key = String.valueOf(a.getAdoptionId());

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

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

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

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