#include <string.h>
#include <assert.h>
#include <libinput.h>
#include <linux/input.h>
#include <stdlib.h>
#include <wlr/interfaces/wlr_tablet_tool.h>
#include <wlr/util/log.h>
#include "backend/libinput.h"
#include "config.h"

struct tablet_tool {
	struct wlr_tablet_tool wlr_tool;
	struct libinput_tablet_tool *handle;
	struct wl_list link; // wlr_libinput_input_device.tablet_tools
};

const struct wlr_tablet_impl libinput_tablet_impl = {
	.name = "libinput-tablet-tool",
};

void init_device_tablet(struct wlr_libinput_input_device *dev) {
	const char *name = get_libinput_device_name(dev->handle);
	struct wlr_tablet *wlr_tablet = &dev->tablet;
	wlr_tablet_init(wlr_tablet, &libinput_tablet_impl, name);

#if HAVE_LIBINPUT_BUSTYPE
	if (libinput_device_get_id_bustype(dev->handle) == BUS_USB)
#endif
	{
		wlr_tablet->usb_vendor_id = libinput_device_get_id_vendor(dev->handle);
		wlr_tablet->usb_product_id = libinput_device_get_id_product(dev->handle);
	}

	libinput_device_get_size(dev->handle, &wlr_tablet->width_mm,
		&wlr_tablet->height_mm);

	struct udev_device *udev = libinput_device_get_udev_device(dev->handle);
	char **dst = wl_array_add(&wlr_tablet->paths, sizeof(char *));
	*dst = strdup(udev_device_get_syspath(udev));
	udev_device_unref(udev);

	wl_list_init(&dev->tablet_tools);
}

static void tool_destroy(struct tablet_tool *tool) {
	wl_signal_emit_mutable(&tool->wlr_tool.events.destroy, &tool->wlr_tool);
	libinput_tablet_tool_unref(tool->handle);
	libinput_tablet_tool_set_user_data(tool->handle, NULL);
	wl_list_remove(&tool->link);
	free(tool);
}

void finish_device_tablet(struct wlr_libinput_input_device *dev) {
	struct tablet_tool *tool, *tmp;
	wl_list_for_each_safe(tool, tmp, &dev->tablet_tools, link) {
		tool_destroy(tool);
	}

	wlr_tablet_finish(&dev->tablet);
}

struct wlr_libinput_input_device *device_from_tablet(
		struct wlr_tablet *wlr_tablet) {
	assert(wlr_tablet->impl == &libinput_tablet_impl);

	struct wlr_libinput_input_device *dev =
		wl_container_of(wlr_tablet, dev, tablet);
	return dev;
}

static bool type_from_libinput(enum libinput_tablet_tool_type type,
		enum wlr_tablet_tool_type *out) {
	switch (type) {
	case LIBINPUT_TABLET_TOOL_TYPE_PEN:
		*out = WLR_TABLET_TOOL_TYPE_PEN;
		return true;
	case LIBINPUT_TABLET_TOOL_TYPE_ERASER:
		*out = WLR_TABLET_TOOL_TYPE_ERASER;
		return true;
	case LIBINPUT_TABLET_TOOL_TYPE_BRUSH:
		*out = WLR_TABLET_TOOL_TYPE_BRUSH;
		return true;
	case LIBINPUT_TABLET_TOOL_TYPE_PENCIL:
		*out = WLR_TABLET_TOOL_TYPE_PENCIL;
		return true;
	case LIBINPUT_TABLET_TOOL_TYPE_AIRBRUSH:
		*out = WLR_TABLET_TOOL_TYPE_AIRBRUSH;
		return true;
	case LIBINPUT_TABLET_TOOL_TYPE_MOUSE:
		*out = WLR_TABLET_TOOL_TYPE_MOUSE;
		return true;
	case LIBINPUT_TABLET_TOOL_TYPE_LENS:
		*out = WLR_TABLET_TOOL_TYPE_LENS;
		return true;
	case LIBINPUT_TABLET_TOOL_TYPE_TOTEM:
		*out = WLR_TABLET_TOOL_TYPE_TOTEM;
		return true;
	}
	return false;
}

static bool proximity_state_from_libinput(enum libinput_tablet_tool_proximity_state state,
		enum wlr_tablet_tool_proximity_state *out) {
	switch (state) {
	case LIBINPUT_TABLET_TOOL_PROXIMITY_STATE_OUT:
		*out = WLR_TABLET_TOOL_PROXIMITY_OUT;
		return true;
	case LIBINPUT_TABLET_TOOL_PROXIMITY_STATE_IN:
		*out = WLR_TABLET_TOOL_PROXIMITY_IN;
		return true;
	}
	return false;
}

static bool tip_state_from_libinput(enum libinput_tablet_tool_tip_state state,
		enum wlr_tablet_tool_tip_state *out) {
	switch (state) {
	case LIBINPUT_TABLET_TOOL_TIP_UP:
		*out = WLR_TABLET_TOOL_TIP_UP;
		return true;
	case LIBINPUT_TABLET_TOOL_TIP_DOWN:
		*out = WLR_TABLET_TOOL_TIP_DOWN;
		return true;
	}
	return false;
}

