package com.example.vdcscore.ui.topscorers;

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.bumptech.glide.Glide;
import com.example.vdcscore.R;
import com.example.vdcscore.models.TopScorer;

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

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

    private List<TopScorer> scorers = new ArrayList<>();

    public void setTopScorers(List<TopScorer> scorers) {
        this.scorers = (scorers != null) ? scorers : new ArrayList<>();
        notifyDataSetChanged();
    }

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

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        TopScorer scorer = scorers.get(position);

        // Styling specifically for the Podium (Top 3)
        if (position == 0) {
            holder.textPosition.setBackgroundResource(R.drawable.rank_1_badge);
            holder.textPosition.setTextColor(android.graphics.Color.WHITE);
        } else if (position == 1) {
            holder.textPosition.setBackgroundResource(R.drawable.rank_2_badge);
            holder.textPosition.setTextColor(android.graphics.Color.WHITE);
        } else if (position == 2) {
            holder.textPosition.setBackgroundResource(R.drawable.rank_3_badge);
            holder.textPosition.setTextColor(android.graphics.Color.WHITE);
        } else {
            // Standard fallback
            holder.textPosition.setBackgroundResource(R.drawable.bg_circle_light_gray);
            // Reset back to standard themed color
            holder.textPosition.setTextColor(holder.itemView.getContext().getResources().getColor(R.color.text_1));
        }

        holder.textPosition.setText(String.valueOf(position + 1)); // Display logical incrementing rank

        holder.textPlayerName.setText(isValid(scorer.getPlayerName()) ? scorer.getPlayerName() : "Jogador");
        holder.textClubName.setText(isValid(scorer.getClubName()) ? scorer.getClubName() : "Clube");
        holder.textGoals.setText(String.valueOf(scorer.getGoals()));

        // Carregar Foto do Jogador
        Glide.with(holder.itemView.getContext())
                .load(scorer.getPlayerPhoto())
                .placeholder(R.drawable.ic_menu_camera)
                .error(R.drawable.ic_menu_camera)
                .circleCrop()
                .into(holder.imgPlayerPhoto);

        // Carregar Logótipo do Clube
        Glide.with(holder.itemView.getContext())
                .load(scorer.getClubLogo())
                .placeholder(R.drawable.ic_menu_gallery)
                .error(R.drawable.ic_menu_gallery)
                .circleCrop()
                .into(holder.imgClubLogo);
    }

    private boolean isValid(String text) {
        return text != null && !text.trim().isEmpty() && !text.equalsIgnoreCase("null");
    }

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

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public final TextView textPosition;
        public final TextView textPlayerName;
        public final TextView textClubName;
        public final TextView textGoals;
        public final ImageView imgPlayerPhoto;
        public final ImageView imgClubLogo;

        public ViewHolder(View view) {
            super(view);
            textPosition = view.findViewById(R.id.text_position);
            textPlayerName = view.findViewById(R.id.text_player_name);
            textClubName = view.findViewById(R.id.text_club_name);
            textGoals = view.findViewById(R.id.text_goals);
            imgPlayerPhoto = view.findViewById(R.id.img_player_photo);
            imgClubLogo = view.findViewById(R.id.img_club_logo);
        }
    }
}
