package com.example.vdcscore.ui.cup;

import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.vdcscore.databinding.ItemCupPhaseBinding;
import com.example.vdcscore.ui.gallery.MatchesAdapter;

import java.util.ArrayList;
import java.util.List;

public class CupPhasesAdapter extends RecyclerView.Adapter<CupPhasesAdapter.PhaseViewHolder> {

    private List<CupPhase> phasesList = new ArrayList<>();

    public void setPhases(List<CupPhase> phases) {
        this.phasesList = phases;
        notifyDataSetChanged();
    }

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

    @Override
    public void onBindViewHolder(@NonNull PhaseViewHolder holder, int position) {
        CupPhase phase = phasesList.get(position);
        holder.binding.textPhaseName.setText(phase.getName());
        
        MatchesAdapter matchesAdapter = new MatchesAdapter();
        matchesAdapter.setMatches(phase.getMatches() != null ? phase.getMatches() : new ArrayList<>());
        holder.binding.recyclerPhaseMatches.setAdapter(matchesAdapter);
        holder.binding.recyclerPhaseMatches.setLayoutManager(
            new androidx.recyclerview.widget.LinearLayoutManager(holder.binding.getRoot().getContext()));
    }

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

    static class PhaseViewHolder extends RecyclerView.ViewHolder {
        ItemCupPhaseBinding binding;

        PhaseViewHolder(ItemCupPhaseBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }
}
