/*
 * 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_button.h"
#include "gtk_roccat_helper.h"
#include <math.h>

#define ROCCAT_COLOR_PALETTE_BUTTON_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), ROCCAT_COLOR_PALETTE_BUTTON_TYPE, RoccatColorPaletteButtonPrivate))

struct _RoccatColorPaletteButtonPrivate {
	gint index;
	size_t length;
	GdkColor const *colors;
};

enum {
	PROP_0,
	PROP_INDEX,
};

G_DEFINE_TYPE(RoccatColorPaletteButton, roccat_color_palette_button, ROCCAT_COLOR_BUTTON_TYPE);

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, GtkDialog *dialog) {
	object_set_index(G_OBJECT(dialog), object_get_index(G_OBJECT(button)));
	gtk_dialog_response(dialog, GTK_RESPONSE_ACCEPT);
}

static GtkWidget *dialog_button_table_new(GtkDialog *dialog, GdkColor const *colors, size_t length) {
	GtkWidget *button, *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);

	for (i = 0; i < length; ++i) {
		row = i / cols;
		col = i % cols;

		button = roccat_color_button_new_with_color(&colors[i]);
		object_set_index(G_OBJECT(button), i);
		g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(dialog_button_clicked_cb), dialog);
		gtk_table_attach(GTK_TABLE(table), button, col, col + 1, row, row + 1, GTK_EXPAND, GTK_EXPAND, 0, 0);
	}

	return table;
}

static GtkWidget *dialog_new(RoccatColorPaletteButton *color_palette_button) {
	RoccatColorPaletteButtonPrivate *priv = color_palette_button->priv;
	GtkWidget *content_area;
	GtkWidget *dialog;

	dialog = gtk_dialog_new_with_buttons("Choose Color", gtk_roccat_widget_get_gtk_window(GTK_WIDGET(color_palette_button)),
			GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
			GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
			NULL);

	content_area = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
	gtk_box_pack_start(GTK_BOX(content_area), dialog_button_table_new(GTK_DIALOG(dialog), priv->colors, priv->length), TRUE, TRUE, 0);
	gtk_widget_show_all(GTK_WIDGET(dialog));

	return dialog;
}

static void button_clicked_cb(RoccatColorPaletteButton *color_palette_button, gpointer user_data) {
	GtkDialog *dialog;

	dialog = GTK_DIALOG(dialog_new(color_palette_button));

	if (gtk_dialog_run(dialog) == GTK_RESPONSE_ACCEPT)
		roccat_color_palette_button_set_index(color_palette_button, object_get_index(G_OBJECT(dialog)));

	gtk_widget_destroy(GTK_WIDGET(dialog));
}

void roccat_color_palette_button_set_colors(RoccatColorPaletteButton *color_palette_button, GdkColor const *colors, size_t length) {
	RoccatColorPaletteButtonPrivate *priv = color_palette_button->priv;
	priv->length = length;
	priv->colors = colors;
}

GtkWidget *roccat_color_palette_button_new(GdkColor const *colors, size_t length) {
	RoccatColorPaletteButton *color_palette_button;

	color_palette_button = g_object_new(ROCCAT_COLOR_PALETTE_BUTTON_TYPE, NULL);

	roccat_color_palette_button_set_colors(color_palette_button, colors, length);

	return GTK_WIDGET(color_palette_button);
}

gint roccat_color_palette_button_get_index(RoccatColorPaletteButton const *color_palette_button) {
	return color_palette_button->priv->index;
}

void roccat_color_palette_button_set_index(RoccatColorPaletteButton *color_palette_button, gint index) {
	RoccatColorPaletteButtonPrivate *priv = color_palette_button->priv;
	if (index < 0 || index >= priv->length)
		return;

	if (index != priv->index) {
		priv->index = index;
		g_signal_emit_by_name((gpointer)color_palette_button, "value-changed");
	}
	roccat_color_button_set_color(ROCCAT_COLOR_BUTTON(color_palette_button), &priv->colors[index]);
}

static void roccat_color_palette_button_set_property(GObject *object, guint prop_id, GValue const *value, GParamSpec *pspec) {
	RoccatColorPaletteButton *color_palette_button = ROCCAT_COLOR_PALETTE_BUTTON(object);

	switch(prop_id) {
	case PROP_INDEX:
		roccat_color_palette_button_set_index(color_palette_button, g_value_get_uint(value));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
	}
}

static void roccat_color_palette_button_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) {
	RoccatColorPaletteButton *color_palette_button = ROCCAT_COLOR_PALETTE_BUTTON(object);

	switch(prop_id) {
	case PROP_INDEX:
		g_value_set_uint(value, roccat_color_palette_button_get_index(color_palette_button));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
	}
}

static void roccat_color_palette_button_init(RoccatColorPaletteButton *color_palette_button) {
	RoccatColorPaletteButtonPrivate *priv = ROCCAT_COLOR_PALETTE_BUTTON_GET_PRIVATE(color_palette_button);
	color_palette_button->priv = priv;

	priv->index = -1;

	g_signal_connect(G_OBJECT(color_palette_button), "clicked", G_CALLBACK(button_clicked_cb), NULL);
}

static void roccat_color_palette_button_class_init(RoccatColorPaletteButtonClass *klass) {
	GObjectClass *gobject_class;

	gobject_class = (GObjectClass*)klass;

	gobject_class->set_property = roccat_color_palette_button_set_property;
	gobject_class->get_property = roccat_color_palette_button_get_property;

	g_type_class_add_private(klass, sizeof(RoccatColorPaletteButtonPrivate));

	g_signal_new("value-changed",
			G_TYPE_FROM_CLASS(klass),
			G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
			G_STRUCT_OFFSET(RoccatColorPaletteButtonClass, value_changed),
			NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);

	g_object_class_install_property(gobject_class, PROP_INDEX,
			g_param_spec_int("index",
					"index",
					"Index of selected color",
					0, 255, 0,
					G_PARAM_READWRITE));
}
