/*
 * 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_settings_name = "profile_settings";

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

static guint16 koneplus_profile_settings_calc_checksum(KoneplusProfileSettings const *profile_settings) {
	return ROCCAT_BYTESUM_PARTIALLY(profile_settings, KoneplusProfileSettings, command, checksum);
}

static void koneplus_profile_settings_set_checksum(KoneplusProfileSettings *profile_settings) {
	guint16 checksum = koneplus_profile_settings_calc_checksum(profile_settings);
	profile_settings->checksum = GUINT16_TO_LE(checksum);
}

void koneplus_profile_settings_finalize(KoneplusProfileSettings *profile_settings, guint index) {
	g_assert(index < KONEPLUS_PROFILE_NUM);
	profile_settings->profile_number = index;
	koneplus_profile_settings_set_checksum(profile_settings);
}

gboolean koneplus_profile_settings_write(RoccatDevice *koneplus, guint profile_number, KoneplusProfileSettings *profile_settings, GError **error) {
	g_assert(profile_number < KONEPLUS_PROFILE_NUM);
	koneplus_profile_settings_finalize(profile_settings, profile_number);
	return roccat_device_write(koneplus, profile_settings_name, (gchar const *)profile_settings, sizeof(KoneplusProfileSettings), error);
}

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

KoneplusProfileSettings *koneplus_profile_settings_read(RoccatDevice *koneplus, guint profile_number, GError **error) {
	KoneplusProfileSettings *settings;

	g_assert(profile_number < KONEPLUS_PROFILE_NUM);

	roccat_device_lock(koneplus);

	if (!koneplus_select(koneplus, profile_number, KONEPLUS_CONTROL_REQUEST_PROFILE_SETTINGS, error)) {
		g_clear_error(error);
		roccat_device_unlock(koneplus);
		return koneplus_profile_settings_read_fallback(koneplus, profile_number, error);
	}

	settings = (KoneplusProfileSettings *)roccat_device_read(koneplus, profile_settings_name, sizeof(KoneplusProfileSettings), error);

	roccat_device_unlock(koneplus);

	return settings;
}
