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

#define KONECONFIG_SENSITIVITY_SELECTOR_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), KONECONFIG_SENSITIVITY_SELECTOR_TYPE, KoneconfigSensitivitySelectorClass))
#define IS_KONECONFIG_SENSITIVITY_SELECTOR_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), KONECONFIG_SENSITIVITY_SELECTOR_TYPE))

typedef struct _KoneconfigSensitivitySelectorClass KoneconfigSensitivitySelectorClass;

struct _KoneconfigSensitivitySelector {
	RoccatSensitivitySelector parent;
};

struct _KoneconfigSensitivitySelectorClass {
	RoccatSensitivitySelectorClass parent_class;
};

G_DEFINE_TYPE(KoneconfigSensitivitySelector, koneconfig_sensitivity_selector, ROCCAT_SENSITIVITY_SELECTOR_TYPE);

static guint scale_to_bin(gdouble scale) {
	return (guint)(100.0 * scale);
}

static gdouble bin_to_scale(guint bin) {
	return (gdouble)bin / 100.0;
}

void koneconfig_sensitivity_selector_set_from_rmp(KoneconfigSensitivitySelector *selector, KoneRMP *rmp) {
	RoccatSensitivitySelector *roccat_selector = ROCCAT_SENSITIVITY_SELECTOR(selector);
	guint advanced = kone_rmp_get_advanced_sensitivity(rmp);
	roccat_sensitivity_selector_set_linked(roccat_selector, advanced == KONE_ADVANCED_SENSITIVITY_OFF);
	if (advanced == KONE_ADVANCED_SENSITIVITY_ON) {
		roccat_sensitivity_selector_set_x_value_blocked(roccat_selector, bin_to_scale(kone_rmp_get_x_sensitivity(rmp)));
		roccat_sensitivity_selector_set_y_value_blocked(roccat_selector, bin_to_scale(kone_rmp_get_y_sensitivity(rmp)));
	} else {
		roccat_sensitivity_selector_set_x_value_blocked(roccat_selector, bin_to_scale(kone_rmp_get_main_sensitivity(rmp)));
		roccat_sensitivity_selector_set_y_value_blocked(roccat_selector, bin_to_scale(kone_rmp_get_main_sensitivity(rmp)));
	}
}

void koneconfig_sensitivity_selector_update_rmp(KoneconfigSensitivitySelector *selector, KoneRMP *rmp) {
	RoccatSensitivitySelector *roccat_selector = ROCCAT_SENSITIVITY_SELECTOR(selector);
	kone_rmp_set_main_sensitivity(rmp, scale_to_bin(roccat_sensitivity_selector_get_x_value(roccat_selector)));
	kone_rmp_set_x_sensitivity(rmp, scale_to_bin(roccat_sensitivity_selector_get_x_value(roccat_selector)));
	kone_rmp_set_y_sensitivity(rmp, scale_to_bin(roccat_sensitivity_selector_get_y_value(roccat_selector)));
	kone_rmp_set_advanced_sensitivity(rmp, roccat_sensitivity_selector_get_linked(roccat_selector) ? KONE_ADVANCED_SENSITIVITY_OFF : KONE_ADVANCED_SENSITIVITY_ON);
}

GtkWidget *koneconfig_sensitivity_selector_new(void) {
	KoneconfigSensitivitySelector *selector;

	selector = KONECONFIG_SENSITIVITY_SELECTOR(g_object_new(KONECONFIG_SENSITIVITY_SELECTOR_TYPE,
			"min", (gdouble)bin_to_scale(KONE_MAIN_SENSITIVITY_MIN),
			"max", (gdouble)bin_to_scale(KONE_MAIN_SENSITIVITY_MAX),
			"step", (gdouble)1.0,
			NULL));

	roccat_sensitivity_selector_set_digits(ROCCAT_SENSITIVITY_SELECTOR(selector), 2);

	return GTK_WIDGET(selector);
}

static void koneconfig_sensitivity_selector_init(KoneconfigSensitivitySelector *selector) {}

static void koneconfig_sensitivity_selector_class_init(KoneconfigSensitivitySelectorClass *klass) {}
