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

static gchar const * const profile_settings_name = "profile_settings";

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

static guint16 pyra_profile_settings_calc_checksum(PyraProfileSettings const *profile_settings) {
	return ROCCAT_BYTESUM_PARTIALLY(profile_settings, PyraProfileSettings, command, checksum);
}

static void pyra_profile_settings_set_checksum(PyraProfileSettings *profile_settings) {
	guint16 checksum = pyra_profile_settings_calc_checksum(profile_settings);
	profile_settings->checksum = GUINT16_TO_LE(checksum);
}

void pyra_profile_settings_finalize(PyraProfileSettings *profile_settings, guint index) {
	g_assert(index < PYRA_PROFILE_NUM);
	profile_settings->number = index;
	pyra_profile_settings_set_checksum(profile_settings);
}

gboolean pyra_profile_settings_write(RoccatDevice *pyra, guint profile_number, PyraProfileSettings *profile_settings, GError **error) {
	g_assert(profile_number < PYRA_PROFILE_NUM);
	pyra_profile_settings_finalize(profile_settings, profile_number);
	return roccat_device_sysfs_write(pyra, profile_settings_name, (gchar const *)profile_settings, sizeof(PyraProfileSettings), error);
}

static PyraProfileSettings *pyra_profile_settings_read_fallback(RoccatDevice *pyra, guint profile_number, GError **error) {
	g_assert(profile_number < PYRA_PROFILE_NUM);
	gchar *name = pyra_build_profile_settings_read_name(profile_number);
	PyraProfileSettings *result = (PyraProfileSettings *)roccat_device_sysfs_read(pyra, name, sizeof(PyraProfileSettings), error);
	g_free(name);
	return result;
}

PyraProfileSettings *pyra_profile_settings_read(RoccatDevice *pyra, guint profile_number, GError **error) {
	PyraProfileSettings *settings;

	g_assert(profile_number < PYRA_PROFILE_NUM);

	roccat_device_lock(pyra);

	if (!pyra_select(pyra, profile_number, PYRA_CONTROL_REQUEST_PROFILE_SETTINGS, error)) {
		g_clear_error(error);
		roccat_device_unlock(pyra);
		return pyra_profile_settings_read_fallback(pyra, profile_number, error);
	}

	settings = (PyraProfileSettings *)roccat_device_sysfs_read(pyra, profile_settings_name, sizeof(PyraProfileSettings), error);

	roccat_device_unlock(pyra);

	return settings;
}
