package com.example.vdcscore.ui.gallery;

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 android.content.Intent;
import android.net.Uri;
import java.util.ArrayList;
import java.util.List;

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

    private List<Match> matches = new ArrayList<>();

    public MatchesAdapter() {
    }

    public MatchesAdapter(List<Match> matches) {
        this.matches = (matches != null) ? matches : new ArrayList<>();
    }

    public void setMatches(List<Match> matches) {
        this.matches = (matches != null) ? matches : new ArrayList<>();
        notifyDataSetChanged();
    }

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

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Match match = matches.get(position);

        // Team Names with fallback
        String homeName = match.getHomeName();
        String awayName = match.getAwayName();
        
        holder.textHomeTeam.setText(isValid(homeName) ? homeName : "Equipa A");
        holder.textAwayTeam.setText(isValid(awayName) ? awayName : "Equipa B");

        // Logos with better loading
        Glide.with(holder.itemView.getContext())
                .load(match.getHomeLogo())
                .placeholder(R.drawable.ic_club_placeholder)
                .error(R.drawable.ic_club_placeholder)
                .circleCrop()
                .into(holder.imgHomeLogo);

        Glide.with(holder.itemView.getContext())
                .load(match.getAwayLogo())
                .placeholder(R.drawable.ic_club_placeholder)
                .error(R.drawable.ic_club_placeholder)
                .circleCrop()
                .into(holder.imgAwayLogo);

        // Scores
        if (match.getHomeScore() != null) {
            holder.textHomeScore.setText(String.valueOf(match.getHomeScore()));
        } else {
            holder.textHomeScore.setText("-");
        }

        if (match.getAwayScore() != null) {
            holder.textAwayScore.setText(String.valueOf(match.getAwayScore()));
        } else {
            holder.textAwayScore.setText("-");
        }

        // Match Info (Date/Time)
        String dateStr = match.getDate() != null ? match.getDate() : "";
        String timeStr = match.getTime() != null ? match.getTime() : "";
        String fullInfo = (dateStr + " " + timeStr).trim();
        holder.textMatchInfo.setText(fullInfo.isEmpty() ? "Data a definir" : fullInfo);

        // Stadium
        String stadium = match.getStadium();
        holder.textStadium.setText(isValid(stadium) ? "Campo: " + stadium : "Campo a definir");

        // Match Report Button
        if (isValid(match.getMatchReportUrl())) {
            holder.btnMatchReport.setVisibility(View.VISIBLE);
            holder.btnMatchReport.setOnClickListener(v -> {
                Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(match.getMatchReportUrl()));
                holder.itemView.getContext().startActivity(browserIntent);
            });
        } else {
            holder.btnMatchReport.setVisibility(View.GONE);
            holder.btnMatchReport.setOnClickListener(null);
        }
    }

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

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

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public final TextView textHomeTeam;
        public final TextView textAwayTeam;
        public final TextView textHomeScore;
        public final TextView textAwayScore;
        public final TextView textMatchInfo;
        public final TextView textStadium;
        public final ImageView imgHomeLogo;
        public final ImageView imgAwayLogo;
        public final com.google.android.material.button.MaterialButton btnMatchReport;

        public ViewHolder(View view) {
            super(view);
            textHomeTeam = view.findViewById(R.id.text_home_team);
            textAwayTeam = view.findViewById(R.id.text_away_team);
            textHomeScore = view.findViewById(R.id.text_home_score);
            textAwayScore = view.findViewById(R.id.text_away_score);
            textMatchInfo = view.findViewById(R.id.text_match_info);
            textStadium = view.findViewById(R.id.text_match_stadium);
            imgHomeLogo = view.findViewById(R.id.img_home_logo);
            imgAwayLogo = view.findViewById(R.id.img_away_logo);
            btnMatchReport = view.findViewById(R.id.btn_match_report);
        }
    }
}
