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

namespace HSUI.Interface.Party
{
    [Exportable(false)]
    [Disableable(false)]
    [DisableParentSettings("Position", "Anchor", "BackgroundColor", "FillColor", "HideWhenInactive", "DrawBorder", "BorderColor", "BorderThickness")]
    [Section("Alliance Frames", true)]
    [SubSection("Health Bar", 0)]
    public class AllianceFramesHealthBarsConfig : PartyFramesHealthBarsConfig
    {
        public new static AllianceFramesHealthBarsConfig DefaultConfig()
        {
            var config = new AllianceFramesHealthBarsConfig(Vector2.Zero, new(100, 22), PluginConfigColor.Empty);
            config.MouseoverAreaConfig.Enabled = false;
            config.Padding = new Vector2(2, 1);
            config.ShowLabels = false;
            config.NameLabelConfig.Enabled = false;
            config.HealthLabelConfig.Enabled = false;
            config.OrderNumberConfig.Enabled = false;
            return config;
        }

        public AllianceFramesHealthBarsConfig(Vector2 position, Vector2 size, PluginConfigColor fillColor, BarDirection fillDirection = BarDirection.Right)
            : base(position, size, fillColor, fillDirection)
        {
        }
    }

    [Exportable(false)]
    [Section("Alliance Frames", true)]
    [SubSection("Mana Bar", 0)]
    public class AllianceFramesManaBarConfig : PartyFramesManaBarConfig
    {
        public new static AllianceFramesManaBarConfig DefaultConfig()
        {
            var config = new AllianceFramesManaBarConfig(new Vector2(0, 0), new Vector2(100, 6));
            config.Enabled = false;
            return config;
        }
        public AllianceFramesManaBarConfig(Vector2 position, Vector2 size) : base(position, size) { }
    }

    [Exportable(false)]
    [Section("Alliance Frames", true)]
    [SubSection("Castbar", 0)]
    public class AllianceFramesCastbarConfig : PartyFramesCastbarConfig
    {
        public new static AllianceFramesCastbarConfig DefaultConfig()
        {
            var size = new Vector2(102, 10);
            var pos = new Vector2(-1, 0);
            var castNameConfig = new LabelConfig(new Vector2(5, 0), "", DrawAnchor.Left, DrawAnchor.Left);
            var castTimeConfig = new NumericLabelConfig(new Vector2(-5, 0), "", DrawAnchor.Right, DrawAnchor.Right);
            castTimeConfig.Enabled = false;
            castTimeConfig.NumberFormat = 1;
            var config = new AllianceFramesCastbarConfig(pos, size, castNameConfig, castTimeConfig);
            config.Enabled = false;
            return config;
        }
        public AllianceFramesCastbarConfig(Vector2 position, Vector2 size, LabelConfig castNameConfig, NumericLabelConfig castTimeConfig)
            : base(position, size, castNameConfig, castTimeConfig) { }
    }

    [Disableable(false)]
    [Exportable(false)]
    [Section("Alliance Frames", true)]
    [SubSection("Icons", 0)]
    public class AllianceFramesIconsConfig : PartyFramesIconsConfig
    {
        public new static AllianceFramesIconsConfig DefaultConfig()
        {
            var config = new AllianceFramesIconsConfig();
            config.Role.Enabled = false;
            config.Sign.Enabled = false;
            config.Leader.Enabled = false;
            config.PlayerStatus.Enabled = false;
            config.ReadyCheckStatus.Icon.Enabled = false;
            config.WhosTalking.Enabled = false;
            return config;
        }
    }

    [Exportable(false)]
    [Section("Alliance Frames", true)]
    [SubSection("Buffs", 0)]
    public class AllianceFramesBuffsConfig : PartyFramesBuffsConfig
    {
        public new static AllianceFramesBuffsConfig DefaultConfig()
        {
            var durationConfig = new LabelConfig(new Vector2(0, -4), "", DrawAnchor.Bottom, DrawAnchor.Center);
            var stacksConfig = new LabelConfig(new Vector2(-3, 4), "", DrawAnchor.TopRight, DrawAnchor.Center);
            stacksConfig.Color = new(Vector4.UnitW);
            stacksConfig.OutlineColor = new(Vector4.One);
            var iconConfig = new StatusEffectIconConfig(durationConfig, stacksConfig);
            iconConfig.DispellableBorderConfig.Enabled = false;
            iconConfig.Size = new Vector2(24, 24);
            var pos = new Vector2(-2, 2);
            var size = new Vector2(iconConfig.Size.X * 4 + 6, iconConfig.Size.Y);
            var config = new AllianceFramesBuffsConfig(DrawAnchor.TopRight, pos, size, true, false, false, GrowthDirections.Left | GrowthDirections.Down, iconConfig);
            config.Limit = 4;
            config.Enabled = false;
            return config;
        }
        public AllianceFramesBuffsConfig(DrawAnchor anchor, Vector2 position, Vector2 size, bool showBuffs, bool showDebuffs, bool showPermanentEffects,
            GrowthDirections growthDirections, StatusEffectIconConfig iconConfig)
            : base(anchor, position, size, showBuffs, showDebuffs, showPermanentEffects, growthDirections, iconConfig) { }
    }

