#import "JubGtkChatUI.h"
#import "JubGtkHelper.h"

struct call_send_block_params {
	jub_send_block_t block;
	JubGtkChatUI *chat;
};

static void call_send_block(GtkEntry *entry, gpointer data)
{
	struct call_send_block_params *params = data;

	OFString *text =
	    [[OFString alloc] initWithUTF8String: gtk_entry_get_text(entry)];

	if ([text length] > 0) {
		gtk_entry_set_text(entry, "");
		params->block(text);
		[params->chat addMessage: text
				  sender: @"You"];
	}

	[text release];
}

static void call_close_block(GtkWidget *object, gpointer data)
{
	jub_close_block_t block = data;
	block();
}

@implementation JubGtkChatUI
- initWithTitle: (OFString*)title
     closeBlock: (jub_close_block_t)closeBlock
      sendBlock: (jub_send_block_t)sendBlock
{
	self = [super init];

	@try {
		__block volatile bool initialized = false;
		__block GtkWidget *chat_window;
		__block GtkTextBuffer *chat_buffer;

		_closeBlock = [closeBlock copy];
		_sendBlock = [sendBlock copy];
		_bufferEmpty = true;

		g_idle_add_block(^{
			GtkEntry *chat_entry;

			GtkBuilder *builder = gtk_builder_new();
			gtk_builder_add_from_file(builder, "data/gtk/chat.ui",
			    NULL);

			chat_window = GTK_WIDGET(
			    gtk_builder_get_object(builder, "ChatWindow"));
			g_signal_connect(chat_window, "destroy",
			    G_CALLBACK(call_close_block), _closeBlock);

			_chat_view = GTK_TEXT_VIEW(
			    gtk_builder_get_object(builder, "ChatTextView"));
			chat_buffer = gtk_text_view_get_buffer(_chat_view);
			gtk_text_buffer_create_tag(chat_buffer, "bold",
			    "weight", PANGO_WEIGHT_BOLD, NULL);

			chat_entry = GTK_ENTRY(
			    gtk_builder_get_object(builder, "ChatEntry"));
			struct call_send_block_params *send_params =
			    [self allocMemoryWithSize: sizeof(*send_params)];
			send_params->block = _sendBlock;
			send_params->chat = self;
			g_signal_connect(chat_entry, "activate",
			    G_CALLBACK(call_send_block), send_params);

			gtk_window_set_title(GTK_WINDOW(chat_window),
			    [title UTF8String]);
			gtk_widget_show(chat_window);

			initialized = true;

			g_object_unref(builder);
		});

		while (!initialized);

		_chat_buffer = chat_buffer;
		_chat_window = chat_window;
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	gtk_widget_destroy(_chat_window);
	[_sendBlock release];
	[_closeBlock release];

	[super dealloc];
}

- (void)addMessage: (OFString*)text
	    sender: (OFString*)sender
{
	bool first = _bufferEmpty;
	if (OF_UNLIKELY(_bufferEmpty)) _bufferEmpty = false;

	g_idle_add_block(^{
		GtkTextIter end_iter;
		GtkTextMark *insert_mark =
		    gtk_text_buffer_get_insert(_chat_buffer);

		if (OF_LIKELY(!first))
			gtk_text_buffer_insert_at_cursor(_chat_buffer, "\n", 1);

		gtk_text_buffer_get_end_iter(_chat_buffer, &end_iter);
		gtk_text_buffer_insert_with_tags_by_name(_chat_buffer,
		    &end_iter, [sender UTF8String], [sender UTF8StringLength],
		    "bold", NULL);
		gtk_text_buffer_get_end_iter(_chat_buffer, &end_iter);
		gtk_text_buffer_insert_with_tags_by_name(_chat_buffer,
		    &end_iter, ": ", 2, "bold", NULL);

		gtk_text_buffer_insert_at_cursor(_chat_buffer,
		    [text UTF8String], [text UTF8StringLength]);

		gtk_text_view_scroll_to_mark(_chat_view, insert_mark, 0,
		    FALSE, 0, 0);
	});
}
@end
