/*
 * 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 "arvo.h"
#include "roccat_device_sysfs.h"
#include <stdlib.h>
#include <string.h>

static gchar const * const actual_profile_name = "actual_profile";
static gchar const * const mode_key_name = "mode_key";
static gchar const * const key_mask_name = "key_mask";

guint arvo_actual_profile_read(RoccatDevice *arvo, GError **error) {
	guint result;
	gchar *string = roccat_device_sysfs_read(arvo, actual_profile_name, 0, error);
	if (*error) return 0;
	result = strtoul(string, NULL, 10);
	g_free(string);
	return result;
}

gboolean arvo_actual_profile_write(RoccatDevice *arvo, guint profile_number, GError **error) {
	gboolean result;
	gchar *string;

	g_assert(profile_number >= 1 && profile_number <= ARVO_PROFILE_NUM);
	string = g_strdup_printf("%i", profile_number);
	result = roccat_device_sysfs_write(arvo, actual_profile_name, string, strlen(string), error);
	g_free(string);
	return result;
}

guint arvo_mode_key_read(RoccatDevice *arvo, GError **error) {
	guint result;
	gchar *string = roccat_device_sysfs_read(arvo, mode_key_name, 0, error);
	if (*error) return 0;
	result = strtoul(string, NULL, 10);
	g_free(string);
	return result;
}

gboolean arvo_mode_key_write(RoccatDevice *arvo, guint mode_key, GError **error) {
	gboolean result;
	gchar *string;

	g_assert(mode_key == ARVO_MODE_KEY_STATE_ON || mode_key == ARVO_MODE_KEY_STATE_OFF);
	string = g_strdup_printf("%i", mode_key);
	result = roccat_device_sysfs_write(arvo, mode_key_name, string, strlen(string), error);
	g_free(string);
	return result;
}

guint arvo_key_mask_read(RoccatDevice *arvo, GError **error) {
	guint result;
	gchar *string = roccat_device_sysfs_read(arvo, key_mask_name, 0, error);
	if (*error) return 0;
	result = strtoul(string, NULL, 10);
	g_free(string);
	return result;
}

gboolean arvo_key_mask_write(RoccatDevice *arvo, guint key_mask, GError **error) {
	gboolean result;
	gchar *string;
	g_assert((key_mask & 0xe0) == 0);
	string = g_strdup_printf("%i", key_mask);
	result = roccat_device_sysfs_write(arvo, key_mask_name, string, strlen(string), error);
	g_free(string);
	return result;
}

gboolean arvo_profile_activate(RoccatDevice *device, guint profile_number, ArvoRkp *rkp, GError **error) {
	guint mode_key, key_mask;

	mode_key = arvo_rkp_get_mode_key(rkp);
	key_mask = arvo_rkp_get_key_mask(rkp);

	arvo_actual_profile_write(device, profile_number, error);
	if (*error)
		return FALSE;
	arvo_mode_key_write(device, mode_key, error);
	if (*error)
		return FALSE;
	arvo_key_mask_write(device, key_mask, error);
	if (*error)
		return FALSE;
	return TRUE;
}
