#include "core/docking_layout.h"
#include "imgui_internal.h"

ImGuiID docking_layout(DockPreset preset) {
    const ImGuiViewport* viewport = ImGui::GetMainViewport();
    ImGuiID dockspace_id = ImGui::DockSpaceOverViewport(0, viewport);

    static bool initialized = false;
    if (initialized) {
        return dockspace_id;
    }
    initialized = true;

    if (preset == DockPreset::Default) {
        return dockspace_id;
    }

    ImGui::DockBuilderRemoveNode(dockspace_id);
    ImGui::DockBuilderAddNode(dockspace_id, ImGuiDockNodeFlags_DockSpace);
    ImGui::DockBuilderSetNodeSize(dockspace_id, viewport->Size);

    if (preset == DockPreset::TwoColumns) {
        ImGuiID right;
        ImGui::DockBuilderSplitNode(dockspace_id, ImGuiDir_Left, 0.6f, nullptr, &right);

    } else if (preset == DockPreset::ThreeColumns) {
        ImGuiID center, right;
        ImGuiID left_node;
        ImGui::DockBuilderSplitNode(dockspace_id, ImGuiDir_Left, 0.33f, &left_node, &center);
        ImGui::DockBuilderSplitNode(center, ImGuiDir_Left, 0.5f, nullptr, &right);

    } else if (preset == DockPreset::SidebarLeft) {
        ImGuiID main;
        ImGui::DockBuilderSplitNode(dockspace_id, ImGuiDir_Left, 0.25f, nullptr, &main);

    } else if (preset == DockPreset::SidebarRight) {
        ImGuiID sidebar;
        ImGui::DockBuilderSplitNode(dockspace_id, ImGuiDir_Right, 0.25f, &sidebar, nullptr);

    } else if (preset == DockPreset::TopBottom) {
        ImGuiID bottom;
        ImGui::DockBuilderSplitNode(dockspace_id, ImGuiDir_Up, 0.6f, nullptr, &bottom);

    } else if (preset == DockPreset::Dashboard) {
        ImGuiID top, bottom;
        ImGui::DockBuilderSplitNode(dockspace_id, ImGuiDir_Up, 0.5f, &top, &bottom);
        ImGui::DockBuilderSplitNode(top,    ImGuiDir_Left, 0.5f, nullptr, nullptr);
        ImGui::DockBuilderSplitNode(bottom, ImGuiDir_Left, 0.5f, nullptr, nullptr);
    }

    ImGui::DockBuilderFinish(dockspace_id);
    return dockspace_id;
}
