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

namespace HSUI.Interface.GeneralElements
{
    /// <summary>
    /// Config for a single hotbar (1-10). HotbarIndex is set by the containing config.
    /// </summary>
    public class HotbarBarConfig : AnchorablePluginConfigObject
    {
        internal int HotbarIndex { get; set; } = 1;

        [RadioSelector("12×1", "6×2", "4×3", "3×4", "2×6", "1×12", Label = "Bar Layout")]
        [Order(20)]
        public int BarLayout = 0; // 0=12x1, 1=6x2, 2=4x3, 3=3x4, 4=2x6, 5=1x12

        /// <summary>Get (columns, rows) for the current BarLayout.</summary>
        public (int Cols, int Rows) GetLayoutGrid() => BarLayout switch
        {
            0 => (12, 1),
            1 => (6, 2),
            2 => (4, 3),
            3 => (3, 4),
            4 => (2, 6),
            5 => (1, 12),
            _ => (12, 1)
        };

        [DragInt("Slot Count", min = 1, max = 12)]
        [Order(21)]
        public int SlotCount = 12;

        [DragInt2("Slot Size", min = 24, max = 96)]
        [Order(22)]
        public Vector2 SlotSize = new Vector2(40, 40);

        [DragInt("Slot Padding", min = 0, max = 16)]
        [Order(23)]
        public int SlotPadding = 2;

        [Checkbox("Show Cooldown Overlay")]
        [Order(24)]
        public bool ShowCooldownOverlay = true;

        [Checkbox("Show Cooldown Numbers")]
        [Order(25)]
        public bool ShowCooldownNumbers = true;

        [Checkbox("Show Border")]
        [Order(26)]
        public bool ShowBorder = true;

        [Checkbox("Show Tooltips")]
        [Order(27)]
        public bool ShowTooltips = true;

        [Checkbox("Show Keybinds")]
        [Order(28)]
        public bool ShowSlotNumbers = true;

        [Checkbox("Show Charge Count")]
        [Order(29)]
        public bool ShowChargeCount = true;

        [Checkbox("Show Combo Highlight")]
        [Order(30)]
        public bool ShowComboHighlight = true;

        [Checkbox("Show Keypress Flash")]
        [Order(31)]
        public bool ShowKeypressFlash = true;

        [NestedConfig("Combo Highlight", 32)]
        public ComboHighlightConfig ComboHighlightConfig = new();

        [Checkbox("Debug Drag & Drop")]
        [Order(32)]
        public bool DebugDragDrop = false;

        [NestedConfig("Visibility", 70)]
        public VisibilityConfig VisibilityConfig = new VisibilityConfig();

        public static HotbarBarConfig DefaultConfig(int hotbarIndex)
        {
            var config = new HotbarBarConfig { HotbarIndex = hotbarIndex };
            ApplyDefaults(config, hotbarIndex);
            return config;
        }

        public static void ApplyDefaults(HotbarBarConfig config, int hotbarIndex)
        {
            var viewport = ImGui.GetMainViewport().Size;
            float yOffset = 60 + (hotbarIndex - 1) * 50;
            config.Position = new Vector2(0, -viewport.Y * 0.5f + yOffset);
            config.Anchor = DrawAnchor.Top;
            config.Size = new Vector2(12 * 40 + 11 * 2, 40);
            config.HotbarIndex = hotbarIndex;
        }
    }

    [Exportable(false)]
    [Section("Hotbars", true)]
    [SubSection("General", 0)]
    public class HotbarsConfig : PluginConfigObject
    {
        [Checkbox("Use HSUI Hotbars", help = "When enabled, HSUI hotbars replace the default game hotbars.")]
        [Order(1, collapseWith = null)]
        public new bool Enabled = true;

        [NestedConfig("Drag & Drop Options", 5, separator = true, collapseWith = null)]
        public HotbarsGeneralOptionsConfig GeneralOptions = new();

