package com.example.cuida.ui.appointments;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.cuida.R;
import com.example.cuida.data.model.Appointment;
import java.util.ArrayList;
import java.util.List;

public class AppointmentAdapter extends RecyclerView.Adapter<AppointmentAdapter.AppointmentViewHolder> {

    private List<Appointment> appointmentList = new ArrayList<>();

    public void setAppointments(List<Appointment> appointments) {
        this.appointmentList = appointments;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public AppointmentViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_appointment, parent, false);
        return new AppointmentViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull AppointmentViewHolder holder, int position) {
        Appointment appointment = appointmentList.get(position);
        holder.textType.setText(appointment.type);
        holder.textDate.setText(appointment.date);
        holder.textTime.setText(appointment.time);
        holder.textReason.setText("Motivo: " + (appointment.reason != null ? appointment.reason : "--"));
        
        String status = appointment.status != null ? appointment.status : "Pendente";
        holder.textStatus.setText(status);
        
        if ("Aceite".equalsIgnoreCase(status)) {
            holder.textStatus.setTextColor(android.graphics.Color.parseColor("#388E3C")); // Green
            holder.textStatus.setBackgroundColor(android.graphics.Color.parseColor("#C8E6C9"));
        } else if ("Rejeitada".equalsIgnoreCase(status)) {
            holder.textStatus.setTextColor(android.graphics.Color.parseColor("#D32F2F")); // Red
            holder.textStatus.setBackgroundColor(android.graphics.Color.parseColor("#FFCDD2"));
        } else {
            holder.textStatus.setTextColor(android.graphics.Color.parseColor("#F57C00")); // Orange
            holder.textStatus.setBackgroundColor(android.graphics.Color.parseColor("#FFE0B2"));
            holder.textStatus.setText("Pendente");
        }
    }

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

    public static class AppointmentViewHolder extends RecyclerView.ViewHolder {
        TextView textType, textDate, textTime, textReason, textStatus;

        public AppointmentViewHolder(@NonNull View itemView) {
            super(itemView);
            textType = itemView.findViewById(R.id.text_type);
            textDate = itemView.findViewById(R.id.text_date);
            textTime = itemView.findViewById(R.id.text_time);
            textReason = itemView.findViewById(R.id.text_reason);
            textStatus = itemView.findViewById(R.id.text_status);
        }
    }
}
