using Sirenix.Utilities;
using UnityEngine;
using UnityEngine.UIElements;
using VOID.ScriptableObjects;

namespace VOID.UserInterface.Game.Tooltip
{
    public class StatusTooltipUI : ITooltip
    {
        // VisualElement - Container
        private readonly VisualElement _templateElement;
        
        // VisualElements - Sections
        private readonly VisualElement _titleSection;
        private readonly VisualElement _descriptionSection;
        
        // VisualElements - Data
        private readonly VisualElement _statusIcon;
        private readonly Label _statusName;
        private readonly Label _statusDescription;
        private readonly Label _statusTimeRemaining;

        public StatusTooltipUI(VisualElement templateElement)
        {
            // Tooltip for this type
            _templateElement = templateElement.Q(null, "tooltip");
            
            // Sections
            _titleSection = templateElement.Q("title-section");
            _descriptionSection = templateElement.Q("description-section");
            
            // Elements
            _statusIcon = templateElement.Q("status-icon");
            _statusName = templateElement.Q<Label>("status-name");
            _statusDescription = templateElement.Q<Label>("status-description");
            _statusTimeRemaining = templateElement.Q<Label>("status-time-remaining");
        }

        public void Show(BasicStatus status)
        {
            _statusIcon.style.backgroundImage = status.displayIcon;
            _templateElement.style.display = DisplayStyle.Flex;
            
            FillBasicInfo(status);
        }

        public void Hide()
        {
            _templateElement.style.display = DisplayStyle.None;
        }

        public Vector2 GetSize()
        {
            return new Vector2(_templateElement.worldBound.width, _templateElement.worldBound.height);
        }

        private void FillBasicInfo(BasicStatus status)
        {
            _statusName.text = status.displayName;
            _statusDescription.text = status.description;
            _descriptionSection.style.display = _statusDescription.text.IsNullOrWhitespace() ? DisplayStyle.None : DisplayStyle.Flex;

            // TODO: temporary status description for tooltip
            _statusDescription.text += "<br><br>TODO: TEMPORARY:";
            _statusDescription.text += "<br>effects: ";
            status.effects.ForEach(effect => _statusDescription.text += "<br>    " + effect.GetSimpleDescription());

            _statusTimeRemaining.text = $"Remaining {status.durationLeftInTurns} turns (~{status.durationLeft:0} seconds)";
        }
    }
}