        [ManualDraw]
        public bool DrawRestoreHotbarsButton(ref bool changed)
        {
            ImGuiHelper.DrawSeparator(2, 1);
            if (ImGui.Button("Restore Hotbar Layout", new Vector2(200, 24)))
            {
                RestoreAllHotbarsToDefaults();
                changed = true;
                return true;
            }
            if (ImGui.IsItemHovered())
                ImGui.SetTooltip("Reset positions and re-enable all hotbars. Use if your hotbars disappeared after an update.");
            return false;
        }

        public new static HotbarsConfig DefaultConfig() => new HotbarsConfig();

        private static void RestoreAllHotbarsToDefaults()
        {
            var cfg = ConfigurationManager.Instance;
            if (cfg == null) return;
            var bars = new (HotbarBarConfig config, int index)[]
            {
                (cfg.GetConfigObject<Hotbar1BarConfig>(), 1),
                (cfg.GetConfigObject<Hotbar2BarConfig>(), 2),
                (cfg.GetConfigObject<Hotbar3BarConfig>(), 3),
                (cfg.GetConfigObject<Hotbar4BarConfig>(), 4),
                (cfg.GetConfigObject<Hotbar5BarConfig>(), 5),
                (cfg.GetConfigObject<Hotbar6BarConfig>(), 6),
                (cfg.GetConfigObject<Hotbar7BarConfig>(), 7),
                (cfg.GetConfigObject<Hotbar8BarConfig>(), 8),
                (cfg.GetConfigObject<Hotbar9BarConfig>(), 9),
                (cfg.GetConfigObject<Hotbar10BarConfig>(), 10)
            };
            foreach (var (bar, idx) in bars)
            {
                if (bar == null) continue;
                bar.Enabled = true;
                bar.VisibilityConfig.CopyFrom(new VisibilityConfig());
                HotbarBarConfig.ApplyDefaults(bar, idx);
            }
        }
    }

    public enum ComboHighlightLineStyle
    {
        Solid = 0,
        Dashed = 1,
        Dotted = 2
    }

    [Exportable(false)]
    public class ComboHighlightConfig : PluginConfigObject
    {
        [ColorEdit4("Color")]
        [Order(1)]
        public PluginConfigColor Color = PluginConfigColor.FromHex(0xFFFFD700);

        [Checkbox("Show Glow")]
        [Order(2)]
        public bool ShowGlow = false;

        [Combo("Line Style", new string[] { "Solid", "Dashed", "Dotted" })]
        [Order(3)]
        public int LineStyle = (int)ComboHighlightLineStyle.Solid;

        [DragInt("Border Thickness", min = 1, max = 8)]
        [Order(4)]
        public int Thickness = 3;

        public ComboHighlightConfig() { }
    }

    public class HotbarsGeneralOptionsConfig : PluginConfigObject
    {
        [Checkbox("Enable drag and drop from game UI", help = "When enabled, you can drag actions, macros, and items from the Actions menu, Macro menu, and Inventory onto HSUI hotbars.")]
        [Order(1)]
        public bool EnableDragDropFromGame = true;

        [Checkbox("Enable Shift+drag to rearrange", help = "When enabled, holding Shift and dragging a hotbar slot lets you swap it with another slot or rearrange your hotbar.")]
        [Order(2)]
        public bool EnableShiftDragToRearrange = true;

        [Checkbox("Enable release outside to clear slot", help = "When enabled, releasing a picked-up icon outside of HSUI hotbars clears that slot.")]
        [Order(3)]
        public bool EnableReleaseOutsideToClear = true;

        public new static HotbarsGeneralOptionsConfig DefaultConfig() => new();
    }

    [Exportable(false)]
    [Section("Hotbars", true)]
    [SubSection("Hotbar 1", 0)]
    public class Hotbar1BarConfig : HotbarBarConfig
    {
        public Hotbar1BarConfig() => HotbarIndex = 1;
        public new static Hotbar1BarConfig DefaultConfig() { var c = new Hotbar1BarConfig(); ApplyDefaults(c, 1); return c; }
    }