    [Exportable(false)]
    [Section("Alliance Frames", true)]
    [SubSection("Debuffs", 0)]
    public class AllianceFramesDebuffsConfig : PartyFramesDebuffsConfig
    {
        public new static AllianceFramesDebuffsConfig DefaultConfig()
        {
            var durationConfig = new LabelConfig(new Vector2(0, -4), "", DrawAnchor.Bottom, DrawAnchor.Center);
            var stacksConfig = new LabelConfig(new Vector2(-3, 4), "", DrawAnchor.TopRight, DrawAnchor.Center);
            stacksConfig.Color = new(Vector4.UnitW);
            stacksConfig.OutlineColor = new(Vector4.One);
            var iconConfig = new StatusEffectIconConfig(durationConfig, stacksConfig);
            iconConfig.Size = new Vector2(20, 20);
            iconConfig.DurationLabelConfig.Enabled = false;
            iconConfig.StacksLabelConfig.Enabled = false;
            var pos = new Vector2(-2, -2);
            var size = new Vector2(iconConfig.Size.X * 4 + 4, iconConfig.Size.Y);
            var config = new AllianceFramesDebuffsConfig(DrawAnchor.BottomRight, pos, size, false, true, false, GrowthDirections.Left | GrowthDirections.Up, iconConfig);
            config.Limit = 4;
            return config;
        }
        public AllianceFramesDebuffsConfig(DrawAnchor anchor, Vector2 position, Vector2 size, bool showBuffs, bool showDebuffs, bool showPermanentEffects,
            GrowthDirections growthDirections, StatusEffectIconConfig iconConfig)
            : base(anchor, position, size, showBuffs, showDebuffs, showPermanentEffects, growthDirections, iconConfig) { }
    }

    [Disableable(false)]
    [Exportable(false)]
    [Section("Alliance Frames", true)]
    [SubSection("Trackers", 0)]
    public class AllianceFramesTrackersConfig : PartyFramesTrackersConfig
    {
        public new static AllianceFramesTrackersConfig DefaultConfig()
        {
            var config = new AllianceFramesTrackersConfig();
            config.Raise.Enabled = false;
            config.Raise.Icon.Enabled = false;
            config.Invuln.Enabled = false;
            config.Invuln.Icon.Enabled = false;
            config.Cleanse.Enabled = false;
            return config;
        }
    }

    [Exportable(false)]
    [Section("Alliance Frames", true)]
    [SubSection("Cooldowns", 0)]
    public class AllianceFramesCooldownListConfig : PartyFramesCooldownListConfig
    {
        public new static AllianceFramesCooldownListConfig DefaultConfig()
        {
            var config = new AllianceFramesCooldownListConfig();
            config.Position = new Vector2(-2, 0);
            config.Size = new Vector2(40 * 8 + 6, 40);
            config.Enabled = false;
            return config;
        }
    }

    /// <summary>Base config for alliance frames. Slot 0 and 1 = the two alliances the player is NOT in.</summary>
    public abstract class AllianceFramesConfigBase : MovablePluginConfigObject
    {
        public abstract int SlotIndex { get; }
        public abstract string FramesLabel { get; }

        [Checkbox("Preview", isMonitored = true)]
        [Order(4)]
        public bool Preview = false;

        [DragInt("Rows", spacing = true, isMonitored = true, min = 1, max = 8, velocity = 0.2f)]
        [Order(10)]
        public int Rows = 4;

        [DragInt("Columns", isMonitored = true, min = 1, max = 8, velocity = 0.2f)]
        [Order(11)]
        public int Columns = 2;

        [Anchor("Bars Anchor", isMonitored = true, spacing = true)]
        [Order(15)]
        public DrawAnchor BarsAnchor = DrawAnchor.TopLeft;

        [Checkbox("Fill Rows First", isMonitored = true)]
        [Order(20)]
        public bool FillRowsFirst = true;

        [Checkbox("Show when not in alliance raid", help = "When enabled, shows a placeholder frame outside raids so you can position it. Disable to hide frames entirely when not in a 24-player alliance raid.")]
        [Order(25)]
        public bool ShowWhenNotInRaid = false;

        [NestedConfig("Title Label", 60)]
        public AllianceFramesTitleLabel TitleLabelConfig = new AllianceFramesTitleLabel(Vector2.Zero, "", DrawAnchor.Left, DrawAnchor.Left);

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

    [Exportable(false)]
    [DisableParentSettings("FrameAnchor", "UseJobColor", "UseRoleColor")]
    public class AllianceFramesTitleLabel : LabelConfig
    {
        public AllianceFramesTitleLabel(Vector2 position, string text, DrawAnchor frameAnchor, DrawAnchor textAnchor)
            : base(position, text, frameAnchor, textAnchor) { }
    }

    [Exportable(false)]
    [Section("Alliance Frames", true)]
    [SubSection("Alliance Frames 1", 0)]
    public class AllianceFrames1Config : AllianceFramesConfigBase
    {
        public override int SlotIndex => 0;
        public override string FramesLabel => "Alliance Frames 1";

        public new static AllianceFrames1Config DefaultConfig()
        {
            var config = new AllianceFrames1Config();
            config.Position = new Vector2(-ImGui.GetMainViewport().Size.X / 3 - 180, -350);
            return config;
        }
    }

    [Exportable(false)]
    [Section("Alliance Frames", true)]
    [SubSection("Alliance Frames 2", 0)]
    public class AllianceFrames2Config : AllianceFramesConfigBase
    {
        public override int SlotIndex => 1;
        public override string FramesLabel => "Alliance Frames 2";

        public new static AllianceFrames2Config DefaultConfig()
        {
            var config = new AllianceFrames2Config();
            config.Position = new Vector2(-ImGui.GetMainViewport().Size.X / 3 - 180, -230);
            return config;
        }
    }
}
