#include <nng/nng.h>
#include <nng/protocol/pubsub0/pub.h>
#include <nng/protocol/pubsub0/sub.h>

#include <imgui.h>
#include <imgui_internal.h>
#include <backends/imgui_impl_sdl.h>
#include <backends/imgui_impl_sdlrenderer.h>
#include <SDL.h>

#include <algorithm>
#include <array>
#include <cerrno>
#include <cstdlib>
#include <cstring>
#include <thread>
#include <type_traits>
#include <vector>

#include <signal.h>
#ifdef __WIN32
#include <windows.h>
#else
#include <spawn.h>
#include <sys/wait.h>
#include <unistd.h>
#endif

#include "dp_util.hpp"
#include "doomlib.hpp"

#if DoomBytesPerPixel == 4
static const u32 DoomSdlTexturePixelFormat = SDL_PIXELFORMAT_ARGB8888;
#else
#error Unhandled DoomBytesPerPixel value. Which SDL_PIXELFORMAT to use?
#endif

void dp_sdl_fatal(const char *const msg)
{
    log_fatal("%s: %s", msg, SDL_GetError());
    abort();
}

void dp_sdl_error(const char *const msg)
{
    log_error("%s: %s", msg, SDL_GetError());
}

struct DoomState
{
    // So easy to get this wrong and destroy the texture by accident. Cleanup is
    // now done in check_on_dooms() before erasing the state. Enabling this code
    // still works though.
#if 0
    DoomState() = default;
    ~DoomState()
    {
        if (texture)
            SDL_DestroyTexture(texture);
    }
    DoomState(const DoomState &) = delete;
    DoomState &operator=(const DoomState &) = delete;

    DoomState(DoomState &&o)
        : DoomState()
    {
        *this = std::move(o);
    }

    DoomState &operator=(DoomState &&o)
    {
        std::swap(id, o.id);
        std::swap(state, o.state);
        std::swap(tLastActive, o.tLastActive);
        std::swap(texture, o.texture);
        return *this;
    }
#endif

    doomid_t id = 0;
    DP_DoomState state = DP_DS_Unknown;
    std::chrono::steady_clock::time_point tLastActive;
    SDL_Texture *texture = nullptr;
};

struct ControllerContext
{
    nng_socket pub;
    nng_socket sub;
    SDL_Window *window;
    SDL_Renderer *renderer;
    std::vector<DoomState> dooms;
    bool quit = false;
    ExampleAppLog appLog;
    int columns = 4;
    float scaleFactor = 1.0;
    s32 offsetX = 0;
    s32 offsetY = 0;
    bool isMousePanning = false;
    bool isFullscreen = false;
    bool uiVisible = true;
    bool publishInputsMode = false;
    MsgInputs inputs;
};

struct ControllerActions
{
    int doomsToSpawn = 0;
    bool endAllDooms = false;
};

#define DOOM_EXECUTABLE "dp_doom"

#if defined(__FREEBSD__)
extern char **environ;
char **__environ = environ;
#endif


#ifdef __WIN32
inline void spawn_doom(ControllerContext &ctx)
{
    (void) ctx;

    // Source: https://learn.microsoft.com/en-us/windows/win32/procthread/creating-processes
    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    ZeroMemory(&si, sizeof(si));
    si.cb = sizeof(si);
    ZeroMemory(&pi, sizeof(pi));

    char cmdLine[256];
    snprintf(cmdLine, sizeof(cmdLine), "%s -nosound", DOOM_EXECUTABLE);

    // Start the child process.
    if (!CreateProcess(NULL,    // No module name (use command line)
                       cmdLine, // Command line
                       NULL,    // Process handle not inheritable
                       NULL,    // Thread handle not inheritable
                       FALSE,   // Set handle inheritance to FALSE
                       0,       // No creation flags
                       NULL,    // Use parent's environment block
                       NULL,    // Use parent's starting directory
                       &si,     // Pointer to STARTUPINFO structure
                       &pi)     // Pointer to PROCESS_INFORMATION structure
    )
    {
        log_error("CreateProcess failed (%d)", GetLastError());
        return;
    }

    log_info("Spawned new %s, pid=%d", cmdLine, pi.dwProcessId);
}
#else
void spawn_doom_posix_spawn(ControllerContext &ctx)
{
    (void) ctx;
    const char *const argv[] = { DOOM_EXECUTABLE, nullptr };

    // TODO: Close stdin and stdout? Leave them open for now to see the logging
    // output.
    pid_t pid;
    if (auto err = posix_spawn(&pid, DOOM_EXECUTABLE, nullptr, nullptr,
        const_cast<char *const *>(argv), __environ))
    {
        log_error("Could not spawn %s: %s", DOOM_EXECUTABLE, strerror(err));
        return;
    }

    log_info("Spawned new %s, pid=%d", DOOM_EXECUTABLE, pid);
}

