/*
 * 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 Macro 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 Macro Public License for more details.
 *
 * You should have received a copy of the GNU Macro Public License
 * along with roccat-tools. If not, see <http://www.gnu.org/licenses/>.
 */

#include "roccat/ryos.h"
#include "roccat_helper.h"
#include <string.h>

static gchar const * const light_name = "stored_lights";

static guint16 ryos_stored_lights_calc_checksum(RyosStoredLights const *stored_lights) {
	return ROCCAT_BYTESUM_PARTIALLY(stored_lights, RyosStoredLights, command, checksum);
}

static void ryos_stored_lights_set_checksum(RyosStoredLights *stored_lights, guint16 new_value) {
	stored_lights->checksum = GUINT16_TO_LE(new_value);
}

static void ryos_stored_lights_finalize(RyosStoredLights *stored_lights, guint profile_index, guint mode) {
	stored_lights->command = RYOS_COMMAND_STORED_LIGHTS;
	stored_lights->size = sizeof(RyosStoredLights);
	stored_lights->profile_index = profile_index;
	stored_lights->illumination_mode = mode;
	ryos_stored_lights_set_checksum(stored_lights, ryos_stored_lights_calc_checksum(stored_lights));
}

gboolean ryos_stored_lights_write(RoccatDevice *ryos, guint profile_index, guint mode, RyosStoredLights *stored_lights, GError **error) {
	g_assert(profile_index < RYOS_PROFILE_NUM);
	ryos_stored_lights_finalize(stored_lights, profile_index, mode);
	return roccat_device_write(ryos, light_name, (gchar const *)stored_lights, sizeof(RyosStoredLights), error);
}

static RyosStoredLights *ryos_stored_lights_read(RoccatDevice *ryos, guint profile_index, guint request, GError **error) {
	RyosStoredLights *stored_lights;

	g_assert(profile_index < RYOS_PROFILE_NUM);

	roccat_device_lock(ryos);

	if (!ryos_select(ryos, profile_index, request, error)) {
		roccat_device_unlock(ryos);
		return NULL;
	}

	stored_lights = (RyosStoredLights *)roccat_device_read(ryos, light_name, sizeof(RyosStoredLights), error);

	roccat_device_unlock(ryos);

	return stored_lights;
}

RyosStoredLights *ryos_stored_lights_read_manual(RoccatDevice *ryos, guint profile_index, GError **error) {
	return ryos_stored_lights_read(ryos, profile_index, RYOS_CONTROL_REQUEST_STORED_LIGHTS_MANUAL, error);
}

RyosStoredLights *ryos_stored_lights_read_automatic(RoccatDevice *ryos, guint profile_index, GError **error) {
	return ryos_stored_lights_read(ryos, profile_index, RYOS_CONTROL_REQUEST_STORED_LIGHTS_AUTOMATIC, error);
}

gboolean ryos_stored_lights_equal(RyosStoredLights const *left, RyosStoredLights const *right) {
	gboolean equal;
	equal = ROCCAT_MEMCMP_PARTIALLY(left, right, RyosStoredLights, illumination_mode, checksum);
	return equal ? FALSE : TRUE;
}

void ryos_stored_lights_copy(RyosStoredLights *destination, RyosStoredLights const *source) {
	memcpy(destination, source, sizeof(RyosStoredLights));
}
