/*
 * 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 "koneplus_sensitivity_selector.h"
#include <gaminggear/gaminggear_xy_scales.h>

#define KONEPLUS_SENSITIVITY_SELECTOR_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), KONEPLUS_SENSITIVITY_SELECTOR_TYPE, KoneplusSensitivitySelectorClass))
#define IS_KONEPLUS_SENSITIVITY_SELECTOR_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), KONEPLUS_SENSITIVITY_SELECTOR_TYPE))

typedef struct _KoneplusSensitivitySelectorClass KoneplusSensitivitySelectorClass;

struct _KoneplusSensitivitySelector {
	GaminggearXyScales parent;
};

struct _KoneplusSensitivitySelectorClass {
	GaminggearXyScalesClass parent_class;
};

G_DEFINE_TYPE(KoneplusSensitivitySelector, koneplus_sensitivity_selector, GAMINGGEAR_XY_SCALES_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 koneplus_sensitivity_selector_set_from_rmp(KoneplusSensitivitySelector *selector, KoneplusRmp *rmp) {
	GaminggearXyScales *gaminggear_selector = GAMINGGEAR_XY_SCALES(selector);
	guint sync = koneplus_rmp_get_xy_synchronous(rmp);
	gaminggear_xy_scales_set_linked(gaminggear_selector, sync == KONEPLUS_PROFILE_SETTINGS_XY_SYNC_SYNC);
	if (sync == KONEPLUS_PROFILE_SETTINGS_XY_SYNC_ASYNC) {
		gaminggear_xy_scales_set_x_value(gaminggear_selector, bin_to_scale(koneplus_rmp_get_sensitivity_x(rmp)));
		gaminggear_xy_scales_set_y_value(gaminggear_selector, bin_to_scale(koneplus_rmp_get_sensitivity_y(rmp)));
	} else
		gaminggear_xy_scales_set_x_value(gaminggear_selector, bin_to_scale(koneplus_rmp_get_sensitivity(rmp)));
}

void koneplus_sensitivity_selector_update_rmp(KoneplusSensitivitySelector *selector, KoneplusRmp *rmp) {
	GaminggearXyScales *gaminggear_selector = GAMINGGEAR_XY_SCALES(selector);
	guint sync = gaminggear_xy_scales_get_linked(gaminggear_selector) ? KONEPLUS_PROFILE_SETTINGS_XY_SYNC_SYNC : KONEPLUS_PROFILE_SETTINGS_XY_SYNC_ASYNC;
	koneplus_rmp_set_xy_synchronous(rmp, sync);
	if (sync == KONEPLUS_PROFILE_SETTINGS_XY_SYNC_ASYNC) {
		koneplus_rmp_set_sensitivity_x(rmp, scale_to_bin(gaminggear_xy_scales_get_x_value(gaminggear_selector)));
		koneplus_rmp_set_sensitivity_y(rmp, scale_to_bin(gaminggear_xy_scales_get_y_value(gaminggear_selector)));
	} else
		koneplus_rmp_set_sensitivity(rmp, scale_to_bin(gaminggear_xy_scales_get_x_value(gaminggear_selector)));
}

GtkWidget *koneplus_sensitivity_selector_new(void) {
	KoneplusSensitivitySelector *selector;

	selector = KONEPLUS_SENSITIVITY_SELECTOR(g_object_new(KONEPLUS_SENSITIVITY_SELECTOR_TYPE,
			"min", (gdouble)bin_to_scale(KONEPLUS_PROFILE_SETTINGS_SENSITIVITY_MIN),
			"max", (gdouble)bin_to_scale(KONEPLUS_PROFILE_SETTINGS_SENSITIVITY_MAX),
			"step", (gdouble)1.0,
			NULL));

	return GTK_WIDGET(selector);
}

static void koneplus_sensitivity_selector_init(KoneplusSensitivitySelector *selector) {}

static void koneplus_sensitivity_selector_class_init(KoneplusSensitivitySelectorClass *klass) {}
