package com.streamplayer;

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 java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class EventAdapter extends RecyclerView.Adapter<EventAdapter.EventViewHolder> {

    public interface OnEventClickListener {
        void onEventClick(EventItem event);
    }

    private final List<EventItem> events = new ArrayList<>();
    private final OnEventClickListener listener;

    public EventAdapter(OnEventClickListener listener) {
        this.listener = listener;
    }

    public void submitList(List<EventItem> newEvents) {
        events.clear();
        events.addAll(newEvents);
        notifyDataSetChanged();
    }

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

    @Override
    public void onBindViewHolder(@NonNull EventViewHolder holder, int position) {
        EventItem event = events.get(position);
        holder.title.setText(event.getTitle());
        holder.time.setText(event.getTime());
        holder.channel.setText(event.getChannelName());
        holder.status.setText(buildStatusText(event));
        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onEventClick(event);
            }
        });
    }

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

    static class EventViewHolder extends RecyclerView.ViewHolder {
        final TextView title;
        final TextView time;
        final TextView channel;
        final TextView status;

        EventViewHolder(@NonNull View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.event_title);
            time = itemView.findViewById(R.id.event_time);
            channel = itemView.findViewById(R.id.event_channel);
            status = itemView.findViewById(R.id.event_status);
        }
    }

    private String buildStatusText(EventItem event) {
        long start = event.getStartMillis();
        if (start <= 0) {
            return event.getStatus();
        }
        long now = System.currentTimeMillis();
        long diff = start - now;
        if (diff > 0) {
            long hours = diff / 3600000;
            long minutes = (diff % 3600000) / 60000;
            if (hours > 0) {
                return String.format(Locale.getDefault(), "En %dh %02dm", hours, minutes);
            } else {
                return String.format(Locale.getDefault(), "En %d min", Math.max(1, minutes));
            }
        } else if (Math.abs(diff) <= 2 * 3600000L) {
            return "En vivo";
        } else {
            return "Finalizado";
        }
    }
}
