using Dalamud.Bindings.ImGui;
using Dalamud.Interface;
using Dalamud.Interface.Utility.Raii;
using HellionChat.Branding;
using HellionChat.Integrations;
using HellionChat.Resources;
using HellionChat.Util;

namespace HellionChat.Ui.SettingsTabs;

// Added in v1.3.0. Each future integration cycle adds a section above
// the "Coming soon" block and removes its stub item.
internal sealed class Integrations : ISettingsTab
{
    private Plugin Plugin { get; }
    private Configuration Mutable { get; }

    public string Name => HellionStrings.Settings_Tab_Integrations + "###tabs-integrations";

    internal Integrations(Plugin plugin, Configuration mutable)
    {
        Plugin = plugin;
        Mutable = mutable;
    }

    public void Draw(bool changed)
    {
        ImGui.TextWrapped(HellionStrings.Settings_Integrations_Intro);
        ImGui.Spacing();
        ImGui.Spacing();

        DrawHonorificSection();
        ImGui.Spacing();
        ImGui.Spacing();

        DrawComingSoonSection();
        ImGui.Spacing();
        ImGui.Spacing();

        DrawGotAnIdeaSection();
    }

    private void DrawHonorificSection()
    {
        DrawSectionHeader(HellionStrings.Settings_Integrations_Honorific_SectionHeader);

        DrawHonorificStatus();
        ImGui.Spacing();

        // Toggle works regardless of detection state: "show when available,
        // hide otherwise". Disabling it when Honorific is missing would force
        // the user to retoggle on every reload.
        if (
            ImGui.Checkbox(
                HellionStrings.Settings_Integrations_Honorific_Toggle,
                ref Mutable.ShowHonorificTitleInHeader
            )
        )
        {
            Plugin.SaveConfig();
        }

        using (ImRaii.PushIndent())
        {
            using (
                ImRaii.PushColor(
                    ImGuiCol.Text,
                    ColourUtil.RgbaToAbgr(Plugin.ThemeRegistry.Active.Colors.TextMuted)
                )
            )
            {
                ImGui.TextWrapped(HellionStrings.Settings_Integrations_Honorific_ToggleHint);
            }

            if (
                ImGui.Checkbox(
                    HellionStrings.Settings_Integrations_Honorific_Glow_Toggle,
                    ref Mutable.ShowHonorificGlow
                )
            )
            {
                Plugin.SaveConfig();
            }
            ImGuiUtil.HelpMarker(HellionStrings.Settings_Integrations_Honorific_Glow_Hint);
        }

        // Honorific has no LICENSE in its repo so we link upstream and author
        // instead of bundling assets. Text labels because FA Brands isn't
        // guaranteed in Dalamud's font set.
        ImGui.Spacing();
        if (ImGui.Button(HellionStrings.Settings_Integrations_Honorific_LinkRepo))
        {
            Plugin.PlatformUtil.OpenLink(IntegrationLinks.HonorificRepo);
        }
        ImGui.SameLine();
        if (ImGui.Button(HellionStrings.Settings_Integrations_Honorific_LinkAuthor))
        {
            Plugin.PlatformUtil.OpenLink(IntegrationLinks.HonorificAuthor);
        }
    }

    private void DrawHonorificStatus()
    {
        var theme = Plugin.ThemeRegistry.Active;
        var service = Plugin.HonorificService;

        if (service.IsAvailable && service.DetectedApiVersion is { } version)
        {
            DrawStatusGlyph('●', theme.Colors.StatusSuccess);
            ImGui.SameLine();
            ImGui.TextUnformatted(
                string.Format(
                    HellionStrings.Settings_Integrations_Honorific_Status_Detected,
                    version.Major,
                    version.Minor
                )
            );
        }
        else if (service.DetectedApiVersion is { } incompatibleVersion)
        {
            DrawStatusGlyph('⚠', theme.Colors.StatusWarning);
            ImGui.SameLine();
            ImGui.TextUnformatted(
                string.Format(
                    HellionStrings.Settings_Integrations_Honorific_Status_Incompatible,
                    HonorificService.ExpectedApiMajor,
                    incompatibleVersion.Major,
                    incompatibleVersion.Minor
                )
            );
        }
        else
        {
            DrawStatusGlyph('○', theme.Colors.TextMuted);
            ImGui.SameLine();
            ImGui.TextUnformatted(
                HellionStrings.Settings_Integrations_Honorific_Status_NotInstalled
            );
        }
    }

