/*
 * This file is part of roccat-tools.
 *
 * roccat-tools is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * roccat-tools is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with roccat-tools. If not, see <http://www.gnu.org/licenses/>.
 */

#include "roccat_color_palette_dialog.h"
#include "roccat_color_button.h"
#include <math.h>

#define ROCCAT_COLOR_PALETTE_DIALOG_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), ROCCAT_COLOR_PALETTE_DIALOG_TYPE, RoccatColorPaletteDialogClass))
#define IS_ROCCAT_COLOR_PALETTE_DIALOG_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), ROCCAT_COLOR_PALETTE_DIALOG_TYPE))
#define ROCCAT_COLOR_PALETTE_DIALOG_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), ROCCAT_COLOR_PALETTE_DIALOG_TYPE, RoccatColorPaletteDialogPrivate))

typedef struct _RoccatColorPaletteDialogClass RoccatColorPaletteDialogClass;

struct _RoccatColorPaletteDialogClass {
	GtkDialogClass parent_class;
};

struct _RoccatColorPaletteDialogPrivate {
	gint index;
	size_t length;
	GtkWidget **buttons;
};

G_DEFINE_TYPE(RoccatColorPaletteDialog, roccat_color_palette_dialog, GTK_TYPE_DIALOG);

static gchar const * const index_key = "Index";

static size_t dialog_calculate_table_width(size_t list_length) {
	return (size_t)ceil(sqrt((double)list_length));
}

static size_t dialog_calculate_table_height(size_t list_length, size_t width) {
	return (list_length + width - 1) / width;
}

static void object_set_index(GObject *object, gint value) {
	g_object_set_data(object, index_key, GINT_TO_POINTER(value));
}

static gint object_get_index(GObject *object) {
	gpointer p = g_object_get_data(object, index_key);
	return GPOINTER_TO_INT(p);
}

static void dialog_button_clicked_cb(GtkButton *button, gpointer user_data) {
	RoccatColorPaletteDialog *color_palette_dialog = ROCCAT_COLOR_PALETTE_DIALOG(user_data);

	color_palette_dialog->priv->index = object_get_index(G_OBJECT(button));
	gtk_dialog_response(GTK_DIALOG(color_palette_dialog), GTK_RESPONSE_ACCEPT);
}

static GtkWidget *dialog_button_table_new(RoccatColorPaletteDialog *color_palette_dialog, GdkColor const *colors, size_t length) {
	RoccatColorPaletteDialogPrivate *priv = color_palette_dialog->priv;
	GtkWidget *table;
	size_t i, row, col;
	size_t rows, cols;

	cols = dialog_calculate_table_width(length);
	rows = dialog_calculate_table_height(length, cols);

	table = gtk_table_new(rows, cols, TRUE);

	priv->buttons = (GtkWidget **)g_malloc(sizeof(GtkWidget *) * length);
	priv->length = length;
	
	for (i = 0; i < length; ++i) {
		priv->buttons[i] = roccat_color_button_new_with_color(&colors[i]);
		g_object_ref_sink(G_OBJECT(priv->buttons[i]));
		object_set_index(G_OBJECT(priv->buttons[i]), i);
		g_signal_connect(G_OBJECT(priv->buttons[i]), "clicked", G_CALLBACK(dialog_button_clicked_cb), color_palette_dialog);

		row = i / cols;
		col = i % cols;
		gtk_table_attach(GTK_TABLE(table), priv->buttons[i], col, col + 1, row, row + 1, GTK_EXPAND, GTK_EXPAND, 0, 0);
	}

	return table;
}

gint roccat_color_palette_dialog(GtkWindow *parent, GdkColor const *colors, size_t length) {
	RoccatColorPaletteDialog *color_palette_dialog;
	gint index;

	color_palette_dialog = ROCCAT_COLOR_PALETTE_DIALOG(roccat_color_palette_dialog_new(parent, colors, length));
	(void)gtk_dialog_run(GTK_DIALOG(color_palette_dialog));
	index = roccat_color_palette_dialog_get_index(color_palette_dialog);
	gtk_widget_destroy(GTK_WIDGET(color_palette_dialog));

	return index;
}

gboolean roccat_color_palette_dialog_get_color_for_index(RoccatColorPaletteDialog *color_palette_dialog, gint index, GdkColor *color) {
	RoccatColorPaletteDialogPrivate *priv = color_palette_dialog->priv;

	if (index < 0 || index >= priv->length)
		return FALSE;

	roccat_color_button_get_color(ROCCAT_COLOR_BUTTON(priv->buttons[index]), color);
	return TRUE;
}

gboolean roccat_color_palette_dialog_get_color(RoccatColorPaletteDialog *color_palette_dialog, GdkColor *color) {
	return roccat_color_palette_dialog_get_color_for_index(color_palette_dialog, color_palette_dialog->priv->index, color);
}

gint roccat_color_palette_dialog_get_index(RoccatColorPaletteDialog *color_palette_dialog) {
	return color_palette_dialog->priv->index;
}

GtkWidget *roccat_color_palette_dialog_new(GtkWindow *parent, GdkColor const *colors, size_t length) {
	RoccatColorPaletteDialog *color_palette_dialog;
	GtkWidget *content_area;

	color_palette_dialog = g_object_new(ROCCAT_COLOR_PALETTE_DIALOG_TYPE, NULL);

	gtk_dialog_add_button(GTK_DIALOG(color_palette_dialog), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);

	gtk_window_set_title(GTK_WINDOW(color_palette_dialog), "Choose color");
	gtk_window_set_transient_for(GTK_WINDOW(color_palette_dialog), parent);
	gtk_window_set_modal(GTK_WINDOW(color_palette_dialog), TRUE);
	gtk_window_set_destroy_with_parent(GTK_WINDOW(color_palette_dialog), TRUE);

	content_area = gtk_dialog_get_content_area(GTK_DIALOG(color_palette_dialog));
	gtk_box_pack_start(GTK_BOX(content_area), dialog_button_table_new(color_palette_dialog, colors, length), TRUE, TRUE, 0);
	gtk_widget_show_all(GTK_WIDGET(content_area));

	return GTK_WIDGET(color_palette_dialog);
}

static void roccat_color_palette_dialog_init(RoccatColorPaletteDialog *color_palette_dialog) {
	RoccatColorPaletteDialogPrivate *priv = ROCCAT_COLOR_PALETTE_DIALOG_GET_PRIVATE(color_palette_dialog);
	color_palette_dialog->priv = priv;
	priv->index = -1;
}

static void roccat_color_palette_dialog_finalize(GObject *object) {
	RoccatColorPaletteDialogPrivate *priv = ROCCAT_COLOR_PALETTE_DIALOG(object)->priv;
	guint i;
	
	for (i = 0; i < priv->length; ++i)
		g_object_unref(G_OBJECT(priv->buttons[i]));
	
	G_OBJECT_CLASS(roccat_color_palette_dialog_parent_class)->finalize(object);
}

static void roccat_color_palette_dialog_class_init(RoccatColorPaletteDialogClass *klass) {
	GObjectClass *gobject_class = (GObjectClass *)klass;

	gobject_class->finalize = roccat_color_palette_dialog_finalize;
	
	g_type_class_add_private(klass, sizeof(RoccatColorPaletteDialogPrivate));
}
