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

enum MenuCmd {
    MENU_NONE = 0,
    MENU_FILE_NEW,
    MENU_FILE_OPEN,
    MENU_FILE_SAVE,
    MENU_FILE_SAVE_AS,
    MENU_FILE_EXIT,
    MENU_IMPORT_AUDIO,
    MENU_IMPORT_MIDI,
    MENU_VIEW_BROWSER,
    MENU_VIEW_PROPERTIES,
    MENU_VIEW_LOG,
    MENU_VIEW_DEMO,
};

static void setup_menus(PlatformWindow *window) {
    PlatformMenuItem file_items[] = {
        { "New",        MENU_FILE_NEW },
        { "Open...",    MENU_FILE_OPEN },
        { "Save",       MENU_FILE_SAVE },
        { "Save As...", MENU_FILE_SAVE_AS },
        { nullptr,      0 },
        { "Exit",       MENU_FILE_EXIT },
    };

    PlatformMenuItem import_items[] = {
        { "Audio...",   MENU_IMPORT_AUDIO },
        { "MIDI...",    MENU_IMPORT_MIDI },
    };

    PlatformMenuItem view_items[] = {
        { "Browser",    MENU_VIEW_BROWSER },
        { "Properties", MENU_VIEW_PROPERTIES },
        { "Log",        MENU_VIEW_LOG },
        { nullptr,      0 },
        { "Demo",       MENU_VIEW_DEMO },
    };

    PlatformMenu menus[] = {
        { "File",   file_items,   sizeof(file_items)   / sizeof(file_items[0]) },
        { "Import", import_items, sizeof(import_items)  / sizeof(import_items[0]) },
        { "View",   view_items,   sizeof(view_items)   / sizeof(view_items[0]) },
    };

    platform_set_menu(window, menus, sizeof(menus) / sizeof(menus[0]));
}