    private static void DrawStatusGlyph(char glyph, uint rgba)
    {
        using (ImRaii.PushColor(ImGuiCol.Text, ColourUtil.RgbaToAbgr(rgba)))
        {
            ImGui.TextUnformatted(glyph.ToString());
        }
    }

    private void DrawComingSoonSection()
    {
        DrawSectionHeader(HellionStrings.Settings_Integrations_ComingSoon_SectionHeader);
        ImGui.TextWrapped(HellionStrings.Settings_Integrations_ComingSoon_Intro);
        ImGui.Spacing();

        // Each integration cycle removes its stub here and adds a full section above.
        DrawComingSoonItem(
            HellionStrings.Settings_Integrations_ComingSoon_ContextMenu_Title,
            HellionStrings.Settings_Integrations_ComingSoon_ContextMenu_Description
        );
        DrawComingSoonItem(
            HellionStrings.Settings_Integrations_ComingSoon_Notifications_Title,
            HellionStrings.Settings_Integrations_ComingSoon_Notifications_Description
        );
        DrawComingSoonItem(
            HellionStrings.Settings_Integrations_ComingSoon_RPStatus_Title,
            HellionStrings.Settings_Integrations_ComingSoon_RPStatus_Description
        );
        DrawComingSoonItem(
            HellionStrings.Settings_Integrations_ComingSoon_ExtraChat_Title,
            HellionStrings.Settings_Integrations_ComingSoon_ExtraChat_Description
        );
        DrawComingSoonItem(
            HellionStrings.Settings_Integrations_ComingSoon_QuickDM_Title,
            HellionStrings.Settings_Integrations_ComingSoon_QuickDM_Description
        );
    }

    private void DrawComingSoonItem(string title, string description)
    {
        var theme = Plugin.ThemeRegistry.Active;
        using (ImRaii.PushColor(ImGuiCol.Text, ColourUtil.RgbaToAbgr(theme.Colors.TextMuted)))
        using (Plugin.FontManager.FontAwesome.Push())
        {
            ImGui.TextUnformatted(FontAwesomeIcon.Hourglass.ToIconString());
        }
        ImGui.SameLine();
        ImGui.TextUnformatted(title);
        using (ImRaii.PushIndent())
        {
            using (ImRaii.PushColor(ImGuiCol.Text, ColourUtil.RgbaToAbgr(theme.Colors.TextMuted)))
            {
                ImGui.TextWrapped(description);
            }
        }
        ImGui.Spacing();
    }

    private void DrawGotAnIdeaSection()
    {
        DrawSectionHeader(HellionStrings.Settings_Integrations_GotAnIdea_SectionHeader);
        ImGui.TextWrapped(HellionStrings.Settings_Integrations_GotAnIdea_Body);
        ImGui.Spacing();

        if (ImGui.Button(HellionStrings.Settings_Integrations_GotAnIdea_LinkLabel))
        {
            Plugin.PlatformUtil.OpenLink(BrandingLinks.HellionForgeDiscordInvite);
        }
    }

    private void DrawSectionHeader(string label)
    {
        var theme = Plugin.ThemeRegistry.Active;
        using (ImRaii.PushColor(ImGuiCol.Text, ColourUtil.RgbaToAbgr(theme.Colors.Primary)))
        {
            ImGui.TextUnformatted("── " + label + " ──");
        }
    }
}
