package com.foxx.androidcast.stats.ui;

/*********************************************************************
 * SessionStatsListAdapter.java
 * Created at: Tue 19 May 2026 19:43:06 +0200
 * Updated at: Wed 20 May 2026 15:17:13 +0200 by Anton Afanasyeu <a.afanasieff@gmail.com>
 * Commit: 5d8e82d2e60a21fff3138d2a394ee4e8b4c6dcb8
 * interface Listener
 * Contributors:
 *    - Anton Afanasyeu <a.afanasieff@gmail.com> (2 commits, 112 lines)
 *    - Cursor Agent (project assistant)
 * Digest: SHA256	01cfd98fe664b1517cd3a8c05259594d0a6772f2482e042eeeff356b2be353f2
**********************************************************************/
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.foxx.androidcast.R;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

final class SessionStatsListAdapter extends RecyclerView.Adapter<SessionStatsListAdapter.Holder> {
    interface Listener {
        void onSelectionChanged(List<SessionRecord> selected);
    }

    private final List<SessionRecord> items = new ArrayList<>();
    private final Set<Integer> selectedPositions = new HashSet<>();
    private final Listener listener;

    SessionStatsListAdapter(Listener listener) {
        this.listener = listener;
    }

    void setItems(List<SessionRecord> records) {
        items.clear();
        selectedPositions.clear();
        if (records != null) {
            items.addAll(records);
            for (int i = 0; i < items.size(); i++) {
                selectedPositions.add(i);
            }
        }
        notifyDataSetChanged();
        notifySelection();
    }

    List<SessionRecord> getSelected() {
        List<SessionRecord> out = new ArrayList<>();
        for (int i : selectedPositions) {
            if (i >= 0 && i < items.size()) {
                out.add(items.get(i));
            }
        }
        return out;
    }

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

    @Override
    public void onBindViewHolder(@NonNull Holder holder, int position) {
        SessionRecord r = items.get(position);
        holder.title.setText(r.fileName);
        holder.subtitle.setText(r.listSubtitle());
        boolean selected = selectedPositions.contains(position);
        holder.itemView.setAlpha(selected ? 1f : 0.45f);
        holder.itemView.setOnClickListener(v -> {
            int pos = holder.getBindingAdapterPosition();
            if (pos == RecyclerView.NO_POSITION) {
                return;
            }
            if (selectedPositions.contains(pos)) {
                selectedPositions.remove(pos);
            } else {
                selectedPositions.add(pos);
            }
            notifyItemChanged(pos);
            notifySelection();
        });
    }

    private void notifySelection() {
        if (listener != null) {
            listener.onSelectionChanged(getSelected());
        }
    }

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

    static final class Holder extends RecyclerView.ViewHolder {
        final TextView title;
        final TextView subtitle;

        Holder(@NonNull View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.text_session_title);
            subtitle = itemView.findViewById(R.id.text_session_subtitle);
        }
    }
}
