/*
 * 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 "koneplus_color_frame.h"
#include "koneplus_color_palette_button.h"

#define KONEPLUS_COLOR_FRAME_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), KONEPLUS_COLOR_FRAME_TYPE, KoneplusColorFrameClass))
#define IS_KONEPLUS_COLOR_FRAME_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), KONEPLUS_COLOR_FRAME_TYPE))
#define KONEPLUS_COLOR_FRAME_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), KONEPLUS_COLOR_FRAME_TYPE, KoneplusColorFramePrivate))

typedef struct _KoneplusColorFrameClass KoneplusColorFrameClass;
typedef struct _KoneplusColorFramePrivate KoneplusColorFramePrivate;

struct _KoneplusColorFrame {
	GtkFrame parent;
	KoneplusColorFramePrivate *priv;
};

struct _KoneplusColorFrameClass {
	GtkFrameClass parent_class;
};

struct _KoneplusColorFramePrivate {
	RoccatColorPaletteButton *colors[KONEPLUS_PROFILE_SETTING_LIGHTS_NUM];
	GtkToggleButton *states[KONEPLUS_PROFILE_SETTING_LIGHTS_NUM];
};

G_DEFINE_TYPE(KoneplusColorFrame, koneplus_color_frame, GTK_TYPE_FRAME);

void koneplus_color_frame_set_from_rmp(KoneplusColorFrame *frame, KoneplusRmp *rmp) {
	KoneplusColorFramePrivate *priv = frame->priv;
	KoneplusRmpLightInfo *light_info;
	guint i;

	for (i = 0; i < KONEPLUS_PROFILE_SETTING_LIGHTS_NUM; ++i) {
		light_info = koneplus_rmp_get_rmp_light_info(rmp, i);
		roccat_color_palette_button_set_index(priv->colors[i], light_info->index);
		gtk_toggle_button_set_active(priv->states[i], (light_info->state == KONEPLUS_RMP_LIGHT_INFO_STATE_ON));
		g_free(light_info);
	}
}

void koneplus_color_frame_update_rmp(KoneplusColorFrame *frame, KoneplusRmp *rmp) {
	KoneplusColorFramePrivate *priv = frame->priv;
	KoneplusRmpLightInfo light_info;
	KoneplusRmpLightInfo const *standard_light_info;
	guint color_index, state;
	guint i;

	for (i = 0; i < KONEPLUS_PROFILE_SETTING_LIGHTS_NUM; ++i) {
		color_index = roccat_color_palette_button_get_index(priv->colors[i]);
		state = gtk_toggle_button_get_active(priv->states[i]) ?
			KONEPLUS_RMP_LIGHT_INFO_STATE_ON : KONEPLUS_RMP_LIGHT_INFO_STATE_OFF;

		standard_light_info = koneplus_rmp_light_info_get_standard(color_index);
		light_info.index = standard_light_info->index;
		light_info.state = state;
		light_info.red = standard_light_info->red;
		light_info.green = standard_light_info->green;
		light_info.blue = standard_light_info->blue;

		koneplus_rmp_set_rmp_light_info(rmp, i, &light_info);
	}
}

GtkWidget *koneplus_color_frame_new(void) {
	KoneplusColorFrame *frame;

	frame = KONEPLUS_COLOR_FRAME(g_object_new(KONEPLUS_COLOR_FRAME_TYPE,
			NULL));

	return GTK_WIDGET(frame);
}

static void koneplus_color_frame_init(KoneplusColorFrame *frame) {
	KoneplusColorFramePrivate *priv = KONEPLUS_COLOR_FRAME_GET_PRIVATE(frame);
	GtkWidget *hbox;
	GtkWidget *table;
	guint i;

	frame->priv = priv;

	/* order is in KoneplusProfileSettingsLightIndex */
	struct {
		guint left_attach, top_attach;
	} lights[KONEPLUS_PROFILE_SETTING_LIGHTS_NUM] = {
			{0, 0},
			{0, 1},
			{1, 0},
			{1, 1}
	};

	table = gtk_table_new(2, 2, TRUE);
	gtk_container_add(GTK_CONTAINER(frame), table);

	for (i = 0; i < KONEPLUS_PROFILE_SETTING_LIGHTS_NUM; ++i) {
		hbox = gtk_hbox_new(FALSE, 0);
		priv->colors[i] = ROCCAT_COLOR_PALETTE_BUTTON(koneplus_color_palette_button_new());
		priv->states[i] = GTK_TOGGLE_BUTTON(gtk_check_button_new());

		gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(priv->colors[i]), TRUE, FALSE, 0);
		gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(priv->states[i]), TRUE, FALSE, 0);
		gtk_table_attach(GTK_TABLE(table), hbox,
				lights[i].left_attach, lights[i].left_attach + 1,
				lights[i].top_attach, lights[i].top_attach + 1,
				GTK_EXPAND, GTK_EXPAND, 0, 0
		);
	}

	gtk_frame_set_label(GTK_FRAME(frame), "Colors");
}

static void koneplus_color_frame_class_init(KoneplusColorFrameClass *klass) {
	g_type_class_add_private(klass, sizeof(KoneplusColorFramePrivate));
}