inline void spawn_doom(ControllerContext &ctx)
{
    spawn_doom_posix_spawn(ctx);
}
#endif

void end_all_dooms(ControllerContext &ctx)
{
    nng_msg *msg = nullptr;
    int res = 0;

    if ((res = nng_msg_alloc(&msg, sizeof(MsgMcstCommand))))
        dp_nng_fatal("ctrl/nng_msg_alloc", res);

    auto dpmsg = DP_NNG_BODY_AS(msg, MsgMcstCommand);
    dpmsg->head.msgType = DP_MT_McstCommand;
    dpmsg->cmd = DP_DC_Endoom;

    if ((res = nng_sendmsg(ctx.pub, msg, 0)))
        dp_nng_fatal("ctrl/sendmsg", res);
}

void perform_actions(ControllerContext &ctx, const ControllerActions &actions)
{
    if (actions.doomsToSpawn)
    {
        log_info("Spawning %d new dooms", actions.doomsToSpawn);
        for (int i=0; i<actions.doomsToSpawn; ++i)
            spawn_doom(ctx);
    }

    if (actions.endAllDooms)
    {
        log_info("Telling all dooms to quit");
        end_all_dooms(ctx);
    }
}

inline auto dp_now() { return std::chrono::steady_clock::now(); }
inline auto dp_elapsed(const std::chrono::steady_clock::time_point &tStart) { return dp_now() - tStart; }

void check_on_dooms(ControllerContext &ctx)
{
#ifndef __WIN32
    pid_t pid = 0;

    // Detect dooms that have terminated. This works for dooms forked from the
    // controller, not for externally started dooms.
    do
    {
        int wstatus = 0;

        if (pid = waitpid(0, &wstatus, WNOHANG); pid > 0)
        {
            if (auto ds = find_in_container(ctx.dooms, [pid] (const auto &ds) { return ds.id == pid; });
                ds != std::end(ctx.dooms))
            {
                if (WIFEXITED(wstatus))
                    log_info("doom(%d) exited with status %d", pid, WEXITSTATUS(wstatus));
                else if (WIFSIGNALED(wstatus))
                    log_warn("doom#(%d) got killed by signal %d", pid, WTERMSIG(wstatus));

                // Manually set Endoom state and let the code below clean it up.
                ds->state = DP_DS_Endoom;
            }
        }
    } while (pid > 0);
#else
//#warning "pid checking not implemented for Windows"
#endif

    // Find dooms that are in Endoom state and remove them. This works for
    // externally started dooms if we received their Endoom DoomState update.
    {
        const auto prevCount = ctx.dooms.size();
        const auto removedBegin = std::remove_if(std::begin(ctx.dooms), std::end(ctx.dooms), [](const auto &ds)
                                                 { return ds.state == DP_DS_Endoom; });
        if (removedBegin != std::end(ctx.dooms))
        {
            auto count = std::distance(removedBegin, std::end(ctx.dooms));
            std::for_each(removedBegin, std::end(ctx.dooms), [](auto &ds)
                          { SDL_DestroyTexture(ds.texture); ds.texture = nullptr; });
            ctx.dooms.erase(removedBegin, std::end(ctx.dooms));
            const auto newCount = ctx.dooms.size();
            log_info("Erased %zu dooms which were in Endoom state. Doomcount before=%zu, after=%zu", count, prevCount, newCount);
        }
    }

    // FIXME: We can miss Endoom state updates when nng has to drop messages due
    // to queue size limits. If this happens for an externally started doom it
    // will never be removed from ctx.dooms (waitpid() does not work because the
    // doom is not our child). Use DoomState::tLastActive and a fixed timeout
    // value to timeout dooms and erase them from ctx.dooms.
}

