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

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

static guint16 kovaplus_profile_settings_calc_checksum(KovaplusProfileSettings const *profile_settings) {
	return ROCCAT_BYTESUM_PARTIALLY(profile_settings, KovaplusProfileSettings, command, checksum);
}

void kovaplus_profile_settings_set_checksum(KovaplusProfileSettings *profile_settings) {
	profile_settings->checksum = GUINT16_TO_LE(kovaplus_profile_settings_calc_checksum(profile_settings));
}

void kovaplus_profile_settings_finalize(KovaplusProfileSettings *profile_settings, guint profile_index) {
	g_assert(profile_index < KOVAPLUS_PROFILE_NUM);
	profile_settings->command = KOVAPLUS_COMMAND_PROFILE_SETTINGS;
	profile_settings->size = sizeof(KovaplusProfileSettings);
	profile_settings->profile_index = profile_index;
	kovaplus_profile_settings_set_checksum(profile_settings);
}

gboolean kovaplus_profile_settings_write(RoccatDevice *kovaplus, guint profile_index, KovaplusProfileSettings *profile_settings, GError **error) {
	gboolean result;
	g_assert(profile_index < KOVAPLUS_PROFILE_NUM);
	kovaplus_profile_settings_finalize(profile_settings, profile_index);
	result = roccat_device_write(kovaplus, profile_settings_name,
			(gchar const *)profile_settings, sizeof(KovaplusProfileSettings), error);
	return result;
}

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

KovaplusProfileSettings *kovaplus_profile_settings_read(RoccatDevice *kovaplus, guint profile_number, GError **error) {
	KovaplusProfileSettings *settings;

	g_assert(profile_number < KOVAPLUS_PROFILE_NUM);

	roccat_device_lock(kovaplus);

	if (!kovaplus_select(kovaplus, profile_number, KOVAPLUS_CONTROL_REQUEST_PROFILE_SETTINGS, error)) {
		g_clear_error(error);
		roccat_device_unlock(kovaplus);
		return kovaplus_profile_settings_read_fallback(kovaplus, profile_number, error);
	}

	settings = (KovaplusProfileSettings *)roccat_device_read(kovaplus, profile_settings_name, sizeof(KovaplusProfileSettings), error);

	roccat_device_unlock(kovaplus);

	return settings;
}

guint kovaplus_cpi_level_to_value(guint level) {
	return (1 << (level - 1)) * 400;
}
