// ::Platform::Functions::Start::

b32 LoadLib(const char *name, Library *out_lib) 
{
	if (!name) {
		return false;
	}

	out_lib->lib = dlopen(name, RTLD_NOW);
	if (!out_lib->lib) {
		return false;
	}

	return true;
}

b32 LoadFn(const char *name, Library *lib, Function *out_fn) 
{
	if (!name) {
		return false;
	}

	out_fn->fn = dlsym(lib->lib, name);
	if (!out_fn->fn) {
		Printf("unable to find function\n");
		return false;
	}

	return true;
}



i32 EPrint(void const *str) 
{
	return Write(_STDERR, str, (isize)StrLen(str));
}

i32 _EPrintf(const char *fmt, va_list arg) 
{
	char buffer[1024];

	int sprf_res = stbsp_vsnprintf(&buffer[0], 1024, fmt, arg);

	if (sprf_res < 0) return sprf_res;
	
	return EPrint(&buffer);
}

i32 _Printf(const char *fmt, va_list arg) 
{
	char buffer[1024];
	
	int sprf_res = stbsp_vsnprintf(&buffer[0], 1024, fmt, arg);
	
	i32 pr_res;
	if (sprf_res < 0) 
		pr_res = sprf_res;
	else 
		pr_res = Write(_STDOUT, &buffer, sprf_res);

	return pr_res;
}

i32 _Printfln(const char *fmt, va_list arg)
{
	char buffer[1024];

	int sprf_res = stbsp_vsnprintf(&buffer[0], 1023, fmt, arg);

	i32 pr_res;
	if (sprf_res < 0)
	{
		pr_res = sprf_res;
	}
	else
	{
		buffer[sprf_res] = '\n';
		buffer[sprf_res+1] = '\0';
		pr_res = Write(_STDOUT, &buffer, sprf_res+1);
	}

	return pr_res;
}

rawptr MemAlloc(isize size) 
{
	void *addr = mmap(
		NULL, 
		size, 
		PROT_READ | PROT_WRITE, 
		MAP_ANON | MAP_PRIVATE,
		-1,
		0
	);

	if (CheckSyscallErr(addr)) addr = NULL;

	return addr;
}

rawptr MemAllocZeroed(isize size)
{
	rawptr ptr = MemAlloc(size);
	MemZero(ptr, size);
	return ptr;
}

void MemFree(rawptr ptr, isize size)
{
	Assert(munmap(ptr, size) == 0, "munmap failed");
}

isize GetPageSize()
{
	return (isize)sysconf(_SC_PAGESIZE);
}

b32 CreatePlatformWindow(const char *window_name) 
{
	PlatformWindow *window = &linux_window;

	window->display = XOpenDisplay(NULL);
	if (!window->display)
		return false;

	window->connection = XGetXCBConnection(window->display);

	if (!window->connection) 
		return false;

	xcb_void_cookie_t cookie;
	xcb_generic_error_t *error;

	const xcb_setup_t *setup = xcb_get_setup(window->connection);
	xcb_screen_iterator_t iter = xcb_setup_roots_iterator(setup);
	xcb_screen_t *screen = iter.data;

	const int event_mask = XCB_EVENT_MASK_EXPOSURE				|
												 XCB_EVENT_MASK_KEY_PRESS				|
												 XCB_EVENT_MASK_KEY_RELEASE			|
												 XCB_EVENT_MASK_BUTTON_PRESS		|
												 XCB_EVENT_MASK_BUTTON_RELEASE	|
												 XCB_EVENT_MASK_POINTER_MOTION	|
												 XCB_EVENT_MASK_STRUCTURE_NOTIFY;

	const int val_win[] = {screen->black_pixel, event_mask};
	const int val_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;

	window->window = xcb_generate_id(window->connection);

	cookie = xcb_create_window(
		window->connection,
		XCB_COPY_FROM_PARENT,
		window->window,
		screen->root,
		0, // x pos
		0, // y pos
		window->w, // width
		window->h, // height
		0,
		XCB_WINDOW_CLASS_INPUT_OUTPUT,
		screen->root_visual,
		val_mask,
		val_win
	);
	XCB_CHECK_ERROR(window, cookie, error, "Failed to create window.");

	cookie = xcb_map_window_checked(window->connection, window->window);
	XCB_CHECK_ERROR(window, cookie, error, "Failed to map window.");

	cookie = xcb_change_property_checked(
		window->connection,
		XCB_PROP_MODE_REPLACE,
		window->window,
		XCB_ATOM_WM_NAME,
		XCB_ATOM_STRING,
		8,
		StrLen(window_name),
		window_name
	);
	XCB_CHECK_ERROR(window, cookie, error, "Failed to rename window.");

	xcb_intern_atom_cookie_t c_proto = xcb_intern_atom(window->connection, 1, 12, "WM_PROTOCOLS");
	xcb_intern_atom_reply_t *r_proto = xcb_intern_atom_reply(window->connection, c_proto, &error);
	XCB_CHECK_CURRENT_ERROR(window, error, "Failed to get WM_PROTOCOLS.");

	xcb_intern_atom_cookie_t c_close = xcb_intern_atom(window->connection, 0, 16, "WM_DELETE_WINDOW");
	xcb_intern_atom_reply_t *r_close = xcb_intern_atom_reply(window->connection, c_close, &error);
	XCB_CHECK_CURRENT_ERROR(window, error, "Failed to get WM_DELETE_WINDOW.");

	xcb_intern_atom_cookie_t c_minimize = xcb_intern_atom(window->connection, 0, 20, "_NET_WM_STATE_HIDDEN");
	xcb_intern_atom_reply_t *r_minimize = xcb_intern_atom_reply(window->connection, c_minimize, &error);
	XCB_CHECK_CURRENT_ERROR(window, error, "Failed to get _NET_WM_STATE_HIDDEN");

	cookie = xcb_change_property_checked(
		window->connection,
		XCB_PROP_MODE_REPLACE,
		window->window,
		r_proto->atom,
		XCB_ATOM_ATOM,
		32,
		1,
		&r_close->atom
	);
	XCB_CHECK_ERROR(window, cookie, error, "Failed to set window close event.");

	window->close_event = r_close->atom;
	window->minimize_event = r_minimize->atom;

	free(r_proto);
	free(r_close);
	free(r_minimize);

	xcb_map_window(window->connection, window->window);

	i32 stream_result = xcb_flush(window->connection);
	if (stream_result <= 0)
	{
		Printfln("Error flushing the stream: %d", stream_result);
		return false;
	}

	return true;
}

PlatformWindow *GetPlatformWindow() 
{
	return &linux_window;
}