//#define DP_DO_DEBUG_DRAWING
#ifdef DP_DO_DEBUG_DRAWING
#include "debug_draw.cc"
#endif

void do_networking(ControllerContext &ctx)
{
    // Set to true if we receive at least one DP_DS_Ready DoomState update. Then
    // a single DP_DC_RunDoom command is broadcast.
    bool sendRunDoom = false;

    // Limit the max time we spend doing network stuff.
    static const auto MaxNetworkingTime = std::chrono::milliseconds(10);
    auto tStart = dp_now();

    while (true)
    {
        if (auto elapsed = dp_elapsed(tStart); elapsed >= MaxNetworkingTime)
            break;

        nng_msg *msg = nullptr;

        if (auto res = dp_recv_new_msg_nonblock(ctx.sub, &msg))
        {
            if (!dp_nng_is_timeout(res))
                dp_nng_fatal("ctrl/recvmsg", res);
            break; // timeout
        }

        auto msgBase = DP_NNG_BODY_AS(msg, MessageBase);

        if (msgBase->msgType == DP_MT_DoomState)
        {
            auto msgDoomState = DP_NNG_BODY_AS(msg, MsgDoomState);

            // Check if we know this doom and register it if we don't.
            auto pid = msgDoomState->doomId;
            auto dit = find_in_container(ctx.dooms, [pid] (const auto &ds) { return ds.id == pid; });

            if (dit != std::end(ctx.dooms))
            {
                dit->state = msgDoomState->doomState;
                dit->tLastActive = dp_now();
            }
            else
            {
                DoomState ds;
                ds.id = pid;
                ds.state = msgDoomState->doomState;
                ds.tLastActive = dp_now();
                ds.texture = SDL_CreateTexture(
                    ctx.renderer, DoomSdlTexturePixelFormat,
                    SDL_TEXTUREACCESS_STREAMING, DoomScreenWidth, DoomScreenHeight);

                if (!ds.texture)
                    dp_sdl_fatal("SDL_CreateTexture");

                #if 0
                {
                    u32 format = 0;
                    int access = 0;
                    int w = 0;
                    int h = 0;
                    if (SDL_QueryTexture(ds.texture, &format, &access, &w, &h))
                        dp_sdl_fatal("SDL_QueryTexture");

                    log_debug("texture info (wanted, got): format=(%u, %u), access=(%d, %d), w=(%d, %d), h=(%d, %d)",
                        DoomSdlTexturePixelFormat, format,
                        SDL_TEXTUREACCESS_STREAMING, access,
                        DoomScreenWidth, w,
                        DoomScreenHeight, h
                    );

                    assert(format == DoomSdlTexturePixelFormat);
                    assert(access == SDL_TEXTUREACCESS_STREAMING);
                    assert(w == DoomScreenWidth);
                    assert(h == DoomScreenHeight);

                }
                #endif

                log_info("Registered new doom (pid=%d)", ds.id);
                ctx.dooms.emplace_back(std::move(ds));
            }

            if (msgDoomState->doomState == DP_DS_Ready)
                sendRunDoom = true;
        }
        else if (msgBase->msgType == DP_MT_DoomFrame)
        {
            auto msgDoomFrame = DP_NNG_BODY_AS(msg, MsgDoomFrame);
            auto pid = msgDoomFrame->doomId;
            auto dit = find_in_container(ctx.dooms, [pid] (const auto &ds) { return ds.id == pid; });

            if (dit != std::end(ctx.dooms))
            {
                auto &ds = *dit;
                ds.tLastActive = dp_now();

                u8 *sourcePixels = msgDoomFrame->frame;

                #ifdef DP_DO_DEBUG_DRAWING
                OffscreenBuffer buffer =
                {
                    DoomScreenWidth,
                    DoomScreenHeight,
                    sourcePixels,
                    DoomScreenWidth * DoomBytesPerPixel,
                    DoomBytesPerPixel
                };

                DrawRectangle(&buffer, 0, 0, 10, 10, { 1, 0, 0, 1 });
                DrawRectangle(&buffer, 0, buffer.height-10, 10, buffer.height, { 0, 1, 0, 1 });
                DrawRectangle(&buffer, buffer.width-10, buffer.height-10, buffer.width, buffer.height, { 0, 0, 1, 1 });
                DrawRectangle(&buffer, buffer.width-10, 0, buffer.width, 10, { 0.840, 0.0168, 0.717, 1 });
                #endif


                if (SDL_UpdateTexture(ds.texture, nullptr, sourcePixels, DoomFramePitch))
                    dp_sdl_fatal("SDL_UpdateTexture");
            }
            else
            {
                // FIXME: this case happens if too many dooms are spawned at
                // once: we miss some of their DoomState DP_DS_Ready messages
                // and thus never register them. The dooms do however see our
                // DP_DC_RunDoom commands so they start running.
                log_trace("Received DoomFrame from unregistered doom (pid=%d)", pid);
            }
        }

        nng_msg_free(msg);
    }

    if (sendRunDoom)
    {
        nng_msg *msg = nullptr;
        int res = 0;

        if ((res = nng_msg_alloc(&msg, sizeof(MsgMcstCommand))))
            dp_nng_fatal("ctrl/nng_msg_alloc", res);

        auto dpmsg = DP_NNG_BODY_AS(msg, MsgMcstCommand);
        dpmsg->head.msgType = DP_MT_McstCommand;
        dpmsg->cmd = DP_DC_RunDoom;

        if ((res = nng_sendmsg(ctx.pub, msg, 0)))
            dp_nng_fatal("ctrl/sendmsg", res);
    }

    if (ctx.publishInputsMode && ctx.inputs.eventCount)
    {
        nng_msg *msg = nullptr;
        int res = 0;
        if ((res = nng_msg_alloc(&msg, sizeof(MsgInputs))))
            dp_nng_fatal("ctrl/nng_msg_alloc", res);

        auto dpmsg = DP_NNG_BODY_AS(msg, MsgInputs);
        *dpmsg = ctx.inputs;
        ctx.inputs.eventCount = 0;

        if ((res = nng_sendmsg(ctx.pub, msg, 0)))
            dp_nng_fatal("ctrl/sendmsg", res);
    }
}

