package com.example.vdcscore.ui.livegames;

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.example.vdcscore.R;

import java.util.List;

public class GameAdapter extends RecyclerView.Adapter<GameAdapter.GameViewHolder> {

    private final List<Game> mGames;
    private final OnGameClickListener mListener;

    public interface OnGameClickListener {
        void onGameClick(Game game);
    }

    public GameAdapter(List<Game> games, OnGameClickListener listener) {
        mGames = games;
        mListener = listener;
    }

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

    @Override
    public void onBindViewHolder(@NonNull GameViewHolder holder, int position) {
        Game game = mGames.get(position);
        holder.bind(game, mListener);
    }

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

    static class GameViewHolder extends RecyclerView.ViewHolder {
        private final TextView time;
        private final TextView homeTeam;
        private final TextView awayTeam;
        private final TextView score;

        public GameViewHolder(@NonNull View itemView) {
            super(itemView);
            time = itemView.findViewById(R.id.text_game_time);
            homeTeam = itemView.findViewById(R.id.text_home_team);
            awayTeam = itemView.findViewById(R.id.text_away_team);
            score = itemView.findViewById(R.id.text_score);
        }

        public void bind(final Game game, final OnGameClickListener listener) {
            time.setText(game.getTime());
            homeTeam.setText(game.getHomeTeam());
            awayTeam.setText(game.getAwayTeam());
            score.setText(game.getHomeScore() + " - " + game.getAwayScore());

            itemView.setOnClickListener(v -> listener.onGameClick(game));
        }
    }
}
