/*
 * 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 "isku_rkp.h"
#include "roccat/iskufx.h"
#include "g_roccat_helper.h"

static gchar const * const iskufx_rkp_group_name = "Setting";

static gchar const * const iskufx_rkp_light_switch_name = "LtSwitch";
static gchar const * const iskufx_rkp_light_breath_switch_name = "LtBreathSwitch";
static gchar const * const iskufx_rkp_light_color_type_name = "LtColorType";
static gchar const * const iskufx_rkp_light_info_name = "FixedLtInfo";
/* TODO What is CCLtInfo? */

/*
 * May terminate application if error occurs
 */
static guint iskufx_rkp_get_default_value(gchar const *key) {
	IskuRkp *default_rkp = iskufx_default_rkp();
	GError *error = NULL;
	guint result;
	result = g_key_file_get_integer(default_rkp->key_file, iskufx_rkp_group_name, key, &error);
	isku_rkp_free(default_rkp);
	if (error)
		g_error("isku_rkp_get_default_value(): unknown error on key '%s': %s", key, error->message);
	return result;
}

static guint iskufx_rkp_get_value(IskuRkp *rkp, gchar const *key) {
	GError *error = NULL;
	guint result = g_key_file_get_integer(rkp->key_file, iskufx_rkp_group_name, key, &error);
	if (error) {
		g_clear_error(&error);
		result = iskufx_rkp_get_default_value(key);
	}
	return result;
}

static void iskufx_rkp_set_value(IskuRkp *rkp, gchar const *key, guint value) {
	g_key_file_set_integer(rkp->key_file, iskufx_rkp_group_name, key, value);
}

void iskufx_rkp_set_light_switch(IskuRkp *rkp, guint value) {
	g_assert(value >= ISKUFX_LIGHT_STATE_OFF && value <= ISKUFX_LIGHT_STATE_ON);
	if (iskufx_rkp_get_light_switch(rkp) != value) {
		iskufx_rkp_set_value(rkp, iskufx_rkp_light_switch_name, value);
		rkp->modified_light = TRUE;
	}
}

guint iskufx_rkp_get_light_switch(IskuRkp *rkp) {
	return iskufx_rkp_get_value(rkp, iskufx_rkp_light_switch_name);
}

void iskufx_rkp_set_light_breath_switch(IskuRkp *rkp, guint value) {
	g_assert(value >= ISKUFX_LIGHT_BREATHING_OFF && value <= ISKUFX_LIGHT_BREATHING_ON);
	if (iskufx_rkp_get_light_breath_switch(rkp) != value) {
		iskufx_rkp_set_value(rkp, iskufx_rkp_light_breath_switch_name, value);
		rkp->modified_light = TRUE;
	}
}

guint iskufx_rkp_get_light_breath_switch(IskuRkp *rkp) {
	return iskufx_rkp_get_value(rkp, iskufx_rkp_light_breath_switch_name);
}

void iskufx_rkp_set_light_color_type(IskuRkp *rkp, guint value) {
	g_assert(value >= ISKUFX_LIGHT_COLOR_TYPE_SINGLE && value <= ISKUFX_LIGHT_COLOR_TYPE_FLOW);
	if (iskufx_rkp_get_light_color_type(rkp) != value) {
		iskufx_rkp_set_value(rkp, iskufx_rkp_light_color_type_name, value);
		rkp->modified_light = TRUE;
	}
}

guint iskufx_rkp_get_light_color_type(IskuRkp *rkp) {
	return iskufx_rkp_get_value(rkp, iskufx_rkp_light_color_type_name);
}

IskufxRkpLightInfo *iskufx_rkp_get_rkp_light_info(IskuRkp *rkp) {
	IskuRkp *default_rkp;
	GError *error = NULL;
	IskufxRkpLightInfo *rkp_light_info;

	rkp_light_info = roccat_key_file_get_binary(rkp->key_file, iskufx_rkp_group_name, iskufx_rkp_light_info_name, sizeof(IskufxRkpLightInfo), &error);
	if (error) {
		g_clear_error(&error);
		default_rkp = iskufx_default_rkp();
		rkp_light_info = roccat_key_file_get_binary(default_rkp->key_file, iskufx_rkp_group_name, iskufx_rkp_light_info_name, sizeof(IskufxRkpLightInfo), &error);
		isku_rkp_free(default_rkp);
		if (error)
			g_error("isku_rkp_get_rkp_light_info: unknown error: %s", error->message);
	}
	return rkp_light_info;
}

void iskufx_rkp_set_rkp_light_info(IskuRkp *rkp, IskufxRkpLightInfo *rkp_light_info) {
	IskufxRkpLightInfo *actual_light_info;

	actual_light_info = iskufx_rkp_get_rkp_light_info(rkp);
	if (!iskufx_rkp_light_info_equal(rkp_light_info, actual_light_info)) {
		iskufx_rkp_light_info_set_checksum(rkp_light_info);
		roccat_key_file_set_binary(rkp->key_file, iskufx_rkp_group_name, iskufx_rkp_light_info_name, (gconstpointer)rkp_light_info, sizeof(IskufxRkpLightInfo));
		rkp->modified_light = TRUE;
	}

	g_free(actual_light_info);
}
