#include "platform/platform.h"
#include "renderer/renderer.h"
#include "imgui.h"
#include "imgui_internal.h"

static void build_default_layout(ImGuiID dockspace_id)
{
    ImGui::DockBuilderRemoveNode(dockspace_id);
    ImGui::DockBuilderAddNode(dockspace_id, ImGuiDockNodeFlags_DockSpace);
    ImGui::DockBuilderSetNodeSize(dockspace_id, ImGui::GetMainViewport()->Size);

    ImGuiID center = dockspace_id;
    ImGuiID left   = ImGui::DockBuilderSplitNode(center, ImGuiDir_Left,  0.15f, nullptr, &center);
    ImGuiID right  = ImGui::DockBuilderSplitNode(center, ImGuiDir_Right, 0.20f, nullptr, &center);
    ImGuiID bottom = ImGui::DockBuilderSplitNode(center, ImGuiDir_Down,  0.25f, nullptr, &center);

    ImGui::DockBuilderDockWindow("Browser",    left);
    ImGui::DockBuilderDockWindow("Main",       center);
    ImGui::DockBuilderDockWindow("Properties", right);
    ImGui::DockBuilderDockWindow("Log",        bottom);

    ImGui::DockBuilderFinish(dockspace_id);
}

int main(int argc, char **argv)
{
    (void)argc;
    (void)argv;

    PlatformWindowDesc window_desc = {};
    PlatformWindow *window = platform_create_window(&window_desc);
    if (!window)
        return 1;

    int32_t w, h;
    platform_get_size(window, &w, &h);

    RendererDesc renderer_desc = {};
    renderer_desc.window_handle = platform_get_native_handle(window);
    renderer_desc.width         = w;
    renderer_desc.height        = h;
    Renderer *renderer = renderer_create(&renderer_desc);
    if (!renderer) {
        platform_destroy_window(window);
        return 1;
    }

    int32_t last_w = w, last_h = h;
    bool show_demo = true;
    bool first_frame = true;

    while (platform_poll_events(window)) {
        platform_get_size(window, &w, &h);
        if (w != last_w || h != last_h) {
            renderer_resize(renderer, w, h);
            last_w = w;
            last_h = h;
        }

        if (!renderer_begin_frame(renderer))
            continue;

        // Full-window dockspace
        ImGuiID dockspace_id = ImGui::GetID("MainDockSpace");
        ImGui::DockSpaceOverViewport(dockspace_id, ImGui::GetMainViewport());

        if (first_frame) {
            build_default_layout(dockspace_id);
            first_frame = false;
        }

        // Left panel
        ImGui::Begin("Browser");
        ImGui::Text("Instruments");
        ImGui::Separator();
        ImGui::End();

        // Main content
        ImGui::Begin("Main");
        ImGui::Text("Main content area");
        ImGui::End();

        // Right panel
        ImGui::Begin("Properties");
        ImGui::Text("Details");
        ImGui::Separator();
        ImGui::End();

        // Bottom panel
        ImGui::Begin("Log");
        ImGui::Text("Output / Log");
        ImGui::Separator();
        ImGui::End();

        // Demo window
        if (show_demo)
            ImGui::ShowDemoWindow(&show_demo);

        renderer_end_frame(renderer);
    }

    renderer_destroy(renderer);
    platform_destroy_window(window);
    return 0;
}
