/*
 *  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 "color.h"


typedef struct ColorDialogDataS {
	GtkWidget *buttons[16];
	int        selected;
} ColorDialogDataT;


static void     addCss(GtkWidget *widget, char *css);
EVENT  void     colorClicked(GtkButton *widget, gpointer userData);
EVENT  gboolean colorDialogClose(GtkWidget *object, GdkEvent *event, gpointer userData);


static void addCss(GtkWidget *widget, char *css) {
	GtkCssProvider   *provider;
	GtkStyleContext  *context;

	provider = gtk_css_provider_new();
	gtk_css_provider_load_from_data(provider, css, -1,NULL);
	context = gtk_widget_get_style_context(widget);
	gtk_style_context_add_provider(context, GTK_STYLE_PROVIDER(provider),GTK_STYLE_PROVIDER_PRIORITY_USER);
	g_object_unref(provider);
}


EVENT void colorClicked(GtkButton *widget, gpointer userData) {
	ColorDialogDataT *self = (ColorDialogDataT *)userData;
	int               i;
	char              *borderOn  = "* { border-width: 3px; border-color: white; }";
	char              *borderOff = "* { border-width: 0px; }";

	for (i=0; i<16; i++) {
		if (widget == GTK_BUTTON(self->buttons[i])) {
			if (self->selected >= 0) {
				addCss(self->buttons[self->selected], borderOff);
			}
			self->selected = i;
			addCss(self->buttons[self->selected], borderOn);
		}
	}
}


EVENT gboolean colorDialogClose(GtkWidget *object, GdkEvent *event, gpointer userData) {
	ColorDialogDataT *self = g_object_get_data(G_OBJECT(object), "colorData");

	(void)event;
	(void)userData;

	DEL(self);

	return FALSE;
}


int color_dialog_get_selected(GtkWidget *colorDialog) {
	ColorDialogDataT *self = g_object_get_data(G_OBJECT(colorDialog), "colorData");

	return self->selected;
}


GtkWidget *color_dialog_new(GtkWidget *parent, char *title, ColorT colors[]) {
	GtkWidget        *dialog;
	GtkWidget        *contentArea;
	GtkWidget        *grid;
	char              temp[256];
	int               x;
	int               y;
	int               i;
	ColorDialogDataT *self;

	self = NEW(ColorDialogDataT);
	self->selected = -1;

	grid = gtk_grid_new();
	gtk_grid_set_column_homogeneous(GTK_GRID(grid), TRUE);
	gtk_grid_set_row_homogeneous(GTK_GRID(grid), TRUE);
	i = 0;
	for (y=0; y<4; y++) {
		for (x=0; x<4; x++) {
			self->buttons[i] = gtk_button_new();
			gtk_widget_set_hexpand(self->buttons[i], TRUE);
			gtk_widget_set_vexpand(self->buttons[i], TRUE);
			g_signal_connect(G_OBJECT(self->buttons[i]), "clicked", G_CALLBACK(colorClicked), self);
			snprintf(temp, 256, "* { background-image:none; background-color: #%02x%02x%02x; }", colors[i].red * 16, colors[i].green * 16, colors[i].blue * 16);
			addCss(self->buttons[i], temp);
			gtk_grid_attach(GTK_GRID(grid), self->buttons[i], x, y, 1, 1);

			i++;
		}
	}

	dialog = gtk_dialog_new_with_buttons(
					title,
					GTK_WINDOW(parent),
					GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
					 "_Cancel",
					 GTK_RESPONSE_CANCEL,
					"_Edit",
					COLOR_RESPONSE_EDIT,
					"_Select",
					GTK_RESPONSE_OK,
					NULL
				);

	g_signal_connect(G_OBJECT(dialog), "destroy", G_CALLBACK(colorDialogClose), self);
	g_object_set_data(G_OBJECT(dialog), "colorData", self);
	gtk_widget_set_size_request(dialog, 320, 320);
	contentArea = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
	gtk_container_add(GTK_CONTAINER(contentArea), grid);

	gtk_widget_show_all(dialog);

	return dialog;
}
