using Sirenix.Utilities;
using UnityEngine;
using UnityEngine.UIElements;
using VOID.Generic.Dict;
using VOID.UserInterface.Game.Util;
using VOID.ScriptableObjects.Abilities;

namespace VOID.UserInterface.Game.Tooltip
{
    public class AbilityTooltipUI : ITooltip
    {

        // VisualElement - Container
        private readonly VisualElement _templateElement;
        
        // VisualElements - Sections
        private readonly VisualElement _titleSection;
        private readonly VisualElement _descriptionSection;
        private readonly VisualElement _footSection;
        
        // VisualElements - Data
        private readonly SlotUI _abilityIcon = new();
        private readonly Label _abilityName;
        private readonly Label _abilityDescription;

        public AbilityTooltipUI(VisualElement templateElement)
        {
            // Tooltip for this type
            _templateElement = templateElement.Q(null, "tooltip");
            
            // Sections
            _titleSection = templateElement.Q("title-section");
            _descriptionSection = templateElement.Q("description-section");
            _footSection = templateElement.Q("foot-section");
            
            // Elements
            _abilityIcon.Init(templateElement.Q("ability-icon"), SlotType.None, -1);
            _abilityName = templateElement.Q<Label>("ability-name");
            _abilityDescription = templateElement.Q<Label>("ability-description");
        }

        public void Show(BasicAbility ability)
        {
            _abilityIcon.Set(ability);
            _templateElement.style.display = DisplayStyle.Flex;
            
            FillBasicInfo(ability);
        }

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

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

        private void FillBasicInfo(BasicAbility ability)
        {
            _abilityName.text = ability.displayName;
            _abilityDescription.text = ability.displayDescription;
            _descriptionSection.style.display = _abilityDescription.text.IsNullOrWhitespace() ? DisplayStyle.None : DisplayStyle.Flex;

            // TODO: temporary ability description for tooltip
            _abilityDescription.text += "<br><br>TODO: TEMPORARY:";
            _abilityDescription.text += "<br>isMelee: " + ability.isRanged;
            _abilityDescription.text += "<br>range: " + ability.range;
            _abilityDescription.text += "<br>castingType: " + ability.castingType;
            _abilityDescription.text += "<br>aimingType: " + ability.aimingType;
            _abilityDescription.text += "<br>castUsages: " + ability.castUsages;
            _abilityDescription.text += "<br>applyOnCaster: ";
            ability.applyOnCaster.ForEach(effect => _abilityDescription.text += "<br>    " + effect.GetSimpleDescription());
            _abilityDescription.text += "<br>applyOnTarget: ";
            ability.applyOnTarget.ForEach(effect => _abilityDescription.text += "<br>    " + effect.GetSimpleDescription());
        }
    }
}