package com.example.boidelov3.hub.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.boidelov3.R;
import com.example.boidelov3.hub.model.GameInfo;
import java.util.List;

/**
 * GameAdapter - Adaptateur pour afficher la liste des jeux dans le RecyclerView
 */
public class GameAdapter extends RecyclerView.Adapter<GameAdapter.GameViewHolder> {

    private List<GameInfo> gamesList;
    private OnItemClickListener listener;

    public interface OnItemClickListener {
        void onItemClick(GameInfo gameInfo);
    }

    public GameAdapter(List<GameInfo> gamesList, OnItemClickListener listener) {
        this.gamesList = gamesList;
        this.listener = listener;
    }

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

    @Override
    public void onBindViewHolder(@NonNull GameViewHolder holder, int position) {
        GameInfo gameInfo = gamesList.get(position);
        holder.bind(gameInfo, listener);
    }

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

    static class GameViewHolder extends RecyclerView.ViewHolder {
        private ImageView gameIcon;
        private TextView gameName;
        private TextView gameDescription;
        private View statusText;

        public GameViewHolder(@NonNull View itemView) {
            super(itemView);
            gameIcon = itemView.findViewById(R.id.gameIcon);
            gameName = itemView.findViewById(R.id.gameName);
            gameDescription = itemView.findViewById(R.id.gameDescription);
            statusText = itemView.findViewById(R.id.statusText);
        }

        public void bind(GameInfo gameInfo, OnItemClickListener listener) {
            gameName.setText(gameInfo.getName());
            gameDescription.setText(gameInfo.getDescription());
            gameIcon.setImageResource(gameInfo.getIconResId());

            if (gameInfo.isAvailable()) {
                statusText.setVisibility(View.GONE);
                itemView.setEnabled(true);
                itemView.setAlpha(1.0f);
                itemView.setOnClickListener(v -> listener.onItemClick(gameInfo));
            } else {
                statusText.setVisibility(View.VISIBLE);
                itemView.setEnabled(false);
                itemView.setAlpha(0.6f);
            }
        }
    }
}
