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

namespace VOID.UserInterface.Game.Tooltip
{
    public class UsableTooltipUI : ITooltip
    {
        // VisualElement - Container
        private readonly VisualElement _templateElement;
        
        // VisualElements - Sections
        private readonly VisualElement _titleSection;
        private readonly VisualElement _detailsSection;
        private readonly VisualElement _descriptionSection;
        private readonly VisualElement _footSection;
        
        // VisualElements - Data
        private readonly SlotUI _usableIcon = new();
        private readonly List<DetailsElement> _details = new();
        private readonly Label _usableName;
        private readonly Label _usableQuality;
        private readonly Label _usableDescription;
        private readonly Label _usableValue;
        private readonly Label _usableWeight;

        public UsableTooltipUI(VisualElement templateElement)
        {
            // Tooltip for this type
            _templateElement = templateElement.Q(null, "tooltip");
            
            // Sections
            _titleSection = templateElement.Q("title-section");
            _detailsSection = templateElement.Q("details-section");
            _descriptionSection = templateElement.Q("description-section");
            _footSection = templateElement.Q("foot-section");
            
            // Elements
            _usableIcon.Init(templateElement.Q("usable-icon"), SlotType.None, -1);
            _usableName = templateElement.Q<Label>("usable-name");
            _usableQuality = templateElement.Q<Label>("usable-quality");
            _usableDescription = templateElement.Q<Label>("usable-description");
            _usableValue = templateElement.Q<Label>("usable-value");
            _usableWeight = templateElement.Q<Label>("usable-weight");
        }

        public void Show(UsableObject usable)
        {
            _usableIcon.Set(usable);
            _templateElement.style.display = DisplayStyle.Flex;
            
            FillBasicInfo(usable);
            FillDetails(usable);
        }

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

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

        private void FillBasicInfo(UsableObject usable)
        {
            _usableName.text = usable.basicData.finalName;
            _usableQuality.text = usable.itemData.quality.displayName;
            _usableDescription.text = usable.basicData.description;
            _descriptionSection.style.display = _usableDescription.text.IsNullOrWhitespace() ? DisplayStyle.None : DisplayStyle.Flex;
            _usableValue.text = usable.itemData.finalValue.ToString(CultureInfo.InvariantCulture);
            _usableWeight.text = usable.itemData.finalWeight.ToString(CultureInfo.InvariantCulture);
        }

        private void FillDetails(UsableObject usable)
        {
            _detailsSection.Clear();
            
            // All effects provided by usable item
            var effects = usable.usableData.applyOnUse
                .Select(effect => effect.GetSimpleDescription())
                .ToArray();
            if (!effects.IsNullOrEmpty()) _details.Add(new DetailsElement(_detailsSection, null, effects));
        }
    }
}