using HSUI.Config;
using HSUI.Enums;
using HSUI.Interface.GeneralElements;
using Dalamud.Bindings.ImGui;
using System;
using System.Numerics;

namespace HSUI.Helpers
{
    /// <summary>
    /// Hit test for HSUI hotbars. Used to determine if the cursor is over an HSUI hotbar
    /// so we only intercept drag-drop releases when the user is actually dropping on our bars.
    /// </summary>
    public static class ActionBarsHitTestHelper
    {
        /// <summary>Returns true if the mouse cursor is over any visible HSUI hotbar.</summary>
        public static bool IsMouseOverAnyHSUIHotbar()
        {
            try
            {
                var hotbarsConfig = ConfigurationManager.Instance?.GetConfigObject<HotbarsConfig>();
                if (hotbarsConfig == null || !hotbarsConfig.Enabled)
                    return false;

                var hudOptions = ConfigurationManager.Instance?.GetConfigObject<HUDOptionsConfig>();
                Vector2 origin = ImGui.GetMainViewport().Size / 2f;
                if (hudOptions != null && hudOptions.UseGlobalHudShift)
                    origin += hudOptions.HudOffset;

                Vector2 mousePos = ImGui.GetMousePos();

                HotbarBarConfig?[] barConfigs = new HotbarBarConfig?[]
                {
                    ConfigurationManager.Instance?.GetConfigObject<Hotbar1BarConfig>(),
                    ConfigurationManager.Instance?.GetConfigObject<Hotbar2BarConfig>(),
                    ConfigurationManager.Instance?.GetConfigObject<Hotbar3BarConfig>(),
                    ConfigurationManager.Instance?.GetConfigObject<Hotbar4BarConfig>(),
                    ConfigurationManager.Instance?.GetConfigObject<Hotbar5BarConfig>(),
                    ConfigurationManager.Instance?.GetConfigObject<Hotbar6BarConfig>(),
                    ConfigurationManager.Instance?.GetConfigObject<Hotbar7BarConfig>(),
                    ConfigurationManager.Instance?.GetConfigObject<Hotbar8BarConfig>(),
                    ConfigurationManager.Instance?.GetConfigObject<Hotbar9BarConfig>(),
                    ConfigurationManager.Instance?.GetConfigObject<Hotbar10BarConfig>()
                };

                foreach (var barConfig in barConfigs)
                {
                    if (barConfig == null) continue;

                    Vector2 barSize = ComputeBarSize(barConfig);
                    Vector2 topLeft = Utils.GetAnchoredPosition(origin + barConfig.Position, barSize, barConfig.Anchor);

                    if (mousePos.X >= topLeft.X && mousePos.X < topLeft.X + barSize.X &&
                        mousePos.Y >= topLeft.Y && mousePos.Y < topLeft.Y + barSize.Y)
                    {
                        return true;
                    }
                }

                return false;
            }
            catch
            {
                return false;
            }
        }

        private static Vector2 ComputeBarSize(HotbarBarConfig config)
        {
            var (cols, _) = config.GetLayoutGrid();
            int effectiveCols = Math.Min(cols, config.SlotCount);
            int effectiveRows = (config.SlotCount + effectiveCols - 1) / effectiveCols;
            float w = effectiveCols * config.SlotSize.X + (effectiveCols - 1) * config.SlotPadding;
            float h = effectiveRows * config.SlotSize.Y + (effectiveRows - 1) * config.SlotPadding;
            return new Vector2(w, h);
        }
    }
}
