/*
 * 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 "konepure_color_frame.h"
#include "konepure_color_palette_button.h"

#define KONEPURE_COLOR_FRAME_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), KONEPURE_COLOR_FRAME_TYPE, KonepureColorFrameClass))
#define IS_KONEPURE_COLOR_FRAME_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), KONEPURE_COLOR_FRAME_TYPE))
#define KONEPURE_COLOR_FRAME_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), KONEPURE_COLOR_FRAME_TYPE, KonepureColorFramePrivate))

typedef struct _KonepureColorFrameClass KonepureColorFrameClass;
typedef struct _KonepureColorFramePrivate KonepureColorFramePrivate;

struct _KonepureColorFrame {
	GtkFrame parent;
	KonepureColorFramePrivate *priv;
};

struct _KonepureColorFrameClass {
	GtkFrameClass parent_class;
};

struct _KonepureColorFramePrivate {
	RoccatColorPaletteButton *color;
	GtkToggleButton *state;
};

G_DEFINE_TYPE(KonepureColorFrame, konepure_color_frame, GTK_TYPE_FRAME);

void konepure_color_frame_set_from_rmp(KonepureColorFrame *frame, KoneplusRmp *rmp) {
	KonepureColorFramePrivate *priv = frame->priv;
	KoneplusRmpLightInfo *light_info;

	light_info = koneplus_rmp_get_rmp_light_info(rmp, 0);
	roccat_color_palette_button_set_index(priv->color, light_info->index);
	gtk_toggle_button_set_active(priv->state, (light_info->state == KONEPLUS_RMP_LIGHT_INFO_STATE_ON));
	g_free(light_info);
}

void konepure_color_frame_update_rmp(KonepureColorFrame *frame, KoneplusRmp *rmp) {
	KonepureColorFramePrivate *priv = frame->priv;
	KoneplusRmpLightInfo light_info;
	KoneplusRmpLightInfo const *standard_light_info;
	guint color_index, state;

	color_index = roccat_color_palette_button_get_index(priv->color);
	state = gtk_toggle_button_get_active(priv->state) ?
		KONEPLUS_RMP_LIGHT_INFO_STATE_ON : KONEPLUS_RMP_LIGHT_INFO_STATE_OFF;

	standard_light_info = konepure_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, 0, &light_info);
	konepure_rmp_set_light_chose_type(rmp, KONEPLUS_RMP_LIGHT_CHOSE_TYPE_TABLE);
}

GtkWidget *konepure_color_frame_new(void) {
	KonepureColorFrame *frame;

	frame = KONEPURE_COLOR_FRAME(g_object_new(KONEPURE_COLOR_FRAME_TYPE,
			NULL));

	return GTK_WIDGET(frame);
}

static void konepure_color_frame_init(KonepureColorFrame *frame) {
	KonepureColorFramePrivate *priv = KONEPURE_COLOR_FRAME_GET_PRIVATE(frame);
	GtkWidget *hbox;
	GtkWidget *vbox;

	frame->priv = priv;

	hbox = gtk_hbox_new(FALSE, 0);
	/* vbox is used to always keep vertical size of button */
	vbox = gtk_vbox_new(FALSE, 0);
	priv->color = ROCCAT_COLOR_PALETTE_BUTTON(konepure_color_palette_button_new());
	priv->state = GTK_TOGGLE_BUTTON(gtk_check_button_new());

	gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(priv->color), TRUE, FALSE, 0);
	gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(priv->state), TRUE, FALSE, 0);
	gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, FALSE, 0);
	gtk_container_add(GTK_CONTAINER(frame), vbox);

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

static void konepure_color_frame_class_init(KonepureColorFrameClass *klass) {
	g_type_class_add_private(klass, sizeof(KonepureColorFramePrivate));
}
