using System.Globalization;
using Sirenix.Utilities;
using UnityEngine;
using UnityEngine.UIElements;
using VOID.Generic.Dict;
using VOID.Generic.Objects.Item;
using VOID.UserInterface.Game.Util;

namespace VOID.UserInterface.Game.Tooltip
{
    public class ItemTooltipUI : 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 _itemIcon = new();
        private readonly Label _itemName;
        private readonly Label _itemQuality;
        private readonly Label _itemDescription;
        private readonly Label _itemValue;
        private readonly Label _itemWeight;

        public ItemTooltipUI(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
            _itemIcon.Init(templateElement.Q("item-icon"), SlotType.None, -1);
            _itemName = templateElement.Q<Label>("item-name");
            _itemQuality = templateElement.Q<Label>("item-quality");
            _itemDescription = templateElement.Q<Label>("item-description");
            _itemValue = templateElement.Q<Label>("item-value");
            _itemWeight = templateElement.Q<Label>("item-weight");
        }

        public void Show(ItemObject item)
        {
            _itemIcon.Set(item);
            _templateElement.style.display = DisplayStyle.Flex;
            
            FillBasicInfo(item);
        }

        public void Hide()
        {
            _templateElement.style.display = DisplayStyle.None;
        }
        
        public Vector2 GetSize()
        {
            return new Vector2(_templateElement.worldBound.width, _templateElement.worldBound.height);
        }

        private void FillBasicInfo(ItemObject item)
        {
            _itemName.text = item.basicData.finalName;
            _itemQuality.text = item.itemData.quality.displayName;
            _itemDescription.text = item.basicData.description;
            _descriptionSection.style.display = _itemDescription.text.IsNullOrWhitespace() ? DisplayStyle.None : DisplayStyle.Flex;
            _itemValue.text = item.itemData.finalValue.ToString(CultureInfo.InvariantCulture);
            _itemWeight.text = item.itemData.finalWeight.ToString(CultureInfo.InvariantCulture);
        }
    }
}