/*
 * 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/kovaplus.h"
#include "roccat_helper.h"

static gchar const * const profile_buttons_name = "profile_buttons";

static gchar *kovaplus_build_profile_buttons_read_name(guint profile_index) {
	return g_strdup_printf("profile%i_settings", profile_index + 1);
}

static guint16 kovaplus_profile_buttons_calc_checksum(KovaplusProfileButtons const *profile_buttons) {
	return ROCCAT_BYTESUM_PARTIALLY(profile_buttons, KovaplusProfileButtons, command, checksum);
}

void kovaplus_profile_buttons_set_checksum(KovaplusProfileButtons *profile_buttons) {
	profile_buttons->checksum = GUINT16_TO_LE(kovaplus_profile_buttons_calc_checksum(profile_buttons));
}

void kovaplus_profile_buttons_finalize(KovaplusProfileButtons *profile_buttons, guint profile_index) {
	g_assert(profile_index < KOVAPLUS_PROFILE_NUM);
	profile_buttons->command = KOVAPLUS_COMMAND_PROFILE_BUTTONS;
	profile_buttons->size = sizeof(KovaplusProfileButtons);
	profile_buttons->profile_index = profile_index;
	kovaplus_profile_buttons_set_checksum(profile_buttons);
}

gboolean kovaplus_profile_buttons_write(RoccatDevice *kovaplus, guint profile_index, KovaplusProfileButtons *profile_buttons, GError **error) {
	gboolean result;
	g_assert(profile_index < KOVAPLUS_PROFILE_NUM);
	kovaplus_profile_buttons_finalize(profile_buttons, profile_index);
	result = roccat_device_write(kovaplus, profile_buttons_name,
			(gchar const *)profile_buttons, sizeof(KovaplusProfileButtons), error);
	return result;
}

static KovaplusProfileButtons *kovaplus_profile_buttons_read_fallback(RoccatDevice *kovaplus, guint profile_index, GError **error) {
	g_assert(profile_index < KOVAPLUS_PROFILE_NUM);
	gchar *name = kovaplus_build_profile_buttons_read_name(profile_index);
	KovaplusProfileButtons *result;
	result = (KovaplusProfileButtons *)roccat_device_read(kovaplus, name, sizeof(KovaplusProfileButtons), error);
	g_free(name);
	return result;
}

KovaplusProfileButtons *kovaplus_profile_buttons_read(RoccatDevice *kovaplus, guint profile_number, GError **error) {
	KovaplusProfileButtons *buttons;

	g_assert(profile_number < KOVAPLUS_PROFILE_NUM);

	roccat_device_lock(kovaplus);

	if (!kovaplus_select(kovaplus, profile_number, KOVAPLUS_CONTROL_REQUEST_PROFILE_BUTTONS, error)) {
		g_clear_error(error);
		roccat_device_unlock(kovaplus);
		return kovaplus_profile_buttons_read_fallback(kovaplus, profile_number, error);
	}

	buttons = (KovaplusProfileButtons *)roccat_device_read(kovaplus, profile_buttons_name, sizeof(KovaplusProfileButtons), error);

	roccat_device_unlock(kovaplus);

	return buttons;
}