static struct tablet_tool *get_tablet_tool(
		struct wlr_libinput_input_device *dev,
		struct libinput_tablet_tool *libinput_tool) {
	struct tablet_tool *tool =
		libinput_tablet_tool_get_user_data(libinput_tool);
	if (tool) {
		return tool;
	}

	enum wlr_tablet_tool_type type;
	if (!type_from_libinput(libinput_tablet_tool_get_type(libinput_tool), &type)) {
		wlr_log(WLR_DEBUG, "Unhandled libinput tablet tool type");
		return NULL;
	}

	tool = calloc(1, sizeof(*tool));
	if (tool == NULL) {
		wlr_log_errno(WLR_ERROR, "failed to allocate wlr_libinput_tablet_tool");
		return NULL;
	}

	tool->wlr_tool.type = type;
	tool->wlr_tool.hardware_serial =
		libinput_tablet_tool_get_serial(libinput_tool);
	tool->wlr_tool.hardware_wacom =
		libinput_tablet_tool_get_tool_id(libinput_tool);

	tool->wlr_tool.pressure = libinput_tablet_tool_has_pressure(libinput_tool);
	tool->wlr_tool.distance = libinput_tablet_tool_has_distance(libinput_tool);
	tool->wlr_tool.tilt = libinput_tablet_tool_has_tilt(libinput_tool);
	tool->wlr_tool.rotation = libinput_tablet_tool_has_rotation(libinput_tool);
	tool->wlr_tool.slider = libinput_tablet_tool_has_slider(libinput_tool);
	tool->wlr_tool.wheel = libinput_tablet_tool_has_wheel(libinput_tool);

	wl_signal_init(&tool->wlr_tool.events.destroy);

	tool->handle = libinput_tablet_tool_ref(libinput_tool);
	libinput_tablet_tool_set_user_data(libinput_tool, tool);

	wl_list_insert(&dev->tablet_tools, &tool->link);
	return tool;
}

void handle_tablet_tool_axis(struct libinput_event *event,
		struct wlr_tablet *wlr_tablet) {
	struct libinput_event_tablet_tool *tevent =
		libinput_event_get_tablet_tool_event(event);
	struct wlr_libinput_input_device *dev = device_from_tablet(wlr_tablet);
	struct tablet_tool *tool =
		get_tablet_tool(dev, libinput_event_tablet_tool_get_tool(tevent));

	struct wlr_tablet_tool_axis_event wlr_event = {
		.tablet = wlr_tablet,
		.tool = &tool->wlr_tool,
		.time_msec = usec_to_msec(libinput_event_tablet_tool_get_time_usec(tevent)),
	};
	if (libinput_event_tablet_tool_x_has_changed(tevent)) {
		wlr_event.updated_axes |= WLR_TABLET_TOOL_AXIS_X;
		wlr_event.x = libinput_event_tablet_tool_get_x_transformed(tevent, 1);
		wlr_event.dx = libinput_event_tablet_tool_get_dx(tevent);
	}
	if (libinput_event_tablet_tool_y_has_changed(tevent)) {
		wlr_event.updated_axes |= WLR_TABLET_TOOL_AXIS_Y;
		wlr_event.y = libinput_event_tablet_tool_get_y_transformed(tevent, 1);
		wlr_event.dy = libinput_event_tablet_tool_get_dy(tevent);
	}
	if (libinput_event_tablet_tool_pressure_has_changed(tevent)) {
		wlr_event.updated_axes |= WLR_TABLET_TOOL_AXIS_PRESSURE;
		wlr_event.pressure = libinput_event_tablet_tool_get_pressure(tevent);
	}
	if (libinput_event_tablet_tool_distance_has_changed(tevent)) {
		wlr_event.updated_axes |= WLR_TABLET_TOOL_AXIS_DISTANCE;
		wlr_event.distance = libinput_event_tablet_tool_get_distance(tevent);
	}
	if (libinput_event_tablet_tool_tilt_x_has_changed(tevent)) {
		wlr_event.updated_axes |= WLR_TABLET_TOOL_AXIS_TILT_X;
		wlr_event.tilt_x = libinput_event_tablet_tool_get_tilt_x(tevent);
	}
	if (libinput_event_tablet_tool_tilt_y_has_changed(tevent)) {
		wlr_event.updated_axes |= WLR_TABLET_TOOL_AXIS_TILT_Y;
		wlr_event.tilt_y = libinput_event_tablet_tool_get_tilt_y(tevent);
	}
	if (libinput_event_tablet_tool_rotation_has_changed(tevent)) {
		wlr_event.updated_axes |= WLR_TABLET_TOOL_AXIS_ROTATION;
		wlr_event.rotation = libinput_event_tablet_tool_get_rotation(tevent);
	}
	if (libinput_event_tablet_tool_slider_has_changed(tevent)) {
		wlr_event.updated_axes |= WLR_TABLET_TOOL_AXIS_SLIDER;
		wlr_event.slider = libinput_event_tablet_tool_get_slider_position(tevent);
	}
	if (libinput_event_tablet_tool_wheel_has_changed(tevent)) {
		wlr_event.updated_axes |= WLR_TABLET_TOOL_AXIS_WHEEL;
		wlr_event.wheel_delta = libinput_event_tablet_tool_get_wheel_delta(tevent);
	}
	wl_signal_emit_mutable(&wlr_tablet->events.axis, &wlr_event);
}

