#ifndef SWIFT_WIDGET_H
#define SWIFT_WIDGET_H
#include <swift/stdlib.h>
#include <swift/window.h>
#include <swift/data.h>
#include <swift/maths.h>
#include <swift/gui/api.h>

typedef struct swift_widget_flags_t swift_widget_flags_t;
typedef struct swift_widget_t swift_widget_t;
typedef struct swift_widgets_t swift_widgets_t;

typedef void(*swift_control_draw)(swift_widgets_t*, swift_widget_t*);
typedef void(*swift_control_mouse_over)(swift_widgets_t*, swift_widget_t*);
typedef void(*swift_control_mouse_exit)(swift_widgets_t*, swift_widget_t*);
typedef void(*swift_control_mouse_down)(swift_widgets_t*, swift_widget_t*, int,
	int);
typedef void(*swift_control_mouse_up)(swift_widgets_t*, swift_widget_t*,int,
	int);
typedef void(*swift_control_mouse_wheel)(swift_widgets_t*, swift_widget_t*,
	int);
typedef void(*swift_control_update_pos)(swift_widgets_t*, swift_widget_t*);
typedef void(*swift_control_key_down)(swift_widgets_t*, swift_widget_t*, int,
	int);
typedef void(*swift_control_unload)(swift_widgets_t*, swift_widget_t*);


struct swift_widget_flags_t {
	bool is_focused :1;
	bool can_focus :1;
	bool mouse_over :1;
	bool clicked :1;
	bool moveable :1;
	bool moving :1;
	bool can_click_behind :1;
	bool always_useable :1;
	bool minimized :1;
	bool checked :1;
	bool can_use_event :1;
	bool focus_click :1;
	bool is_password :1;
	bool is_multi_lined :1;
	bool window_moveable :1;
};

struct swift_widget_t {
	swift_widget_t *parent;
	swift_list_t shown, snode, hidden, hnode;
	swift_vec2f_t pos;
	swift_sprite_t sprite;
	swift_widget_flags_t action;
	uint8_t type;
	size_t value;
	void *control, *data;
	swift_control_draw draw;
	swift_control_mouse_over mouse_over;
	swift_control_mouse_exit mouse_exit;
	swift_control_mouse_down mouse_down;
	swift_control_mouse_up mouse_up;
	swift_control_mouse_wheel mouse_wheel;
	swift_control_update_pos update_pos;
	swift_control_key_down key_down;
	swift_control_unload unload;
	swift_control_draw control_draw;
	swift_control_mouse_down control_mouse_down;
	swift_control_mouse_up control_mouse_up;
	swift_control_mouse_wheel control_mouse_wheel;
	swift_control_key_down control_key_down;
	swift_control_update_pos control_update_pos;
	swift_control_mouse_over control_mouse_over;
	swift_control_mouse_exit control_mouse_exit;
	swift_control_unload control_unload;
};

struct swift_widgets_t {
	swift_window_t *window;
	swift_widget_t root, *focused, *over_widget;
	swift_vec2f_t mouse_clicked, mouse_pos, new_mouse_pos;
	int button;
	bool moving :1, clicked :1;
	void *user_data, *callbacks;
};

#define SWIFT_WIDGET(...) ((swift_widget_t){__VA_ARGS__})
#define SWIFT_WIDGET_FLAGS(...) ((swift_widget_flags_t){__VA_ARGS__})
#define SWIFT_WIDGETs(...) ((swift_widgets_t){__VA_ARGS__})

SWIFT_GUI_API void swift_widget_move(swift_widgets_t *wgts, float x, float y);
SWIFT_GUI_API bool swift_widget_check_focus(swift_widgets_t *wgts);
SWIFT_GUI_API bool swift_init_widgets(swift_window_t *window,
	swift_widgets_t *wgts, void *user_data);
SWIFT_GUI_API void swift_widget_hide(swift_widget_t *control);
SWIFT_GUI_API void swift_widget_show(swift_widget_t *control);
SWIFT_GUI_API void swift_widget_add(swift_widget_t *parent,
	swift_widget_t *child);
SWIFT_GUI_API void swift_widget_add_hidden(swift_widget_t *parent,
	swift_widget_t *child);
SWIFT_GUI_API void swift_init_widget(swift_widget_t *control);
SWIFT_GUI_API void swift_unload_widgets(swift_widgets_t *wgts);
SWIFT_GUI_API void swift_unload_widgets_from(swift_widgets_t *wgts,
	swift_widget_t *parent);
SWIFT_GUI_API void swift_widget_unload(swift_widgets_t *wgts,
	swift_widget_t *parent);
SWIFT_GUI_API void swift_widgets_render(swift_widgets_t *wgts,
	swift_widget_t *parent);
SWIFT_GUI_API bool swift_widget_rect_contains(swift_widgets_t *wgts,
	swift_widget_t *control);
SWIFT_GUI_API bool swift_widget_has_mouse_over(swift_widgets_t *wgts,
	swift_widget_t *control);
SWIFT_GUI_API bool swift_widget_usable(swift_widget_t *control);
SWIFT_GUI_API void swift_widget_manual_focused(swift_widgets_t *wgts,
	swift_widget_t *control);
SWIFT_GUI_API void swift_widget_mouse_release(swift_widgets_t *wgts,
	swift_widget_t *control);
SWIFT_GUI_API void swift_widget_mouse_press(swift_widgets_t *wgts,
	swift_widget_t *control);
SWIFT_GUI_API void swift_set_gui_user_data(swift_widgets_t *wgts, void *data);
SWIFT_GUI_API void *swift_get_gui_user_data(swift_widgets_t *wgts);
#endif
