/*
 * 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_polling_rate_selector.h"
#include "roccat_polling_rate_selector.h"
#include "roccat/kone.h"

#define KONECONFIG_POLLING_RATE_SELECTOR_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), KONECONFIG_POLLING_RATE_SELECTOR_TYPE, KoneconfigPollingRateSelectorClass))
#define IS_KONECONFIG_POLLING_RATE_SELECTOR_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), KONECONFIG_POLLING_RATE_SELECTOR_TYPE))
#define KONECONFIG_POLLING_RATE_SELECTOR_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), KONECONFIG_POLLING_RATE_SELECTOR_TYPE, KoneconfigPollingRateSelectorPrivate))

typedef struct _KoneconfigPollingRateSelectorClass KoneconfigPollingRateSelectorClass;

struct _KoneconfigPollingRateSelector {
	RoccatPollingRateSelector parent;
};

struct _KoneconfigPollingRateSelectorClass {
	RoccatPollingRateSelectorClass parent_class;
};

G_DEFINE_TYPE(KoneconfigPollingRateSelector, koneconfig_polling_rate_selector, ROCCAT_POLLING_RATE_SELECTOR_TYPE);

GtkWidget *koneconfig_polling_rate_selector_new(void) {
	KoneconfigPollingRateSelector *polling_rate_selector;

	polling_rate_selector = KONECONFIG_POLLING_RATE_SELECTOR(g_object_new(KONECONFIG_POLLING_RATE_SELECTOR_TYPE,
			"mask", ROCCAT_POLLING_RATE_SELECTOR_125 | ROCCAT_POLLING_RATE_SELECTOR_500 | ROCCAT_POLLING_RATE_SELECTOR_1000,
			NULL));

	return GTK_WIDGET(polling_rate_selector);
}

static void koneconfig_polling_rate_selector_init(KoneconfigPollingRateSelector *polling_rate_selector) {}

static void koneconfig_polling_rate_selector_class_init(KoneconfigPollingRateSelectorClass *klass) {}

void koneconfig_polling_rate_selector_set_value(KoneconfigPollingRateSelector *polling_rate_selector, guint rmp_value) {
	guint roccat_value;

	switch (rmp_value) {
	case KONE_POLLING_RATE_125:
		roccat_value = ROCCAT_POLLING_RATE_SELECTOR_125;
		break;
	case KONE_POLLING_RATE_500:
		roccat_value = ROCCAT_POLLING_RATE_SELECTOR_500;
		break;
	case KONE_POLLING_RATE_1000:
		roccat_value = ROCCAT_POLLING_RATE_SELECTOR_1000;
		break;
	default:
		roccat_value = ROCCAT_POLLING_RATE_SELECTOR_NONE;
		// TODO warning
		break;
	}

	roccat_polling_rate_selector_set_value(ROCCAT_POLLING_RATE_SELECTOR(polling_rate_selector), roccat_value);
}

guint koneconfig_polling_rate_selector_get_value(KoneconfigPollingRateSelector *polling_rate_selector) {
	guint roccat_value;
	guint rmp_value;

	roccat_value = roccat_polling_rate_selector_get_value(ROCCAT_POLLING_RATE_SELECTOR(polling_rate_selector));

	switch (roccat_value) {
	case ROCCAT_POLLING_RATE_SELECTOR_125:
		rmp_value = KONE_POLLING_RATE_125;
		break;
	case ROCCAT_POLLING_RATE_SELECTOR_500:
		rmp_value = KONE_POLLING_RATE_500;
		break;
	case ROCCAT_POLLING_RATE_SELECTOR_1000:
		rmp_value = KONE_POLLING_RATE_1000;
		break;
	case ROCCAT_POLLING_RATE_SELECTOR_NONE:
		rmp_value = KONE_POLLING_RATE_1000;
		// TODO warning
		break;
	default:
		rmp_value = KONE_POLLING_RATE_1000;
		// TODO error
		break;
	}

	return rmp_value;
}
