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

static gchar const * const profile_buttons_name = "profile_buttons";

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

static guint16 pyra_profile_buttons_calc_checksum(PyraProfileButtons const *profile_buttons) {
	return ROCCAT_BYTESUM_PARTIALLY(profile_buttons, PyraProfileButtons, command, checksum);
}

static void pyra_profile_buttons_set_checksum(PyraProfileButtons *profile_buttons) {
	guint16 checksum = pyra_profile_buttons_calc_checksum(profile_buttons);
	profile_buttons->checksum = GUINT16_TO_LE(checksum);
}

void pyra_profile_buttons_finalize(PyraProfileButtons *profile_buttons, guint profile_number) {
	g_assert(profile_number < PYRA_PROFILE_NUM);
	profile_buttons->number = profile_number;
	pyra_profile_buttons_set_checksum(profile_buttons);
}

gboolean pyra_profile_buttons_write(RoccatDevice *pyra, guint profile_number, PyraProfileButtons *profile_buttons, GError **error) {
	g_assert(profile_number < PYRA_PROFILE_NUM);
	pyra_profile_buttons_finalize(profile_buttons, profile_number);
	return roccat_device_write(pyra, profile_buttons_name, (gchar const *)profile_buttons, sizeof(PyraProfileButtons), error);
}

static PyraProfileButtons *pyra_profile_buttons_read_fallback(RoccatDevice *pyra, guint profile_number, GError **error) {
	g_assert(profile_number < PYRA_PROFILE_NUM);
	gchar *name = pyra_build_profile_buttons_read_name(profile_number);
	PyraProfileButtons *result = (PyraProfileButtons *)roccat_device_read(pyra, name, sizeof(PyraProfileButtons), error);
	g_free(name);
	return result;
}

PyraProfileButtons *pyra_profile_buttons_read(RoccatDevice *pyra, guint profile_number, GError **error) {
	PyraProfileButtons *buttons;

	g_assert(profile_number < PYRA_PROFILE_NUM);

	roccat_device_lock(pyra);

	if (!pyra_select(pyra, profile_number, PYRA_CONTROL_REQUEST_PROFILE_BUTTONS, error)) {
		g_clear_error(error);
		roccat_device_unlock(pyra);
		return pyra_profile_buttons_read_fallback(pyra, profile_number, error);
	}

	buttons = (PyraProfileButtons *)roccat_device_read(pyra, profile_buttons_name, sizeof(PyraProfileButtons), error);

	roccat_device_unlock(pyra);

	return buttons;
}
