#include "pch.h"

#include "platform_windows/windows_lifecycle_shell.h"

#include "legacy_preference_storage.h"
#include "platform_windows/windows_lifecycle_state.h"
#include "platform_windows/windows_main_window_session.h"
#include "platform_windows/windows_stylus_input.h"

namespace pp::platform::windows {

void request_window_close(HWND hWnd)
{
    PostMessage(hWnd, kUserCloseMessage, 0, 0);
}

void handle_window_close_message(VrShellState& vr)
{
    shutdown_bound_lifecycle_and_quit(vr);
}

void update_stylus_frame(float dt)
{
    update_stylus_state(dt);
}

void update_window_fps(HWND hWnd, const wchar_t* window_title, VrShellState& vr, int frames)
{
    wchar_t title_fps[512]{};
    const int vr_fps = current_vr_fps(vr);
    if (read_vr_session_snapshot(vr).vr_active)
        swprintf_s(title_fps, 512, L"%s - %d fps - %d vr fps", window_title, frames, vr_fps);
    else
        swprintf_s(title_fps, 512, L"%s - %d fps", window_title, frames);

    SetWindowText(hWnd, title_fps);
    PostMessage(hWnd, kUserWakeupMessage, 0, 0);
}

void save_window_preferences(HWND hWnd)
{
    WINDOWPLACEMENT placement{};
    GetWindowPlacement(hWnd, &placement);
    pp::panopainter::set_legacy_window_preferences(placement.showCmd, {
        placement.rcNormalPosition.left,
        placement.rcNormalPosition.top,
        placement.rcNormalPosition.right,
        placement.rcNormalPosition.bottom });
}

bool start_window_vr(VrShellState& vr, bool sandboxed)
{
    return start_bound_lifecycle_vr(vr, sandboxed);
}

void stop_window_vr(VrShellState& vr)
{
    stop_bound_lifecycle_vr(vr);
}

}
