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

namespace HellionChat.Ui.SettingsTabs;

internal sealed class Window : ISettingsTab
{
    private Plugin Plugin { get; }
    private Configuration Mutable { get; }

    public string Name => HellionStrings.Settings_Tab_Window + "###tabs-window";

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

    public void Draw(bool sectionJustEntered)
    {
        DrawHideSection(sectionJustEntered);
        ImGui.Spacing();
        DrawInactivityHideSection(sectionJustEntered);
        ImGui.Spacing();
        DrawFrameSection(sectionJustEntered);
    }

    private void DrawHideSection(bool sectionJustEntered)
    {
        if (sectionJustEntered)
            ImGui.SetNextItemOpen(false);
        using var tree = ImRaii.TreeNode(HellionStrings.Settings_Section_Hide);
        if (!tree.Success)
        {
            return;
        }

        using (ImRaii.PushIndent(ImGui.GetStyle().IndentSpacing, false))
        {
            ImGui.Checkbox(Language.Options_HideChat_Name, ref Mutable.HideChat);
            ImGuiUtil.HelpMarker(Language.Options_HideChat_Description);

            ImGui.Checkbox(
                Language.Options_HideDuringCutscenes_Name,
                ref Mutable.HideDuringCutscenes
            );
            ImGuiUtil.HelpMarker(
                string.Format(Language.Options_HideDuringCutscenes_Description, Plugin.PluginName)
            );

            ImGui.Checkbox(
                Language.Options_HideWhenNotLoggedIn_Name,
                ref Mutable.HideWhenNotLoggedIn
            );
            ImGuiUtil.HelpMarker(
                string.Format(Language.Options_HideWhenNotLoggedIn_Description, Plugin.PluginName)
            );

            ImGui.Checkbox(Language.Options_HideWhenUiHidden_Name, ref Mutable.HideWhenUiHidden);
            ImGuiUtil.HelpMarker(
                string.Format(Language.Options_HideWhenUiHidden_Description, Plugin.PluginName)
            );

            ImGui.Checkbox(
                Language.Options_HideInLoadingScreens_Name,
                ref Mutable.HideInLoadingScreens
            );
            ImGuiUtil.HelpMarker(
                string.Format(Language.Options_HideInLoadingScreens_Description, Plugin.PluginName)
            );

            ImGui.Checkbox(Language.Options_HideInBattle_Name, ref Mutable.HideInBattle);
            ImGuiUtil.HelpMarker(Language.Options_HideInBattle_Description);

            ImGui.Checkbox(
                Language.Options_HideInNewGamePlusMenu_Name,
                ref Mutable.HideInNewGamePlusMenu
            );
            ImGuiUtil.HelpMarker(Language.Options_HideInNewGamePlusMenu_Description);
        }
    }

    private void DrawInactivityHideSection(bool sectionJustEntered)
    {
        if (sectionJustEntered)
            ImGui.SetNextItemOpen(false);
        using var tree = ImRaii.TreeNode(HellionStrings.Settings_Section_InactivityHide);
        if (!tree.Success)
        {
            return;
        }

        using (ImRaii.PushIndent(ImGui.GetStyle().IndentSpacing, false))
        {
            ImGui.Checkbox(Language.Options_HideWhenInactive_Name, ref Mutable.HideWhenInactive);
            ImGuiUtil.HelpMarker(Language.Options_HideWhenInactive_Description);

            if (!Mutable.HideWhenInactive)
            {
                return;
            }

            ImGuiUtil.InputIntVertical(
                Language.Options_InactivityHideTimeout_Name,
                Language.Options_InactivityHideTimeout_Description,
                ref Mutable.InactivityHideTimeout,
                1,
                10
            );
            // Floor at 2 seconds to prevent self-soft-lock.
            Mutable.InactivityHideTimeout = Math.Max(2, Mutable.InactivityHideTimeout);

            using (ImRaii.Disabled(Mutable.HideInBattle))
            {
                ImGui.Checkbox(
                    Language.Options_InactivityHideActiveDuringBattle_Name,
                    ref Mutable.InactivityHideActiveDuringBattle
                );
                ImGuiUtil.HelpMarker(Language.Options_InactivityHideActiveDuringBattle_Description);
            }

            using var channelTree = ImRaii.TreeNode(Language.Options_InactivityHideChannels_Name);
            if (!channelTree.Success)
            {
                return;
            }

            if (
                ImGuiUtil.CtrlShiftButton(
                    Language.Options_InactivityHideChannels_All_Label,
                    Language.Options_InactivityHideChannels_Button_Tooltip
                )
            )
            {
                Mutable.InactivityHideChannelsV2 = TabsUtil.AllChannels();
                Mutable.InactivityHideExtraChatAll = true;
                Mutable.InactivityHideExtraChatChannels = [];
            }

            ImGui.SameLine();
            if (
                ImGuiUtil.CtrlShiftButton(
                    Language.Options_InactivityHideChannels_None_Label,
                    Language.Options_InactivityHideChannels_Button_Tooltip
                )
            )
            {
                Mutable.InactivityHideChannelsV2 = [];
                Mutable.InactivityHideExtraChatAll = false;
                Mutable.InactivityHideExtraChatChannels = [];
            }

            ImGui.Spacing();

            ImGuiUtil.ChannelSelector(
                Language.Options_Tabs_Channels,
                Mutable.InactivityHideChannelsV2
            );
            ImGuiUtil.ExtraChatSelector(
                Language.Options_Tabs_ExtraChatChannels,
                ref Mutable.InactivityHideExtraChatAll,
                Mutable.InactivityHideExtraChatChannels
            );
        }
    }

    private void DrawFrameSection(bool sectionJustEntered)
    {
        if (sectionJustEntered)
            ImGui.SetNextItemOpen(false);
        using var tree = ImRaii.TreeNode(HellionStrings.Settings_Section_Frame);
        if (!tree.Success)
        {
            return;
        }

        using (ImRaii.PushIndent(ImGui.GetStyle().IndentSpacing, false))
        {
            ImGui.Checkbox(Language.Options_CanMove_Name, ref Mutable.CanMove);
            ImGui.Checkbox(Language.Options_CanResize_Name, ref Mutable.CanResize);

            ImGui.Checkbox(
                HellionStrings.Settings_Window_PopOutInputEnabled_Name,
                ref Mutable.PopOutInputEnabled
            );
            ImGuiUtil.HelpMarker(HellionStrings.Settings_Window_PopOutInputEnabled_Description);

            ImGui.Spacing();

            // Fallback for off-screen windows after a display layout change.
            if (ImGui.Button(HellionStrings.Settings_Window_ResetPosition_Name))
                Plugin.ChatLogWindow.RequestPositionReset = true;
            ImGuiUtil.HelpMarker(HellionStrings.Settings_Window_ResetPosition_Description);
        }
    }
}
