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

public class AppointmentAdapter extends RecyclerView.Adapter<AppointmentAdapter.AppointmentViewHolder> implements BulkDeleteHandler.SelectableAdapter {

    private List<AppointmentDTO> appointmentList;
    private OnAppointmentClickListener appointmentClickListener;
    private final SelectionHelper selectionHelper;

    public interface OnAppointmentClickListener {
        void onAppointmentClick(int position);
        void onSelectionChanged(int count);
    }

    public AppointmentAdapter(List<AppointmentDTO> appointmentList,
                              OnAppointmentClickListener appointmentClickListener) {
        this.appointmentList = appointmentList;
        this.appointmentClickListener = appointmentClickListener;
        this.selectionHelper = new SelectionHelper(new SelectionHelper.SelectionListener() {
            @Override
            public void onSelectionChanged(int count) {
                appointmentClickListener.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 AppointmentViewHolder extends RecyclerView.ViewHolder {
        private final ItemAppointmentBinding binding;

        public AppointmentViewHolder(@NonNull ItemAppointmentBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }

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

    @Override
    public void onBindViewHolder(@NonNull AppointmentViewHolder holder, int position) {
        AppointmentDTO a = appointmentList.get(position);
        ItemAppointmentBinding binding = holder.binding;

        binding.tvCustomerName.setText(a.getCustomerName() != null ? a.getCustomerName() : "");
        binding.tvApptPetName.setText("Pet: " + (a.getPetName() != null ? a.getPetName() : ""));
        binding.tvServiceType.setText(a.getServiceType() != null ? a.getServiceType() : "");
        binding.tvStaffName.setText("Staff: " + (a.getEmployeeName() != null ? a.getEmployeeName() : "Unassigned"));
        binding.tvDateTime.setText((a.getAppointmentDate() != null ? a.getAppointmentDate() : "") +
                " at " + (a.getAppointmentTime() != null ? a.getAppointmentTime() : ""));

        String status = a.getStatus() != null ? a.getStatus() : "";
        binding.tvAppointmentStatus.setText(status);

        switch (status.toUpperCase()) {
            case "BOOKED":
                binding.tvAppointmentStatus.setBackgroundColor(Color.parseColor("#2196F3")); // blue
                break;
            case "COMPLETED":
                binding.tvAppointmentStatus.setBackgroundColor(Color.parseColor("#4CAF50")); // green
                break;
            case "CANCELLED":
                binding.tvAppointmentStatus.setBackgroundColor(Color.parseColor("#F44336")); // red
                break;
            default:
                binding.tvAppointmentStatus.setBackgroundColor(Color.parseColor("#9E9E9E")); // gray
                break;
        }

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

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

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

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

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