
using JetBrains.Annotations;
using Sirenix.OdinInspector;
using TMPro;
using UnityEngine;

namespace JohnsonUtils.Canvases
{
    public class TextUIComponent : UIComponentBase
    {
#if ODIN_INSPECTOR
        [Required]
#endif
        [Header("Association"), SerializeField]
        private TMP_Text text;

        private string _initialText;

        public string Text
        {
            get => text.text;
            set => text.text = string.IsNullOrEmpty(value) ? string.Empty : value;
        }

        [UsedImplicitly]
        public Color Color
        {
            get => text.color;
            set => text.color = value;
        }

        private void Awake() => _initialText = text.text;

        private void Start() => Debug.Assert(text, $"A {nameof(text)} must be assigned to a {nameof(TMP_Text)}");

        public void ResetText() => text.text = _initialText;

        [PublicAPI]
        public void AddText(string textToAdd) => text.text += textToAdd;

        [PublicAPI]
        public void EraseText() => text.text = string.Empty;

        private void OnValidate()
        {
            if (!text)
                text = GetComponent<TMP_Text>();
        }
    }
}