/*
 * This source file is part of RmlUi, the HTML/CSS Interface Middleware
 * Modified for Mosis Designer to add input recording hooks.
 *
 * Original copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
 * Original copyright (c) 2019-2023 The RmlUi Team, and contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 */

#ifndef RMLUI_BACKENDS_BACKEND_H
#define RMLUI_BACKENDS_BACKEND_H

#include <RmlUi/Core/Input.h>
#include <RmlUi/Core/RenderInterface.h>
#include <RmlUi/Core/SystemInterface.h>
#include <RmlUi/Core/Types.h>
#include <functional>

using KeyDownCallback = bool (*)(Rml::Context* context, Rml::Input::KeyIdentifier key, int key_modifier, float native_dp_ratio, bool priority);

// Input recording callbacks (Mosis extension)
using MouseButtonCallback = std::function<void(int x, int y, int button, bool pressed)>;
using MouseMoveCallback = std::function<void(int x, int y)>;
using KeyCallback = std::function<void(int key, bool pressed)>;

/**
    This interface serves as a basic abstraction over the various backends included with RmlUi.
    Modified for Mosis Designer to add input recording hooks.
 */
namespace Backend {

// Initializes the backend, including the custom system and render interfaces, and opens a window for rendering the RmlUi context.
bool Initialize(const char* window_name, int width, int height, bool allow_resize);
// Closes the window and release all resources owned by the backend, including the system and render interfaces.
void Shutdown();

// Returns a pointer to the custom system interface which should be provided to RmlUi.
Rml::SystemInterface* GetSystemInterface();
// Returns a pointer to the custom render interface which should be provided to RmlUi.
Rml::RenderInterface* GetRenderInterface();

// Polls and processes events from the current platform, and applies any relevant events to the provided RmlUi context and the key down callback.
// @return False to indicate that the application should be closed.
bool ProcessEvents(Rml::Context* context, KeyDownCallback key_down_callback = nullptr, bool power_save = false);
// Request application closure during the next event processing call.
void RequestExit();

// Prepares the render state to accept rendering commands from RmlUi, call before rendering the RmlUi context.
void BeginFrame();
// Presents the rendered frame to the screen, call after rendering the RmlUi context.
void PresentFrame();

// --- Mosis Extension: Input Recording Hooks ---

// Set callback for mouse button events (called before RmlUi processes the event)
void SetMouseButtonCallback(MouseButtonCallback callback);

// Set callback for mouse move events (called before RmlUi processes the event)
void SetMouseMoveCallback(MouseMoveCallback callback);

// Set callback for keyboard events (called before RmlUi processes the event)
void SetKeyCallback(KeyCallback callback);

} // namespace Backend

#endif
