package com.example.vdcscore.ui.clubs;

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

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

import com.bumptech.glide.Glide;
import com.example.vdcscore.R;
import com.example.vdcscore.models.Club;

import java.util.List;

public class ClubAdapter extends RecyclerView.Adapter<ClubAdapter.ClubViewHolder> {

    private final List<Club> mClubs;
    private final OnItemClickListener mListener;

    public interface OnItemClickListener {
        void onItemClick(Club club);
    }

    public ClubAdapter(List<Club> clubs, OnItemClickListener listener) {
        mClubs = clubs;
        mListener = listener;
    }

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

    @Override
    public void onBindViewHolder(@NonNull ClubViewHolder holder, int position) {
        Club club = mClubs.get(position);
        holder.bind(club, mListener);
    }

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

    static class ClubViewHolder extends RecyclerView.ViewHolder {
        private final TextView name;
        private final TextView stadium;
        private final ImageView image_detail_logo;

        public ClubViewHolder(@NonNull View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.text_club_name);
            stadium = itemView.findViewById(R.id.text_club_stadium);
            image_detail_logo = itemView.findViewById(R.id.image_club_logo);
        }

        public void bind(final Club club, final OnItemClickListener listener) {
            name.setText(club.getName());
            stadium.setText(club.getStadium());

            Glide.with(itemView.getContext())
                    .load(club.getImageUrl())
                    .into(image_detail_logo);

            itemView.setOnClickListener(v -> listener.onItemClick(club));
        }
    }
}
