using System.Globalization;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

namespace MajacloudSdk.Samples.Starter
{
    public sealed class LeaderboardRowView : MonoBehaviour
    {
        [SerializeField] private TMP_Text rankLabel;
        [SerializeField] private TMP_Text nameLabel;
        [SerializeField] private TMP_Text scoreLabel;
        [SerializeField] private Image background;
        private static readonly Color Normal = new Color32(247, 249, 252, 255);
        private static readonly Color Highlight = new Color32(234, 241, 255, 255);
        private static readonly Color HighlightText = new Color32(26, 86, 196, 255);
        private static readonly Color Text = new Color32(19, 33, 60, 255);

        public void Bind(MajacloudLeaderboardEntry entry)
        {
            if (rankLabel != null) rankLabel.text = entry.Rank.HasValue ? entry.Rank.Value.ToString(CultureInfo.InvariantCulture) : "–";
            if (nameLabel != null)
            {
                nameLabel.text = entry.IsMe ? entry.PlayerName + " · you" : entry.PlayerName;
                nameLabel.color = entry.IsMe ? HighlightText : Text;
            }
            if (scoreLabel != null) scoreLabel.text = entry.Score.ToString("#,0.##", CultureInfo.InvariantCulture);
            if (background != null) background.color = entry.IsMe ? Highlight : Normal;
        }
    }
}
