using Dalamud.Interface.Windowing;
using Dalamud.Utility;
using SpotifyHonorific.Updaters;
using SpotifyHonorific.Utils;
using Dalamud.Bindings.ImGui;
using System.Numerics;

namespace SpotifyHonorific.Windows;

public class ConfigWindow : Window
{
    private Config Config { get; init; }
    private ImGuiHelper ImGuiHelper { get; init; }
    private Updater Updater { get; init; }

    public ConfigWindow(Config config, ImGuiHelper imGuiHelper, Updater updater) : base("Spotify Honorific Config (Modified By Lozy Rhel)##configWindow")
    {
        SizeConstraints = new WindowSizeConstraints
        {
            MinimumSize = new Vector2(760, 420),
            MaximumSize = new Vector2(float.MaxValue, float.MaxValue)
        };

        Config = config;
        ImGuiHelper = imGuiHelper;
        Updater = updater;
    }

    public override void Draw()
    {
        var enabled = Config.Enabled;
        if (ImGui.Checkbox("Enabled##enabled", ref enabled))
        {
            Config.Enabled = enabled;
            Config.Save();
            Updater.Enable(enabled);
        }



        if (ImGui.BeginTabBar("activityConfigsTabBar"))
        {
            foreach (var activityConfig in Config.ActivityConfigs)
            {
                var activityConfigId = $"activityConfigs{activityConfig.GetHashCode()}";

                var name = activityConfig.Name;
                if (ImGui.BeginTabItem($"{(name.IsNullOrWhitespace() ? "(Blank)" : name)}###{activityConfigId}TabItem"))
                {
                    ImGui.Indent(10);

                    var activityConfigEnabled = activityConfig.Enabled;
                    if (ImGui.Checkbox($"Enabled###{activityConfigId}enabled", ref activityConfigEnabled))
                    {
                        activityConfig.Enabled = activityConfigEnabled;
                        Config.Save();
                    }



                    if (ImGui.InputText($"Name###{activityConfigId}Name", ref name, ushort.MaxValue))
                    {
                        activityConfig.Name = name;
                        Config.Save();
                    }

                    var priority = activityConfig.Priority;
                    if (ImGui.InputInt($"Priority###{activityConfigId}Priority", ref priority, 1))
                    {
                        activityConfig.Priority = priority;
                        Config.Save();
                    }

                    var typeName = activityConfig.TypeName;

                    var filterTemplate = activityConfig.FilterTemplate;
                    var filterTemplateInput = ImGui.InputTextMultiline($"Filter Template (scriban)###{activityConfigId}FilterTemplate", ref filterTemplate, ushort.MaxValue, new(ImGui.GetWindowWidth() - 170, 50));
                    if (ImGui.IsItemHovered())
                    {
                        ImGui.SetTooltip("Expects parsable boolean as output if provided\nSyntax reference available on https://github.com/scriban/scriban");
                    }
                    if (filterTemplateInput)
                    {
                        activityConfig.FilterTemplate = filterTemplate;
                        Config.Save();
                    }

                    var titleTemplate = activityConfig.TitleTemplate;
                    var titleTemplateInput = ImGui.InputTextMultiline($"Title Template (scriban)###{activityConfigId}TitleTemplate", ref titleTemplate, ushort.MaxValue, new(ImGui.GetWindowWidth() - 170, ImGui.GetWindowHeight() - ImGui.GetCursorPosY() - 40));
                    if (ImGui.IsItemHovered())
                    {
                        ImGui.SetTooltip("Expects single line as output\nSyntax reference available on https://github.com/scriban/scriban");
                    }
                    if (titleTemplateInput)
                    {
                        activityConfig.TitleTemplate = titleTemplate;
                        Config.Save();
                    }

                    var isPrefix = activityConfig.IsPrefix;
                    if (ImGui.Checkbox($"Prefix###{activityConfigId}Prefix", ref isPrefix))
                    {
                        activityConfig.IsPrefix = isPrefix;
                        Config.Save();
                    }
                    ImGui.SameLine();
                    ImGui.Spacing();
                    ImGui.SameLine();

                    var checkboxSize = new Vector2(ImGui.GetTextLineHeightWithSpacing(), ImGui.GetTextLineHeightWithSpacing());

                    var color = activityConfig.Color;
                    if (ImGuiHelper.DrawColorPicker($"Color###{activityConfigId}Color", ref color, checkboxSize))
                    {
                        activityConfig.Color = color;
                        Config.Save();
                    }

                    ImGui.SameLine();
                    ImGui.Spacing();
                    ImGui.SameLine();
                    var glow = activityConfig.Glow;
                    if (ImGuiHelper.DrawColorPicker($"Glow###{activityConfigId}Glow", ref glow, checkboxSize))
                    {
                        activityConfig.Glow = glow;
                        Config.Save();
                    }

                    ImGui.Unindent();
                    ImGui.EndTabItem();
                }
            }
            ImGui.EndTabBar();
        } 
    }
}
