/*
 * 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 "savuconfig_sensitivity_selector.h"
#include "roccat_sensitivity_selector.h"

#define SAVUCONFIG_SENSITIVITY_SELECTOR_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), SAVUCONFIG_SENSITIVITY_SELECTOR_TYPE, SavuconfigSensitivitySelectorClass))
#define IS_SAVUCONFIG_SENSITIVITY_SELECTOR_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), SAVUCONFIG_SENSITIVITY_SELECTOR_TYPE))

typedef struct _SavuconfigSensitivitySelectorClass SavuconfigSensitivitySelectorClass;

struct _SavuconfigSensitivitySelector {
	RoccatSensitivitySelector parent;
};

struct _SavuconfigSensitivitySelectorClass {
	RoccatSensitivitySelectorClass parent_class;
};

G_DEFINE_TYPE(SavuconfigSensitivitySelector, savuconfig_sensitivity_selector, ROCCAT_SENSITIVITY_SELECTOR_TYPE);

static guint scale_to_bin(gdouble scale) {
	return (guint)(scale + 6.0);
}

static gdouble bin_to_scale(guint bin) {
	return (gdouble)bin - 6.0;
}

void savuconfig_sensitivity_selector_set_from_rmp(SavuconfigSensitivitySelector *selector, SavuRmp *rmp) {
	RoccatSensitivitySelector *roccat_selector = ROCCAT_SENSITIVITY_SELECTOR(selector);
	roccat_sensitivity_selector_set_x_value_blocked(roccat_selector, bin_to_scale(savu_rmp_get_sensitivity_x(rmp)));
	roccat_sensitivity_selector_set_y_value_blocked(roccat_selector, bin_to_scale(savu_rmp_get_sensitivity_y(rmp)));
	roccat_sensitivity_selector_set_linked(roccat_selector, savu_rmp_get_xy_synchronous(rmp) == SAVU_GENERAL_SENSITIVITY_SYNC_ON);
}

void savuconfig_sensitivity_selector_update_rmp(SavuconfigSensitivitySelector *selector, SavuRmp *rmp) {
	RoccatSensitivitySelector *roccat_selector = ROCCAT_SENSITIVITY_SELECTOR(selector);
	savu_rmp_set_sensitivity_x(rmp, scale_to_bin(roccat_sensitivity_selector_get_x_value(roccat_selector)));
	savu_rmp_set_sensitivity_y(rmp, scale_to_bin(roccat_sensitivity_selector_get_y_value(roccat_selector)));
	savu_rmp_set_xy_synchronous(rmp, roccat_sensitivity_selector_get_linked(roccat_selector) ? SAVU_GENERAL_SENSITIVITY_SYNC_ON : SAVU_GENERAL_SENSITIVITY_SYNC_OFF);
}

GtkWidget *savuconfig_sensitivity_selector_new(void) {
	SavuconfigSensitivitySelector *selector;

	selector = SAVUCONFIG_SENSITIVITY_SELECTOR(g_object_new(SAVUCONFIG_SENSITIVITY_SELECTOR_TYPE,
			"min", (gdouble)bin_to_scale(SAVU_GENERAL_SENSITIVITY_MIN),
			"max", (gdouble)bin_to_scale(SAVU_GENERAL_SENSITIVITY_MAX),
			"step", (gdouble)1.0,
			NULL));

	return GTK_WIDGET(selector);
}

static void savuconfig_sensitivity_selector_init(SavuconfigSensitivitySelector *selector) {}

static void savuconfig_sensitivity_selector_class_init(SavuconfigSensitivitySelectorClass *klass) {}
