// Copyright (c) 2026 EzAdmin. All rights reserved.
// Licensed under the EzAdmin Client License. See LICENSE-CLIENT.txt.

using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

namespace EzAdmin.Client
{

    [DisallowMultipleComponent]
    public sealed class EzAdminProfileView : MonoBehaviour
    {
        [System.Serializable] public class StringEvent : UnityEvent<string> { }
        [System.Serializable] public class TextureEvent : UnityEvent<Texture> { }

        [Tooltip("Optional: show this player straight away.")]
        [SerializeField] private string _playFabId = "";

        [EzAdminGroup("Where it goes (all optional)")]
        [SerializeField] private RawImage _avatar;
        [SerializeField] private Text _name;
        [SerializeField] private Text _discordId;
        [Tooltip("Shown only when this player's Discord is visible, e.g. a small Discord logo.")]
        [SerializeField] private GameObject _discordBadge;

        [EzAdminGroup("Look")]
        [SerializeField, Range(16, 512)] private int _avatarSize = 64;
        [Tooltip("Shown instead of the Discord id when it is private.")]
        [SerializeField] private string _privateIdText = "";

        [EzAdminGroup("Events (for TextMeshPro or your own UI)")]
        [SerializeField] private StringEvent _onName = new StringEvent();
        [SerializeField] private StringEvent _onDiscordId = new StringEvent();
        [SerializeField] private TextureEvent _onAvatar = new TextureEvent();

        private string _showing = "";

        public EzAdminProfile Profile { get; private set; } = EzAdminProfile.Unavailable;

        private void Start()
        {
            if (!string.IsNullOrEmpty(_playFabId)) Show(_playFabId);
        }

        public void Show(string playFabId, string gameName = "")
        {
            _showing = playFabId ?? "";
            if (!string.IsNullOrEmpty(gameName)) SetName(gameName);
            if (_discordBadge != null) _discordBadge.SetActive(false);
            if (string.IsNullOrEmpty(_showing)) return;

            string asked = _showing;
            EzAdminProfiles.Get(asked, profile =>
            {
                if (this == null || asked != _showing) return;
                Profile = profile;
                if (!profile.Visible)
                {
                    SetId(_privateIdText);
                    return;
                }
                SetName(profile.Name);
                SetId(profile.DiscordIdText);
                if (_discordBadge != null) _discordBadge.SetActive(true);
                EzAdminProfiles.LoadAvatar(profile.AvatarUrl, texture =>
                {
                    if (this == null || asked != _showing || texture == null) return;
                    if (_avatar != null) _avatar.texture = texture;
                    _onAvatar.Invoke(texture);
                }, _avatarSize);
            });
        }

        public void OpenDiscordProfile()
        {
            if (Profile.Visible && !string.IsNullOrEmpty(Profile.ProfileUrl)) EzAdminWeb.Open(Profile.ProfileUrl);
        }

        private void SetName(string value)
        {
            if (_name != null) _name.text = value;
            _onName.Invoke(value);
        }

        private void SetId(string value)
        {
            if (_discordId != null) _discordId.text = value;
            _onDiscordId.Invoke(value);
        }
    }
}