void handle_tablet_tool_proximity(struct libinput_event *event,
		struct wlr_tablet *wlr_tablet) {
	struct libinput_event_tablet_tool *tevent =
		libinput_event_get_tablet_tool_event(event);
	struct wlr_libinput_input_device *dev = device_from_tablet(wlr_tablet);
	struct tablet_tool *tool =
		get_tablet_tool(dev, libinput_event_tablet_tool_get_tool(tevent));

	struct wlr_tablet_tool_proximity_event wlr_event = {
		.tablet = wlr_tablet,
		.tool = &tool->wlr_tool,
		.time_msec = usec_to_msec(libinput_event_tablet_tool_get_time_usec(tevent)),
		.x = libinput_event_tablet_tool_get_x_transformed(tevent, 1),
		.y = libinput_event_tablet_tool_get_y_transformed(tevent, 1),
	};

	if (!proximity_state_from_libinput(libinput_event_tablet_tool_get_proximity_state(tevent),
			&wlr_event.state)) {
		wlr_log(WLR_DEBUG, "Unhandled libinput tablet tool proximity state");
		return;
	}

	wl_signal_emit_mutable(&wlr_tablet->events.proximity, &wlr_event);

	if (libinput_event_tablet_tool_get_proximity_state(tevent) ==
			LIBINPUT_TABLET_TOOL_PROXIMITY_STATE_IN) {
		handle_tablet_tool_axis(event, wlr_tablet);
	}

	// If the tool is not unique, libinput will not find it again after the
	// proximity out, so we should destroy it
	if (!libinput_tablet_tool_is_unique(tool->handle)
			&& libinput_event_tablet_tool_get_proximity_state(tevent) ==
			LIBINPUT_TABLET_TOOL_PROXIMITY_STATE_OUT) {
		// The tool isn't unique, it can't be on multiple tablets
		tool_destroy(tool);
	}
}

void handle_tablet_tool_tip(struct libinput_event *event,
		struct wlr_tablet *wlr_tablet) {
	handle_tablet_tool_axis(event, wlr_tablet);
	struct libinput_event_tablet_tool *tevent =
		libinput_event_get_tablet_tool_event(event);
	struct wlr_libinput_input_device *dev = device_from_tablet(wlr_tablet);
	struct tablet_tool *tool =
		get_tablet_tool(dev, libinput_event_tablet_tool_get_tool(tevent));

	struct wlr_tablet_tool_tip_event wlr_event = {
		.tablet = wlr_tablet,
		.tool = &tool->wlr_tool,
		.time_msec = usec_to_msec(libinput_event_tablet_tool_get_time_usec(tevent)),
		.x = libinput_event_tablet_tool_get_x_transformed(tevent, 1),
		.y = libinput_event_tablet_tool_get_y_transformed(tevent, 1),
	};

	if (!tip_state_from_libinput(libinput_event_tablet_tool_get_tip_state(tevent), &wlr_event.state)) {
		wlr_log(WLR_DEBUG, "Unhandled libinput tablet tool tip state");
		return;
	}

	wl_signal_emit_mutable(&wlr_tablet->events.tip, &wlr_event);
}

void handle_tablet_tool_button(struct libinput_event *event,
		struct wlr_tablet *wlr_tablet) {
	handle_tablet_tool_axis(event, wlr_tablet);
	struct libinput_event_tablet_tool *tevent =
		libinput_event_get_tablet_tool_event(event);
	struct wlr_libinput_input_device *dev = device_from_tablet(wlr_tablet);
	struct tablet_tool *tool =
		get_tablet_tool(dev, libinput_event_tablet_tool_get_tool(tevent));

	struct wlr_tablet_tool_button_event wlr_event = {
		.tablet = wlr_tablet,
		.tool = &tool->wlr_tool,
		.time_msec = usec_to_msec(libinput_event_tablet_tool_get_time_usec(tevent)),
		.button = libinput_event_tablet_tool_get_button(tevent),
	};

	if (!button_state_from_libinput(libinput_event_tablet_tool_get_button_state(tevent), &wlr_event.state)) {
		wlr_log(WLR_DEBUG, "Unhandled libinput button state");
		return;
	}

	wl_signal_emit_mutable(&wlr_tablet->events.button, &wlr_event);
}