void final_cleanup(ControllerContext &ctx)
{
    log_debug("final cleanup: ending all dooms");
    end_all_dooms(ctx);
    std::this_thread::sleep_for(std::chrono::milliseconds(50));
    check_on_dooms(ctx);

    if (!ctx.dooms.empty())
    {
        log_warn("final cleanup: %zu dooms remain", ctx.dooms.size());
    }
}

// Same as D_PostEvent from d_main.c
void post_doom_event(ControllerContext &ctx, const dp_doom_event_t &doom_event)
{
    ctx.inputs.events[ctx.inputs.eventCount] = doom_event;
    ctx.inputs.eventCount = (ctx.inputs.eventCount+1) & (DoomMaxEvents-1);
}

void render_dooms(ControllerContext &ctx)
{
    SDL_Rect destRect = { ctx.offsetX, ctx.offsetY, DoomScreenWidth, DoomScreenHeight };

    destRect.w *= ctx.scaleFactor;
    destRect.h *= ctx.scaleFactor;

    const size_t doomCount = ctx.dooms.size();

    for (size_t i=0; i<doomCount; ++i)
    {
        if (i != 0 && i % ctx.columns == 0)
        {
            destRect.x = ctx.offsetX;
            destRect.y += destRect.h;
        }

        auto &ds = ctx.dooms.at(i);
        auto texture = ds.texture;
        SDL_RenderCopy(ctx.renderer, texture, nullptr, &destRect);
        destRect.x += destRect.w;
    }
}

