using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.UIElements;
using VOID.Generic.Dict;
using VOID.Generic.Objects.Item;
using VOID.UserInterface.Game.Util;
using VOID.ScriptableObjects.Abilities;

namespace VOID.UserInterface.Game
{
    public class DraggingUI : MonoBehaviour
    {
        public static DraggingUI current {get; private set;}

        // Private properties
        private VisualElement _slotElement;
        private SlotUI _slot;

        private void Awake()
        {
            current = this;
            
            var uiDocument = gameObject.GetComponent<UIDocument>();
            var templateElement = uiDocument.rootVisualElement;

            _slotElement = templateElement.Q("slot");
            _slot = new SlotUI();
            _slot.Init(_slotElement, SlotType.None, -1);
            Hide();
            
            // SAFEGUARD - dragging should never be clickable, if somehow UI was built weird then this should fix it
            templateElement.Query<VisualElement>().ForEach(element => element.pickingMode = PickingMode.Ignore);
        }

        private void Update()
        {
            var pos = RuntimePanelUtils.ScreenToPanel(_slotElement.panel, Mouse.current.position.ReadValue());
            _slotElement.style.bottom = new StyleLength(pos.y);
            _slotElement.style.left = new StyleLength(pos.x);
        }

        public void Set(ItemObject item) => _slot.Set(item);

        public void Set(BasicAbility ability) => _slot.Set(ability);

        public void Show()
        {
            _slotElement.style.display = DisplayStyle.Flex;
            enabled = true;
        }

        public void Hide()
        {
            _slotElement.style.display = DisplayStyle.None;
            enabled = false;
        }
    }
}
