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

#define SAVUCONFIG_POLLING_RATE_SELECTOR_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), SAVUCONFIG_POLLING_RATE_SELECTOR_TYPE, SavuconfigPollingRateSelectorClass))
#define IS_SAVUCONFIG_POLLING_RATE_SELECTOR_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), SAVUCONFIG_POLLING_RATE_SELECTOR_TYPE))
#define SAVUCONFIG_POLLING_RATE_SELECTOR_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), SAVUCONFIG_POLLING_RATE_SELECTOR_TYPE, SavuconfigPollingRateSelectorPrivate))

typedef struct _SavuconfigPollingRateSelectorClass SavuconfigPollingRateSelectorClass;

struct _SavuconfigPollingRateSelector {
	RoccatPollingRateSelector parent;
};

struct _SavuconfigPollingRateSelectorClass {
	RoccatPollingRateSelectorClass parent_class;
};

G_DEFINE_TYPE(SavuconfigPollingRateSelector, savuconfig_polling_rate_selector, ROCCAT_POLLING_RATE_SELECTOR_TYPE);

GtkWidget *savuconfig_polling_rate_selector_new(void) {
	SavuconfigPollingRateSelector *polling_rate_selector;

	polling_rate_selector = SAVUCONFIG_POLLING_RATE_SELECTOR(g_object_new(SAVUCONFIG_POLLING_RATE_SELECTOR_TYPE,
			"mask", ROCCAT_POLLING_RATE_SELECTOR_ALL,
			NULL));

	return GTK_WIDGET(polling_rate_selector);
}

static void savuconfig_polling_rate_selector_init(SavuconfigPollingRateSelector *polling_rate_selector) {}

static void savuconfig_polling_rate_selector_class_init(SavuconfigPollingRateSelectorClass *klass) {}

void savuconfig_polling_rate_selector_set_value(SavuconfigPollingRateSelector *polling_rate_selector, guint rmp_value) {
	guint roccat_value;

	switch (rmp_value) {
	case SAVU_GENERAL_POLLING_RATE_125:
		roccat_value = ROCCAT_POLLING_RATE_SELECTOR_125;
		break;
	case SAVU_GENERAL_POLLING_RATE_250:
		roccat_value = ROCCAT_POLLING_RATE_SELECTOR_250;
		break;
	case SAVU_GENERAL_POLLING_RATE_500:
		roccat_value = ROCCAT_POLLING_RATE_SELECTOR_500;
		break;
	case SAVU_GENERAL_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 savuconfig_polling_rate_selector_get_value(SavuconfigPollingRateSelector *polling_rate_selector) {
	guint roccat_value;
	guint savu_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:
		savu_value = SAVU_GENERAL_POLLING_RATE_125;
		break;
	case ROCCAT_POLLING_RATE_SELECTOR_250:
		savu_value = SAVU_GENERAL_POLLING_RATE_250;
		break;
	case ROCCAT_POLLING_RATE_SELECTOR_500:
		savu_value = SAVU_GENERAL_POLLING_RATE_500;
		break;
	case ROCCAT_POLLING_RATE_SELECTOR_1000:
		savu_value = SAVU_GENERAL_POLLING_RATE_1000;
		break;
	case ROCCAT_POLLING_RATE_SELECTOR_NONE:
		savu_value = SAVU_GENERAL_POLLING_RATE_1000;
		// TODO warning
		break;
	default:
		savu_value = SAVU_GENERAL_POLLING_RATE_1000;
		// TODO error
		break;
	}

	return savu_value;
}