ControllerActions run_ui(ControllerContext &ctx)
{
#ifndef IMGUI_DISABLE_DEBUG_TOOLS
    const bool has_debug_tools = true;
#else
    const bool has_debug_tools = false;
#endif
    static bool show_app_metrics = false;
    static bool show_app_debug_log = false;
    static bool show_app_about = false;

    static bool show_log_window = true;
    static bool show_userguide = false;

    if (show_app_metrics)
        ImGui::ShowMetricsWindow(&show_app_metrics);
    if (show_app_debug_log)
        ImGui::ShowDebugLogWindow(&show_app_debug_log);
    if (show_app_about)
        ImGui::ShowAboutWindow(&show_app_about);

    const ImGuiViewport* main_viewport = ImGui::GetMainViewport();

    if (show_log_window)
    {
        ImGui::SetNextWindowPos(ImVec2(main_viewport->WorkPos.x + 666, main_viewport->WorkPos.y + 20), ImGuiCond_FirstUseEver);
        ImGui::SetNextWindowSize(ImVec2(500, 400), ImGuiCond_FirstUseEver);
        ctx.appLog.Draw("log");
    }

    if (show_userguide)
    {
        ImGui::SetNextWindowPos(ImVec2(main_viewport->WorkPos.x + 666, main_viewport->WorkPos.y + 420), ImGuiCond_FirstUseEver);
        ImGui::SetNextWindowSize(ImVec2(500, 360), ImGuiCond_FirstUseEver);
        if (ImGui::Begin("Dear ImGui User Guide", &show_userguide))
        {
            ImGui::ShowUserGuide();
            ImGui::End();
        }
    }

    ImGui::SetNextWindowPos(ImVec2(main_viewport->WorkPos.x + 20, main_viewport->WorkPos.y + 20), ImGuiCond_FirstUseEver);
    ImGui::SetNextWindowSize(ImVec2(420, 340), ImGuiCond_FirstUseEver);
    ImGuiWindowFlags window_flags = ImGuiWindowFlags_MenuBar;

    static std::array<char, 1024> strbuf;
    auto &io = ImGui::GetIO();

    aprintf(strbuf, "doompanning - #dooms=%zu, %.2f ms/frame (%.1f fps)###doompanning",
        ctx.dooms.size(), 1000.0f / io.Framerate, io.Framerate);

    // Main body of the doompanning window starts here.
    if (!ImGui::Begin(strbuf.data(), nullptr, window_flags))
    {
        // Early out if the window is collapsed, as an optimization.
        ImGui::End();
        return {};
    }

    // Menu Bar
    if (ImGui::BeginMenuBar())
    {
        if (ImGui::BeginMenu("Menu"))
        {
            ImGui::MenuItem("Send inputs to dooms", "Ctrl-A", &ctx.publishInputsMode, true);
            ImGui::Separator();
            ImGui::MenuItem("Quit", "Ctrl+Q", &ctx.quit, true);
            ImGui::EndMenu();
        }

        if (ImGui::BeginMenu("Tools"))
        {
            ImGui::MenuItem("Log Window", nullptr, &show_log_window);
            ImGui::MenuItem("Dear ImGui Metrics/Debugger", NULL, &show_app_metrics, has_debug_tools);
            ImGui::MenuItem("Dear ImGui Debug Log", NULL, &show_app_debug_log, has_debug_tools);
            ImGui::EndMenu();
        }

        if (ImGui::BeginMenu("Help"))
        {
            ImGui::MenuItem("Dear ImGui User Guide", NULL, &show_userguide);
            ImGui::MenuItem("About Dear ImGui", NULL, &show_app_about);
            ImGui::EndMenu();
        }

        ImGui::EndMenuBar();
    }

    // Window contents

    ControllerActions result = {};
    static int doomsToSpawn = 4;

    ImGui::PushItemWidth(ImGui::GetFontSize() * -16); // affects stuff like slider widths

    ImGui::SliderInt("Layout columns##columns", &ctx.columns, 1, 32, "%d", ImGuiSliderFlags_AlwaysClamp);
    ImGui::SliderFloat("Doom scale", &ctx.scaleFactor, 0.1, 10, "%.3f", ImGuiSliderFlags_AlwaysClamp);

    ImGui::SliderInt("##dooms", &doomsToSpawn, 1, 256, "%d", ImGuiSliderFlags_AlwaysClamp | ImGuiSliderFlags_Logarithmic);
    aprintf(strbuf, "Spawn %d more doom%s###spawnmore", doomsToSpawn, doomsToSpawn > 1 ? "s" : "");
    if (ImGui::SameLine(); ImGui::Button(strbuf.data()))
        result.doomsToSpawn = doomsToSpawn;

    if (ImGui::Button("End all dooms"))
        result.endAllDooms = true;


    ImGui::PopItemWidth();
    ImGui::End();

    return result;
}

