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

static gchar const * const profile_buttons_name = "profile_buttons";

static gchar *koneplus_build_profile_buttons_read_name(guint profile_number) {
	return g_strdup_printf("profile%i_buttons", profile_number + 1);
}

static guint16 koneplus_profile_buttons_calc_checksum(KoneplusProfileButtons const *profile_buttons) {
	return ROCCAT_BYTESUM_PARTIALLY(profile_buttons, KoneplusProfileButtons, command, checksum);
}

static void koneplus_profile_buttons_set_checksum(KoneplusProfileButtons *profile_buttons) {
	guint16 checksum = koneplus_profile_buttons_calc_checksum(profile_buttons);
	profile_buttons->checksum = GUINT16_TO_LE(checksum);
}

void koneplus_profile_buttons_finalize(KoneplusProfileButtons *profile_buttons, guint profile_number) {
	g_assert(profile_number < KONEPLUS_PROFILE_NUM);
	profile_buttons->profile_number = profile_number;
	koneplus_profile_buttons_set_checksum(profile_buttons);
}

gboolean koneplus_profile_buttons_write(RoccatDevice *koneplus, guint profile_number, KoneplusProfileButtons *profile_buttons, GError **error) {
	g_assert(profile_number < KONEPLUS_PROFILE_NUM);
	koneplus_profile_buttons_finalize(profile_buttons, profile_number);
	return roccat_device_write(koneplus, profile_buttons_name, (gchar const *)profile_buttons, sizeof(KoneplusProfileButtons), error);
}

static KoneplusProfileButtons *koneplus_profile_buttons_read_fallback(RoccatDevice *koneplus, guint profile_number, GError **error) {
	g_assert(profile_number < KONEPLUS_PROFILE_NUM);
	gchar *name = koneplus_build_profile_buttons_read_name(profile_number);
	KoneplusProfileButtons *result;
	result = (KoneplusProfileButtons *)roccat_device_read(koneplus, name, sizeof(KoneplusProfileButtons), error);
	g_free(name);
	return result;
}

KoneplusProfileButtons *koneplus_profile_buttons_read(RoccatDevice *koneplus, guint profile_number, GError **error) {
	KoneplusProfileButtons *profile_buttons;

	g_assert(profile_number < KONEPLUS_PROFILE_NUM);

	roccat_device_lock(koneplus);

	if (!koneplus_select(koneplus, profile_number, KONEPLUS_CONTROL_REQUEST_PROFILE_BUTTONS, error)) {
		g_clear_error(error);
		roccat_device_unlock(koneplus);
		return koneplus_profile_buttons_read_fallback(koneplus, profile_number, error);
	}

	profile_buttons = (KoneplusProfileButtons *)roccat_device_read(koneplus, profile_buttons_name, sizeof(KoneplusProfileButtons), error);

	roccat_device_unlock(koneplus);
	return profile_buttons;
}
