/*
 *  JoeyDev
 *  Copyright (C) 2018-2023 Scott Duensing <scott@kangaroopunch.com>
 *
 *  This software is provided 'as-is', without any express or implied
 *  warranty.  In no event will the authors be held liable for any damages
 *  arising from the use of this software.
 *
 *  Permission is granted to anyone to use this software for any purpose,
 *  including commercial applications, and to alter it and redistribute it
 *  freely, subject to the following restrictions:
 *
 *  1. The origin of this software must not be misrepresented; you must not
 *     claim that you wrote the original software. If you use this software
 *     in a product, an acknowledgment in the product documentation would be
 *     appreciated but is not required.
 *  2. Altered source versions must be plainly marked as such, and must not be
 *     misrepresented as being the original software.
 *  3. This notice may not be removed or altered from any source distribution.
*/


#include "common.h"
#include "messages.h"
#include "utils.h"


static GtkWidget *_lstMessages = NULL;


EVENT  gboolean winMessagesClose(GtkWidget *object, gpointer userData);
static void     winMessagesDelete(gpointer userData);


void message(MessageTypesT level, char *format, ...) {
	WindowDataT  *self = NULL;
	char         *widgetNames[]  = {
			"winMessages",
			"lstMessages",
			NULL
	};
	GtkWidget   **widgets[]      = {
			NULL,
			&_lstMessages
	};
	GtkWidget *row;
	GtkWidget *box;
	GtkWidget *label;
	va_list    args;
	char      *string;
	char      *msg;
	char      *tok;
	char      *labels[MSG_COUNT] = {
							"<span foreground=\"gray\">   Info:</span>",
							"<span foreground=\"yellow\">Warning:</span>",
							"<span foreground=\"red\">  Error:</span>",
							"<span foreground=\"red\"><b> Severe:</b></span>"
					};

	// Do we need to open this window?
	if (!_lstMessages) {
		// Set up instance data.  We only need WindowDataT since this is a "singleton" window.
		self = NEW(WindowDataT);
		self->closeWindow = winMessagesClose;

		widgets[0] = &self->window;
		utilGetWidgetsFromMemory("/com/kangaroopunch/joeydev/Messages.glade", widgetNames, widgets, self);

		// Register window.
		utilWindowRegister(self);

		// Show window.
		gtk_widget_show_all(self->window);
	}

	// Display message.
	va_start(args, format);
	msg = utilCreateStringVArgs(format, args);
	va_end(args);

	// Break multiline messages down into individual lines.
	tok = strtok(msg, "\n");
	while (tok != NULL) {
		//***TODO*** Filter out things that could be mistaken as markup tags.
		string = utilCreateString("<tt>%s  %s</tt>", labels[level], tok);

		row = gtk_list_box_row_new();
		box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 6);
		gtk_widget_set_hexpand(box, TRUE);
		label = gtk_label_new(string);
		gtk_label_set_use_markup(GTK_LABEL(label), TRUE);

		DEL(string);

		gtk_box_pack_start(GTK_BOX(box), label, FALSE, FALSE, 0);
		gtk_container_add(GTK_CONTAINER(row), box);
		gtk_list_box_insert(GTK_LIST_BOX(_lstMessages), row, -1);

		gtk_widget_show_all(row);

		tok = strtok(NULL, "\n");
	}
}


EVENT gboolean winMessagesClose(GtkWidget *object, gpointer userData) {
	// userData is not reliable due to util indirectly calling us.
	WindowDataT *self = (WindowDataT *)utilGetWindowData(object);

	(void)userData;

	winMessagesDelete(self);
	return FALSE;
}


static void winMessagesDelete(gpointer userData) {
	utilWindowUnRegister(userData);
	_lstMessages = NULL;
	DEL(userData);
}
