/*
 * Copyright © Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 2 or 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef MIR_EVENT_BUILDERS_H_
#define MIR_EVENT_BUILDERS_H_

#include <mir_toolkit/event.h>

#include <mir/geometry/size.h>
#include <mir/geometry/point.h>
#include <mir/geometry/rectangle.h>
#include <mir/geometry/displacement.h>
#include <mir/frontend/surface_id.h>
#include <mir/events/input_device_state.h>
#include <mir/events/scroll_axis.h>
#include <mir/events/touch_contact.h>

#include <memory>
#include <chrono>
#include <vector>
#include <optional>

namespace mir
{
using EventUPtr = std::unique_ptr<MirEvent, void (*)(MirEvent*)>;

namespace events
{
// Surface orientation change event
EventUPtr make_surface_orientation_event(frontend::SurfaceId const& surface_id, MirOrientation orientation);
// Surface resize event
EventUPtr make_window_resize_event(frontend::SurfaceId const& surface_id, geometry::Size const& size);
// Window configure event
EventUPtr make_window_configure_event(frontend::SurfaceId const& surface_id, MirWindowAttrib attribute, int value);
// Close surface event
EventUPtr make_window_close_event(frontend::SurfaceId const& surface_id);

// Surface output event
EventUPtr make_window_output_event(
    frontend::SurfaceId const& surface_id,
    int dpi,
    float scale,
    double refresh_rate,
    MirFormFactor form_factor,
    uint32_t id);

/// Surface placement event
EventUPtr make_window_placement_event(frontend::SurfaceId const& surface_id, geometry::Rectangle placement);

// Key event
EventUPtr make_key_event(
    MirInputDeviceId device_id,
    std::chrono::nanoseconds timestamp,
    MirKeyboardAction action,
    xkb_keysym_t keysym,
    int scan_code,
    MirInputEventModifiers modifiers);

void set_modifier(MirEvent& event, MirInputEventModifiers modifiers);
void set_cursor_position(MirEvent& event, mir::geometry::Point const& pos);
void set_cursor_position(MirEvent& event, float x, float y);
void set_button_state(MirEvent& event, MirPointerButtons button_state);

void add_touch(
    MirEvent& event,
    MirTouchId touch_id,
    MirTouchAction action,
    MirTouchTooltype tooltype,
    float x_axis_value,
    float y_axis_value,
    float pressure_value,
    float touch_major_value,
    float touch_minor_value,
    float size_value);

// Pointer event with all properties, using Mir geometry types
EventUPtr make_pointer_event(
    MirInputDeviceId device_id,
    std::chrono::nanoseconds timestamp,
    MirInputEventModifiers mods,
    MirPointerAction action,
    MirPointerButtons buttons,
    std::optional<mir::geometry::PointF> position,
    mir::geometry::DisplacementF motion,
    MirPointerAxisSource axis_source,
    events::ScrollAxisH h_scroll,
    events::ScrollAxisV v_scroll);

// Pointer event
EventUPtr make_pointer_event(
    MirInputDeviceId device_id,
    std::chrono::nanoseconds timestamp,
    MirInputEventModifiers modifiers,
    MirPointerAction action,
    MirPointerButtons buttons_pressed,
    float x_axis_value,
    float y_axis_value,
    float hscroll_value,
    float vscroll_value,
    float relative_x_value,
    float relative_y_value);

// Input configuration event
EventUPtr make_input_configure_event(
    std::chrono::nanoseconds timestamp,
    MirPointerButtons pointer_buttons,
    MirInputEventModifiers modifiers,
    float x_axis_value,
    float y_axis_value,
    std::vector<InputDeviceState>&& device_states);

EventUPtr make_touch_event(
    MirInputDeviceId device_id,
    std::chrono::nanoseconds timestamp,
    MirInputEventModifiers modifiers,
    std::vector<TouchContact> const& contacts);

EventUPtr clone_event(MirEvent const& event);
void set_window_id(MirEvent& event, int window_id);
}
}

#endif // MIR_EVENT_BUILDERS_H_