int doom_controller_loop(ControllerContext &ctx)
{
    static constexpr ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);

    ControllerActions actions = {};

    while (!ctx.quit)
    {
        SDL_Event event;
        auto &io = ImGui::GetIO();
        s32 mouseWheel = 0.0;

        static const float ScaleStep = 0.01f;
        static const s32 PanStep = 2;

        while (SDL_PollEvent(&event))
        {
            ImGui_ImplSDL2_ProcessEvent(&event);

            if (event.type == SDL_QUIT)
                ctx.quit = true;

            if (event.type == SDL_WINDOWEVENT
                && event.window.event == SDL_WINDOWEVENT_CLOSE
                && event.window.windowID == SDL_GetWindowID(ctx.window))
            {
                ctx.quit = true;
            }

            if (event.type == SDL_MOUSEWHEEL)
                mouseWheel += event.wheel.y;

            if (ctx.publishInputsMode)
            {
                if (auto doomEvent = doomevent_from_sdlevent(event))
                    post_doom_event(ctx, *doomEvent);
            }

            if (!io.WantCaptureKeyboard && (event.type == SDL_KEYDOWN))
            {
                // FIXME: it doesn't work like this. use buttonstate and update it in here, then handle everything later like was done with imgui io
                const auto sym = event.key.keysym.sym;
                const auto mod = event.key.keysym.mod;
                const auto repeat = event.key.repeat;

                // Ctrl-Q to quit
                if (mod & KMOD_CTRL && sym == SDLK_q)
                    ctx.quit = true;

                // keyboard zoom with - and =, reset with 0
                if (sym == SDLK_EQUALS)
                    ctx.scaleFactor += ScaleStep;
                if (sym == SDLK_MINUS)
                    ctx.scaleFactor -= ScaleStep;
                if (sym == SDLK_0)
                    ctx.scaleFactor = 1.0;

                s32 PanFactor = mod & KMOD_CTRL ? 10 : 1;

                // hjkl scrolling and reset with g
                if (sym == SDLK_h)
                    ctx.offsetX -= PanStep;
                if (sym == SDLK_j)
                    ctx.offsetY += PanStep;
                if (sym == SDLK_k)
                    ctx.offsetY -= PanStep;
                if (sym == SDLK_l)
                    ctx.offsetX += PanStep;
                if (sym == SDLK_g)
                    ctx.offsetX = ctx.offsetY = 0;

                // Alt-Enter fullscreen toggle
                if (mod == KMOD_ALT && sym == SDLK_RETURN && !repeat)
                {
                    u32 flag = (ctx.isFullscreen ? 0 : SDL_WINDOW_FULLSCREEN_DESKTOP);
                    SDL_SetWindowFullscreen(ctx.window, flag);
                    ctx.isFullscreen = !ctx.isFullscreen;
                }

                // TODO: F1 - help

                // F2 - toggle publish inputs to dooms
                if (sym == SDLK_F2 && !repeat)
                {
                    ctx.publishInputsMode = !ctx.publishInputsMode;
                    if (SDL_SetRelativeMouseMode(ctx.publishInputsMode ? SDL_TRUE : SDL_FALSE) < 0)
                        log_warn("SDL_SetRelativeMouseMode: %s", SDL_GetError());
                    io.SetAppAcceptingEvents(!ctx.publishInputsMode);
                }

                // F3 to toggle ImGui visibility (skips the call to run_ui() if true)
                if (sym == SDLK_F3 && !repeat)
                    ctx.uiVisible = !ctx.uiVisible;
            }
        }

        // Process input events not consumed by ImGui
        #if 0
        if (!io.WantCaptureKeyboard)
        {
            // TODO: make scaling scale with the current scale factor
            static const float ScaleStep = 0.01f;
            s32 PanStep = io.KeyCtrl ? 10 : 2;

            // Ctrl-Q to quit
            if (io.KeyCtrl && ImGui::IsKeyDown(ImGuiKey_Q))
                ctx.quit = true;

            // keyboard zoom with - and =, reset with 0
            if (ImGui::IsKeyDown(ImGuiKey_Equal))
                ctx.scaleFactor += ScaleStep;
            if (ImGui::IsKeyDown(ImGuiKey_Minus))
                ctx.scaleFactor -= ScaleStep;
            if (ImGui::IsKeyDown(ImGuiKey_0))
                ctx.scaleFactor = 1.0;

            // hjkl scrolling and reset with g
            if (ImGui::IsKeyDown(ImGuiKey_H))
                ctx.offsetX -= PanStep;
            if (ImGui::IsKeyDown(ImGuiKey_J))
                ctx.offsetY += PanStep;
            if (ImGui::IsKeyDown(ImGuiKey_K))
                ctx.offsetY -= PanStep;
            if (ImGui::IsKeyDown(ImGuiKey_L))
                ctx.offsetX += PanStep;
            if (ImGui::IsKeyDown(ImGuiKey_G))
                ctx.offsetX = ctx.offsetY = 0;

            // Alt-Enter fullscreen toggle
            if (io.KeyAlt && ImGui::IsKeyPressed(ImGuiKey_Enter, false))
            {
                u32 flag = (ctx.isFullscreen ? 0 : SDL_WINDOW_FULLSCREEN_DESKTOP);
                SDL_SetWindowFullscreen(ctx.window, flag);
                ctx.isFullscreen = !ctx.isFullscreen;
            }

            // TODO: F1 - help

            // F2 - toggle publish inputs to dooms
            if (ImGui::IsKeyPressed(ImGuiKey_F2, false))
            {
                ctx.publishInputsMode = !ctx.publishInputsMode;
                if (SDL_SetRelativeMouseMode(ctx.publishInputsMode ? SDL_TRUE : SDL_FALSE) < 0)
                    log_warn("SDL_SetRelativeMouseMode: %s", SDL_GetError());
            }

            // F3 to toggle ImGui visibility (skips the call to run_ui() if true)
            if (ImGui::IsKeyPressed(ImGuiKey_F3, false))
                ctx.uiVisible = !ctx.uiVisible;
        }
        #endif

        static ImVec2 panStartPos;

        if (!io.WantCaptureMouse)
        {
            ctx.scaleFactor += mouseWheel * 0.05;

            if (ImGui::IsKeyPressed(ImGuiKey_MouseLeft, false))
            {
                panStartPos = io.MousePos;
                ctx.isMousePanning = true;
            }
        }

        if (ImGui::IsKeyReleased(ImGuiKey_MouseLeft))
            ctx.isMousePanning = false;

        if (ctx.isMousePanning && !ctx.publishInputsMode)
        {
            auto curPos = io.MousePos;
            auto dx = curPos.x - panStartPos.x;
            auto dy = curPos.y - panStartPos.y;
            ctx.offsetX -= dx;
            ctx.offsetY -= dy;
            panStartPos = curPos;
        }

        ctx.scaleFactor = std::clamp(ctx.scaleFactor, 0.1f, 10.0f);

        perform_actions(ctx, actions);
        check_on_dooms(ctx);
        do_networking(ctx);

        // Start the Dear ImGui frame
        ImGui_ImplSDLRenderer_NewFrame();
        ImGui_ImplSDL2_NewFrame();
        ImGui::NewFrame();

        actions = {};
        if (ctx.uiVisible)
            actions = run_ui(ctx);

        // Rendering
        const auto [r, g, b, a] = imvec4_to_rgba(clear_color);
        SDL_SetRenderDrawColor(ctx.renderer, r, g, b, a);
        SDL_RenderClear(ctx.renderer);

        render_dooms(ctx);

        ImGui::Render();
        ImGui_ImplSDLRenderer_RenderDrawData(ImGui::GetDrawData());
        SDL_RenderPresent(ctx.renderer);
    }

    final_cleanup(ctx);

    return 0;
}

