#define WLR_USE_UNSTABLE
#include <wlr/backend.h>
#include <wlr/backend/multi.h>
#include <wlr/types/wlr_output.h>
#include <wlr/types/wlr_output_layout.h>
#include <wlr/types/wlr_scene.h>
#include <wlr/types/wlr_compositor.h>
#include <wlr/types/wlr_cursor.h>
#include <wlr/types/wlr_data_device.h>
#include <wlr/render/allocator.h>
#include <wlr/render/wlr_renderer.h>
#include <wlr/util/log.h>
#include <wayland-server-core.h>
#include <stdlib.h>
#include <string.h>

struct server {
	struct wl_display *display;
	struct wlr_backend *backend;
	struct wlr_renderer *renderer;
	struct wlr_allocator *allocator;
	struct wlr_scene *scene;
	struct wlr_output_layout *layout;
	struct wl_listener new_output;
};

static void handle_new_output(struct wl_listener *listener, void *data) {
	struct server *server = wl_container_of(listener, server, new_output);
	struct wlr_output *output = data;

	// Pick the first available mode
	if (!wl_list_empty(&output->modes)) {
		struct wlr_output_mode *mode = wl_container_of(output->modes.next, mode, link);

		struct wlr_output_state state;
		memset(&state, 0, sizeof(state));
		state.committed = WLR_OUTPUT_STATE_ENABLED | WLR_OUTPUT_STATE_MODE;
		state.enabled = true;
		state.mode_type = WLR_OUTPUT_STATE_MODE_FIXED;
		state.mode = mode;

		if (!wlr_output_commit_state(output, &state)) {
			wlr_log(WLR_ERROR, "Failed to commit output state");
			return;
		}
	}

	wlr_output_layout_add_auto(server->layout, output);
	wlr_scene_output_create(server->scene, output);

	float bg[4] = {0.1f, 0.1f, 0.1f, 1.0f};
	struct wlr_scene_rect *rect =
		wlr_scene_rect_create(&server->scene->tree, 1, 1, bg);
	wlr_scene_node_lower_to_bottom(&rect->node);
}

int main(void) {
    wlr_log_init(WLR_DEBUG, NULL);
    struct server server = {0};

    server.display = wl_display_create();
    struct wl_event_loop *loop = wl_display_get_event_loop(server.display);

    // Create the backend first
    server.backend = wlr_backend_autocreate(loop, NULL); // Automatically choose the backend
    if (!server.backend) {
        wlr_log(WLR_ERROR, "Failed to create backend");
        return 1;
    }

    // Create the renderer after the backend
    server.renderer = wlr_renderer_autocreate(server.backend);
    if (!server.renderer) {
        wlr_log(WLR_ERROR, "Failed to create renderer");
        return 1;
    }

    // Now create the allocator with the valid renderer
    server.allocator = wlr_allocator_autocreate(server.backend, server.renderer);
    if (!server.allocator) {
        wlr_log(WLR_ERROR, "Failed to create allocator");
        return 1;
    }

    // Create the compositor with the renderer
    wlr_compositor_create(server.display, 4, server.renderer);
    wlr_data_device_manager_create(server.display);

    server.scene = wlr_scene_create();
    server.layout = wlr_output_layout_create(server.display);

    server.new_output.notify = handle_new_output;
    wl_signal_add(&server.backend->events.new_output, &server.new_output);

    // Start the backend
    if (!wlr_backend_start(server.backend)) {
        wlr_log(WLR_ERROR, "Failed to start backend");
        return 1;
    }

    wlr_log(WLR_INFO, "Panes compositor running (wlroots 0.18)...");
    wl_display_run(server.display);
    wl_display_destroy(server.display);
    return 0;
}