package com.example.vdcscore.ui.gallery;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

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

import com.example.vdcscore.R;

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

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

    private List<Matchday> matchdays = new ArrayList<>();
    private RecyclerView.RecycledViewPool viewPool = new RecyclerView.RecycledViewPool();

    public void setMatchdays(List<Matchday> matchdays) {
        this.matchdays = matchdays;
        notifyDataSetChanged();
    }

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

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Matchday matchday = matchdays.get(position);
        holder.textName.setText(matchday.getName());

        // Setup nested RecyclerView
        LinearLayoutManager layoutManager = new LinearLayoutManager(
                holder.recyclerMatches.getContext(),
                LinearLayoutManager.VERTICAL,
                false);
        layoutManager.setInitialPrefetchItemCount(matchday.getMatches().size());

        MatchesAdapter matchesAdapter = new MatchesAdapter(matchday.getMatches());
        holder.recyclerMatches.setLayoutManager(layoutManager);
        holder.recyclerMatches.setAdapter(matchesAdapter);
        holder.recyclerMatches.setRecycledViewPool(viewPool);
    }

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

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public final TextView textName;
        public final RecyclerView recyclerMatches;

        public ViewHolder(View view) {
            super(view);
            textName = view.findViewById(R.id.text_matchday_name);
            recyclerMatches = view.findViewById(R.id.recycler_matches);
        }
    }
}
