using Dalamud.Configuration;
using ImGuiNET;
using System;
using System.Numerics;
using System.Linq;
using JobBars.Gauges;
using JobBars.UI;
using JobBars.Cursors;
using JobBars.Gauges.Rolling;
using System.Collections.Generic;
using JobBars.Cooldowns;

namespace JobBars.Data {
    public enum GaugePositionType {
        Global,
        PerJob,
        Split
    }

    public enum CursorPositionType {
        MouseCursor,
        Middle,
        CustomPosition
    }

    public enum AttachAddon {
        Chatbox,
        HP_MP_Bars,
        PartyList
    }

    public struct CustomCooldownProps {
        public string Name;
        public JobIds Job;
        public CooldownProps Props;
    }

    [Serializable]
    public class Configuration : IPluginConfiguration {
        public int Version { get; set; } = 2;

        public bool Use4K = true;

        public AttachAddon AttachAddon = AttachAddon.Chatbox;

        public AttachAddon CooldownAttachAddon = AttachAddon.PartyList;

        // ====== GAUGES ======

        public float GaugeScale = 1.0f;
        public bool 横向量谱 = false;
        public bool GaugeAlignRight = false;
        public bool 量谱从下到上 = false;

        public bool GaugesEnabled = true;
        public bool GaugesHideOutOfCombat = false;
        public bool GaugesHideWeaponSheathed = false;
        public bool GaugeGCDTextVisible = true;

        public GaugePositionType 量谱位置类型 = GaugePositionType.Global;
        public Vector2 GaugePositionGlobal = new(200, 200); // global
        public VectorValueConfig GaugePerJobPosition = new(new Vector2(200, 200)); // per job
        public VectorValueConfig GaugeSplitPosition = new(new Vector2(200, 200)); // split

        public FloatValueConfig GaugeIndividualScale = new(1.0f);
        public BoolValueConfig GaugeEnabled = new(true);
        public IntValueConfig GaugeOrder = new(-1);
        public BoolValueConfig GaugeVertical = new(false);
        public IntValueConfig 量谱进程顺序 = new(-1);
        public ColorConfig 量谱进程颜色 = new();
        public BoolValueConfig GaugeHideInactive = new(false);
        public BoolValueConfig GaugeInvert = new(false);
        public BoolValueConfig GaugeShowSegments = new(true);
        public BoolValueConfig GaugeReverseFill = new(false);
        public ColorConfig 量谱颜色 = new();
        public ComboValueConfig<GaugeVisualType> GaugeType = new();
        public BoolValueConfig 量谱显示文本 = new(true);
        public BoolValueConfig GaugeSwapText = new(false);
        public FloatValueConfig GaugeTimerOffset = new(0f);
        public ComboValueConfig<GaugeGCDRollingType> GaugeGCDRolling = new();
        public float GaugeSlidecastTime = 0.5f;
        public IntValueConfig GaugeMaxGcds = new(1);

        public ComboValueConfig<GaugeCompleteSoundType> 量谱填满音效 = new(); // GCD, stacks, charges
        public FloatValueConfig GaugeLowTimerWarning_2 = new(4.0f);
        public IntValueConfig GaugeSoundEffect_2 = new(0);
        public IntValueConfig GaugeCompletionSoundEffect_2 = new(78);

        public bool GaugePulse = true;

        // ===== BUFFS ======

        public Vector2 BuffPosition = new(200, 200);
        public float BuffScale = 1.0f;

        public bool BuffBarEnabled = true;
        public bool BuffPartyListASTText = false;
        public bool BuffHideOutOfCombat = false;
        public bool BuffHideWeaponSheathed = false;
        public bool BuffIncludeParty = true;
        public bool BuffOrderByActive = true;
        public float BuffDisplayTimer = 30f;
        public bool BuffThinBorder = false;
        public bool BuffSquare = false;
        public float BuffOnCDOpacity = 1.0f;

        public BoolValueConfig 启用Buff = new(true);
        public BoolValueConfig 小队列表Buff高亮 = new(true);

        public int BuffHorizontal = 5;
        public bool BuffRightToLeft = false;
        public bool BuffBottomToTop = false;
        public int BuffTextSize_v2 = 23;

        // ===== COOLDOWNS ======

        public Vector2 CooldownPosition = new(-40, 40);
        public float CooldownScale = 1.0f;
        public float CooldownsSpacing = 40f;

        public bool CooldownsEnabled = true;
        public bool CooldownsHideOutOfCombat = false;
        public bool CooldownsHideWeaponSheathed = false;
        public bool CooldownsHideActiveBuffDuration = false;
        public bool CooldownsShowPartyMembers = true;
        public float CooldownsOnCDOpacity = 1.0f;

        public bool CooldownsStateShowDefault = true;
        public bool CooldownsStateShowRunning = true;
        public bool CooldownsStateShowOnCD = true;
        public bool CooldownsStateShowOffCD = true;

        public bool CooldownsLeftAligned = false;

        public BoolValueConfig CooldownEnabled = new(true);
        public IntValueConfig CooldownOrder = new(-1);
        public BoolValueConfig CooldownShowBorderWhenActive = new(true);
        public BoolValueConfig CooldownShowBorderWhenOffCD = new(false);

        public List<CustomCooldownProps> CustomCooldown = new();

        // ===== CURSOR =======

        public bool CursorsEnabled = false;
        public bool CursorHideWhenHeld = false;
        public bool CursorHideWeaponSheathed = false;
        public bool CursorHideOutOfCombat = false;
        public CursorPositionType CursorPosition = CursorPositionType.MouseCursor;
        public Vector2 CursorCustomPosition = new(200, 200);
        public float CursorInnerScale = 1.5f;
        public float CursorOuterScale = 1.2f;
        public string CursorInnerColor = UIColor.法条色.Name;
        public string CursorOuterColor = UIColor.HealthGreen.Name;

        public ComboValueConfig<CursorType> CursorType = new();
        public ComboValueConfig<Helper.ItemData> CursorStatus = new(true);
        public FloatValueConfig CursorStatusDuration = new(5f);

        // ===== ICONS ===========

        public bool IconsEnabled = true;
        public bool IconBuffLarge = true;
        public bool IconTimerLarge = false;
        public BoolValueConfig IconEnabled = new(true);
        public ComboValueConfig<UIIconComboType> IconComboType = new();
        public FloatValueConfig IconTimerOffset = new(0f);
        public BoolValueConfig IconTimerRing = new(true);

        // =====================

        public void RemoveCustomCooldown(string name) {
            CustomCooldown.RemoveAll(x => x.Name == name);
            Save();
        }

        public void AddCustomCooldown(string name, JobIds job, CooldownProps props) {
            CustomCooldown.Add(new CustomCooldownProps {
                Job = job,
                Name = name,
                Props = props
            });
            Save();
        }

        public void Save() {
            JobBarsCN.PluginInterface.SavePluginConfig(this);
        }

        public static bool DrawColor(string id, ElementColor currentValue, out ElementColor value) {
            value = currentValue;
            if (ImGui.BeginCombo(id, value.Name)) {
                foreach (var entry in UIColor.AllColors) {
                    if (ImGui.Selectable($"{entry.Key}##连击", value.Name == entry.Key)) {
                        value = entry.Value;
                        ImGui.EndCombo();
                        return true;
                    }
                }
                ImGui.EndCombo();
            }
            return false;
        }
    }
}
