package com.example.pap_findu.adapters;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;
import com.example.pap_findu.R;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.pap_findu.models.AlertMessage;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class HistoryAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE_HEADER = 0;
    private static final int TYPE_EVENT = 1;
    private List<Object> items = new ArrayList<>();
    private Context context;

    // Construtor atualizado para receber o contexto
    public HistoryAdapter(Context context) {
        this.context = context;
    }

    public void setItems(List<Object> items) {
        this.items = items;
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        if (items.get(position) instanceof String) return TYPE_HEADER;
        return TYPE_EVENT;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == TYPE_HEADER) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_history_header, parent, false);
            return new HeaderViewHolder(view);
        } else {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_history_event, parent, false);
            return new EventViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position) == TYPE_HEADER) {
            ((HeaderViewHolder) holder).tvHeaderDate.setText((String) items.get(position));
        } else {
            AlertMessage alert = (AlertMessage) items.get(position);
            EventViewHolder ev = (EventViewHolder) holder;

            ev.tvEventTitle.setText(alert.getTitle() != null ? alert.getTitle() : "Evento");
            ev.tvEventDesc.setText(alert.getMessage() != null ? alert.getMessage() : "");

            if (alert.getTimestamp() != null) {
                SimpleDateFormat sdf = new SimpleDateFormat("HH:mm", Locale.getDefault());
                ev.tvEventTime.setText(sdf.format(alert.getTimestamp()));
            }

            String title = alert.getTitle() != null ? alert.getTitle().toLowerCase() : "";
            if (title.contains("entrada")) {
                ev.tvEventIconText.setText("🏠");
                ev.flEventIconBg.setBackgroundColor(Color.parseColor("#E8F5E9"));
            } else if (title.contains("saída")) {
                ev.tvEventIconText.setText("🚪");
                ev.flEventIconBg.setBackgroundColor(Color.parseColor("#FFF3E0"));
            } else if (title.contains("movimento") || title.contains("deslocação")) {
                ev.tvEventIconText.setText("🚶");
                ev.flEventIconBg.setBackgroundColor(Color.parseColor("#E0E7FF"));
            } else if (title.contains("paragem")) {
                ev.tvEventIconText.setText("🛑");
                ev.flEventIconBg.setBackgroundColor(Color.parseColor("#FEE2E2"));
            } else {
                ev.tvEventIconText.setText("📌");
                ev.flEventIconBg.setBackgroundColor(Color.parseColor("#F3F4F6"));
            }
        }
    }

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

    static class HeaderViewHolder extends RecyclerView.ViewHolder {
        TextView tvHeaderDate;
        HeaderViewHolder(View itemView) { super(itemView); tvHeaderDate = itemView.findViewById(R.id.tvHeaderDate); }
    }

    static class EventViewHolder extends RecyclerView.ViewHolder {
        FrameLayout flEventIconBg;
        TextView tvEventIconText, tvEventTitle, tvEventDesc, tvEventTime;
        EventViewHolder(View itemView) {
            super(itemView);
            flEventIconBg = itemView.findViewById(R.id.flEventIconBg);
            tvEventIconText = itemView.findViewById(R.id.tvEventIconText);
            tvEventTitle = itemView.findViewById(R.id.tvEventTitle);
            tvEventDesc = itemView.findViewById(R.id.tvEventDesc);
            tvEventTime = itemView.findViewById(R.id.tvEventTime);
        }
    }
}