static void setup_theme() {
    ImGuiIO &io = ImGui::GetIO();

    // Load Segoe UI from Windows system fonts
    ImFontConfig font_cfg = {};
    font_cfg.OversampleH = 2;
    font_cfg.OversampleV = 1;
    font_cfg.PixelSnapH  = true;
    io.Fonts->AddFontFromFileTTF("C:\\Windows\\Fonts\\segoeui.ttf", 15.0f, &font_cfg);

    // DAW-style dark theme
    ImGuiStyle &s = ImGui::GetStyle();

    // Geometry
    s.WindowPadding     = ImVec2(8, 8);
    s.FramePadding      = ImVec2(6, 4);
    s.ItemSpacing       = ImVec2(8, 4);
    s.ItemInnerSpacing  = ImVec2(4, 4);
    s.ScrollbarSize     = 12.0f;
    s.GrabMinSize       = 8.0f;
    s.WindowBorderSize  = 1.0f;
    s.FrameBorderSize   = 0.0f;
    s.TabBorderSize     = 0.0f;
    s.WindowRounding    = 2.0f;
    s.FrameRounding     = 2.0f;
    s.GrabRounding      = 2.0f;
    s.TabRounding       = 2.0f;
    s.ScrollbarRounding = 2.0f;

    ImVec4 *c = s.Colors;

    // Backgrounds
    c[ImGuiCol_WindowBg]            = ImVec4(0.12f, 0.12f, 0.13f, 1.00f);
    c[ImGuiCol_ChildBg]             = ImVec4(0.12f, 0.12f, 0.13f, 1.00f);
    c[ImGuiCol_PopupBg]             = ImVec4(0.15f, 0.15f, 0.16f, 1.00f);

    // Borders
    c[ImGuiCol_Border]              = ImVec4(0.22f, 0.22f, 0.24f, 1.00f);
    c[ImGuiCol_BorderShadow]        = ImVec4(0.00f, 0.00f, 0.00f, 0.00f);

    // Text
    c[ImGuiCol_Text]                = ImVec4(0.88f, 0.88f, 0.88f, 1.00f);
    c[ImGuiCol_TextDisabled]        = ImVec4(0.44f, 0.44f, 0.44f, 1.00f);

    // Headers (collapsing headers, menu bar items)
    c[ImGuiCol_Header]              = ImVec4(0.20f, 0.20f, 0.22f, 1.00f);
    c[ImGuiCol_HeaderHovered]       = ImVec4(0.28f, 0.28f, 0.30f, 1.00f);
    c[ImGuiCol_HeaderActive]        = ImVec4(0.24f, 0.24f, 0.26f, 1.00f);

    // Buttons
    c[ImGuiCol_Button]              = ImVec4(0.22f, 0.22f, 0.24f, 1.00f);
    c[ImGuiCol_ButtonHovered]       = ImVec4(0.30f, 0.30f, 0.33f, 1.00f);
    c[ImGuiCol_ButtonActive]        = ImVec4(0.26f, 0.26f, 0.28f, 1.00f);

    // Frame backgrounds (inputs, checkboxes, sliders)
    c[ImGuiCol_FrameBg]             = ImVec4(0.16f, 0.16f, 0.17f, 1.00f);
    c[ImGuiCol_FrameBgHovered]      = ImVec4(0.20f, 0.20f, 0.22f, 1.00f);
    c[ImGuiCol_FrameBgActive]       = ImVec4(0.18f, 0.18f, 0.20f, 1.00f);

    // Tabs
    c[ImGuiCol_Tab]                 = ImVec4(0.16f, 0.16f, 0.17f, 1.00f);
    c[ImGuiCol_TabHovered]          = ImVec4(0.28f, 0.28f, 0.30f, 1.00f);
    c[ImGuiCol_TabSelected]         = ImVec4(0.20f, 0.20f, 0.22f, 1.00f);
    c[ImGuiCol_TabSelectedOverline] = ImVec4(0.34f, 0.54f, 0.69f, 1.00f);
    c[ImGuiCol_TabDimmed]           = ImVec4(0.12f, 0.12f, 0.13f, 1.00f);
    c[ImGuiCol_TabDimmedSelected]   = ImVec4(0.16f, 0.16f, 0.17f, 1.00f);

    // Title bar
    c[ImGuiCol_TitleBg]             = ImVec4(0.10f, 0.10f, 0.11f, 1.00f);
    c[ImGuiCol_TitleBgActive]       = ImVec4(0.13f, 0.13f, 0.14f, 1.00f);
    c[ImGuiCol_TitleBgCollapsed]    = ImVec4(0.10f, 0.10f, 0.11f, 1.00f);

    // Scrollbar
    c[ImGuiCol_ScrollbarBg]         = ImVec4(0.10f, 0.10f, 0.11f, 1.00f);
    c[ImGuiCol_ScrollbarGrab]       = ImVec4(0.24f, 0.24f, 0.26f, 1.00f);
    c[ImGuiCol_ScrollbarGrabHovered]= ImVec4(0.30f, 0.30f, 0.33f, 1.00f);
    c[ImGuiCol_ScrollbarGrabActive] = ImVec4(0.34f, 0.34f, 0.37f, 1.00f);

    // Slider grab
    c[ImGuiCol_SliderGrab]          = ImVec4(0.34f, 0.54f, 0.69f, 1.00f);
    c[ImGuiCol_SliderGrabActive]    = ImVec4(0.40f, 0.60f, 0.75f, 1.00f);

    // Checkmark, accent
    c[ImGuiCol_CheckMark]           = ImVec4(0.34f, 0.54f, 0.69f, 1.00f);

    // Separator
    c[ImGuiCol_Separator]           = ImVec4(0.22f, 0.22f, 0.24f, 1.00f);
    c[ImGuiCol_SeparatorHovered]    = ImVec4(0.34f, 0.54f, 0.69f, 1.00f);
    c[ImGuiCol_SeparatorActive]     = ImVec4(0.34f, 0.54f, 0.69f, 1.00f);

    // Resize grip
    c[ImGuiCol_ResizeGrip]          = ImVec4(0.22f, 0.22f, 0.24f, 0.50f);
    c[ImGuiCol_ResizeGripHovered]   = ImVec4(0.34f, 0.54f, 0.69f, 0.67f);
    c[ImGuiCol_ResizeGripActive]    = ImVec4(0.34f, 0.54f, 0.69f, 0.95f);

    // Docking
    c[ImGuiCol_DockingPreview]      = ImVec4(0.34f, 0.54f, 0.69f, 0.70f);
    c[ImGuiCol_DockingEmptyBg]      = ImVec4(0.10f, 0.10f, 0.11f, 1.00f);

    // Menu bar
    c[ImGuiCol_MenuBarBg]           = ImVec4(0.14f, 0.14f, 0.15f, 1.00f);
}

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;
    }

    setup_theme();
    setup_menus(window);

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

    while (platform_poll_events(window)) {
        int32_t menu_cmd = platform_poll_menu_command(window);
        switch (menu_cmd) {
        case MENU_FILE_EXIT:      platform_destroy_window(window); return 0;
        case MENU_VIEW_BROWSER:   show_browser = !show_browser; break;
        case MENU_VIEW_PROPERTIES:show_props   = !show_props;   break;
        case MENU_VIEW_LOG:       show_log     = !show_log;     break;
        case MENU_VIEW_DEMO:      show_demo    = !show_demo;    break;
        default: break;
        }
        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
        if (show_browser) {
            ImGui::Begin("Browser", &show_browser);
            ImGui::Text("Instruments");
            ImGui::Separator();
            ImGui::End();
        }

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

        // Right panel
        if (show_props) {
            ImGui::Begin("Properties", &show_props);
            ImGui::Text("Details");
            ImGui::Separator();
            ImGui::End();
        }

        // Bottom panel
        if (show_log) {
            ImGui::Begin("Log", &show_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;
}