    [Exportable(false)]
    [Section("Hotbars", true)]
    [SubSection("Hotbar 2", 0)]
    public class Hotbar2BarConfig : HotbarBarConfig
    {
        public Hotbar2BarConfig() => HotbarIndex = 2;
        public new static Hotbar2BarConfig DefaultConfig() { var c = new Hotbar2BarConfig(); ApplyDefaults(c, 2); return c; }
    }

    [Exportable(false)]
    [Section("Hotbars", true)]
    [SubSection("Hotbar 3", 0)]
    public class Hotbar3BarConfig : HotbarBarConfig
    {
        public Hotbar3BarConfig() => HotbarIndex = 3;
        public new static Hotbar3BarConfig DefaultConfig() { var c = new Hotbar3BarConfig(); ApplyDefaults(c, 3); return c; }
    }

    [Exportable(false)]
    [Section("Hotbars", true)]
    [SubSection("Hotbar 4", 0)]
    public class Hotbar4BarConfig : HotbarBarConfig
    {
        public Hotbar4BarConfig() => HotbarIndex = 4;
        public new static Hotbar4BarConfig DefaultConfig() { var c = new Hotbar4BarConfig(); ApplyDefaults(c, 4); return c; }
    }

    [Exportable(false)]
    [Section("Hotbars", true)]
    [SubSection("Hotbar 5", 0)]
    public class Hotbar5BarConfig : HotbarBarConfig
    {
        public Hotbar5BarConfig() => HotbarIndex = 5;
        public new static Hotbar5BarConfig DefaultConfig() { var c = new Hotbar5BarConfig(); ApplyDefaults(c, 5); return c; }
    }

    [Exportable(false)]
    [Section("Hotbars", true)]
    [SubSection("Hotbar 6", 0)]
    public class Hotbar6BarConfig : HotbarBarConfig
    {
        public Hotbar6BarConfig() => HotbarIndex = 6;
        public new static Hotbar6BarConfig DefaultConfig() { var c = new Hotbar6BarConfig(); ApplyDefaults(c, 6); return c; }
    }

    [Exportable(false)]
    [Section("Hotbars", true)]
    [SubSection("Hotbar 7", 0)]
    public class Hotbar7BarConfig : HotbarBarConfig
    {
        public Hotbar7BarConfig() => HotbarIndex = 7;
        public new static Hotbar7BarConfig DefaultConfig() { var c = new Hotbar7BarConfig(); ApplyDefaults(c, 7); return c; }
    }

    [Exportable(false)]
    [Section("Hotbars", true)]
    [SubSection("Hotbar 8", 0)]
    public class Hotbar8BarConfig : HotbarBarConfig
    {
        public Hotbar8BarConfig() => HotbarIndex = 8;
        public new static Hotbar8BarConfig DefaultConfig() { var c = new Hotbar8BarConfig(); ApplyDefaults(c, 8); return c; }
    }

    [Exportable(false)]
    [Section("Hotbars", true)]
    [SubSection("Hotbar 9", 0)]
    public class Hotbar9BarConfig : HotbarBarConfig
    {
        public Hotbar9BarConfig() => HotbarIndex = 9;
        public new static Hotbar9BarConfig DefaultConfig() { var c = new Hotbar9BarConfig(); ApplyDefaults(c, 9); return c; }
    }

    [Exportable(false)]
    [Section("Hotbars", true)]
    [SubSection("Hotbar 10", 0)]
    public class Hotbar10BarConfig : HotbarBarConfig
    {
        public Hotbar10BarConfig() => HotbarIndex = 10;
        public new static Hotbar10BarConfig DefaultConfig() { var c = new Hotbar10BarConfig(); ApplyDefaults(c, 10); return c; }
    }
}