void log_to_imgui(log_Event *ev)
{
    auto ctx = reinterpret_cast<ControllerContext *>(ev->udata);
    ctx->appLog.AddLog(ev->fmt, ev->ap);
}

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

#ifndef NDEBUG
    log_set_level(LOG_TRACE);
#else
    log_set_level(LOG_DEBUG);
#endif

    log_info("doompanning ctrl starting");

    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER))
        dp_sdl_fatal("SDL_Init");

#ifdef SDL_HINT_IME_SHOW_UI
    SDL_SetHint(SDL_HINT_IME_SHOW_UI, "1");
#endif

    const auto windowFlags = SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_OPENGL;
    auto window = SDL_CreateWindow("doompanning", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 960, windowFlags);
    if (!window)
        dp_sdl_fatal("SDL_CreateWindow");

    auto renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED);

    if (!renderer)
        dp_sdl_fatal("SDL_CreateRenderer");

    IMGUI_CHECKVERSION();
    /*auto imgui = */ImGui::CreateContext();
    ImGui::GetIO().IniFilename = "doompanning_ui.ini";
    ImGui::StyleColorsDark();
    ImGui_ImplSDL2_InitForSDLRenderer(window, renderer);
    ImGui_ImplSDLRenderer_Init(renderer);

    //ImGuiSettingsHandler dpSettingsHandler;
    //imgui->SettingsHandlers.push_back(dpSettingsHandler);

    ControllerContext ctx;

    // ctrl pub socket: ctrl -> dooms
    if (int res = nng_pub0_open(&ctx.pub))
        dp_nng_fatal("ctrl/nng_pub0_open", res);

    if (int res = nng_listen(ctx.pub, CtrlUrlIpc, nullptr, 0))
        dp_nng_fatal("ctrl/nng_listen/ipc", res);

    if (int res = nng_listen(ctx.pub, CtrlUrlTcp, nullptr, 0))
        dp_nng_fatal("ctrl/nng_listen/tcp4", res);

    // ctrl sub socket: dooms -> ctrl
    if (int res = nng_sub0_open(&ctx.sub))
        dp_nng_fatal("ctrl/nng_sub0_open", res);

    if (int res = nng_setopt(ctx.sub, NNG_OPT_SUB_SUBSCRIBE, "", 0))
        dp_nng_fatal("ctrl/subscribe", res);

    if (int res = nng_socket_set_ms(ctx.sub, NNG_OPT_RECVTIMEO, 100))
        dp_nng_fatal("ctrl/recvtimeo", res);

    if (int res = nng_listen(ctx.sub, DoomUrlIpc, NULL, 0))
        dp_nng_fatal("make_ctrl_sub/nng_listen", res);

    if (int res = nng_listen(ctx.sub, DoomUrlTcp, nullptr, 0))
        dp_nng_fatal("ctrl/nng_listen/tcp4", res);

    ctx.window = window;
    ctx.renderer = renderer;
    ctx.inputs.head.msgType = DP_MT_Inputs;
    ctx.inputs.eventCount = 0;

    log_add_callback(log_to_imgui, &ctx, LOG_DEBUG);

    int ret = doom_controller_loop(ctx);

    nng_close(ctx.pub);
    nng_close(ctx.sub);

    return ret;
}
