package com.example.vdcscore.ui.home;

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.ArrayList;
import java.util.List;

import com.bumptech.glide.Glide;

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

    private List<Team> mTeams;
    private OnTeamClickListener mListener;

    public interface OnTeamClickListener {
        void onTeamClick(Team team);
    }

    public StandingsAdapter() {
        this.mTeams = new ArrayList<>();
    }

    public void setOnTeamClickListener(OnTeamClickListener listener) {
        this.mListener = listener;
    }

    public void setTeams(List<Team> teams) {
        this.mTeams = teams;
        notifyDataSetChanged();
    }

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

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Team team = mTeams.get(position);
        if (team == null) return;

        int rank = position + 1;
        holder.textPosition.setText(String.valueOf(rank));
        
        if (rank >= 1 && rank <= 3) {
            holder.textPosition.setBackgroundResource(R.drawable.bg_circle_green);
            holder.textPosition.setTextColor(android.graphics.Color.WHITE);
        } else if (rank == 4) {
            holder.textPosition.setBackgroundResource(R.drawable.bg_circle_yellow);
            holder.textPosition.setTextColor(android.graphics.Color.WHITE);
        } else if (rank >= 5 && rank <= 6) {
            holder.textPosition.setBackgroundResource(R.drawable.bg_circle_orange);
            holder.textPosition.setTextColor(android.graphics.Color.WHITE);
        } else {
            holder.textPosition.setBackgroundResource(R.drawable.bg_circle_transparent);
            holder.textPosition.setTextColor(android.graphics.Color.BLACK);
        }

        String imageUrl = team.getImagem();
        Glide.with(holder.itemView.getContext())
             .load(imageUrl != null ? imageUrl : "")
             .placeholder(R.mipmap.ic_launcher)
             .error(R.mipmap.ic_launcher)
             .into(holder.imageLogo);

        holder.textTeamName.setText(team.getNome() != null ? team.getNome() : "---");
        holder.textPlayed.setText(String.valueOf(team.getJogos()));
        holder.textWon.setText(String.valueOf(team.getVitorias()));
        holder.textDrawn.setText(String.valueOf(team.getEmpates()));
        holder.textLost.setText(String.valueOf(team.getDerrotas()));
        holder.textGoalDiff.setText(String.valueOf(team.getDiferenca_golos()));
        holder.textPoints.setText(String.valueOf(team.getPontos()));

        holder.itemView.setOnClickListener(v -> {
            if (mListener != null) {
                mListener.onTeamClick(team);
            }
        });
    }

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

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public final TextView textPosition;
        public final android.widget.ImageView imageLogo;
        public final TextView textTeamName;
        public final TextView textPlayed;
        public final TextView textWon;
        public final TextView textDrawn;
        public final TextView textLost;
        public final TextView textGoalDiff;
        public final TextView textPoints;

        public ViewHolder(View view) {
            super(view);
            textPosition = view.findViewById(R.id.text_position);
            imageLogo = view.findViewById(R.id.image_logo);
            textTeamName = view.findViewById(R.id.text_team_name);
            textPlayed = view.findViewById(R.id.text_played);
            textWon = view.findViewById(R.id.text_won);
            textDrawn = view.findViewById(R.id.text_drawn);
            textLost = view.findViewById(R.id.text_lost);
            textGoalDiff = view.findViewById(R.id.text_goal_diff);
            textPoints = view.findViewById(R.id.text_points);
        }
    }
}
