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

#define LUACONFIG_POLLING_RATE_SELECTOR_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), LUACONFIG_POLLING_RATE_SELECTOR_TYPE, LuaconfigPollingRateSelectorClass))
#define IS_LUACONFIG_POLLING_RATE_SELECTOR_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), LUACONFIG_POLLING_RATE_SELECTOR_TYPE))
#define LUACONFIG_POLLING_RATE_SELECTOR_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), LUACONFIG_POLLING_RATE_SELECTOR_TYPE, LuaconfigPollingRateSelectorPrivate))

typedef struct _LuaconfigPollingRateSelectorClass LuaconfigPollingRateSelectorClass;

struct _LuaconfigPollingRateSelector {
	RoccatPollingRateSelector parent;
};

struct _LuaconfigPollingRateSelectorClass {
	RoccatPollingRateSelectorClass parent_class;
};

G_DEFINE_TYPE(LuaconfigPollingRateSelector, luaconfig_polling_rate_selector, ROCCAT_POLLING_RATE_SELECTOR_TYPE);

GtkWidget *luaconfig_polling_rate_selector_new(void) {
	LuaconfigPollingRateSelector *polling_rate_selector;

	polling_rate_selector = LUACONFIG_POLLING_RATE_SELECTOR(g_object_new(LUACONFIG_POLLING_RATE_SELECTOR_TYPE,
			"mask", ROCCAT_POLLING_RATE_SELECTOR_ALL,
			NULL));

	return GTK_WIDGET(polling_rate_selector);
}

static void luaconfig_polling_rate_selector_init(LuaconfigPollingRateSelector *polling_rate_selector) {}

static void luaconfig_polling_rate_selector_class_init(LuaconfigPollingRateSelectorClass *klass) {}

void luaconfig_polling_rate_selector_set_value(LuaconfigPollingRateSelector *polling_rate_selector, guint rmp_value) {
	guint lua_value;
	guint roccat_value;

	lua_value = lua_value_to_polling_rate(rmp_value);

	switch (lua_value) {
	case LUA_POLLING_RATE_125:
		roccat_value = ROCCAT_POLLING_RATE_SELECTOR_125;
		break;
	case LUA_POLLING_RATE_250:
		roccat_value = ROCCAT_POLLING_RATE_SELECTOR_250;
		break;
	case LUA_POLLING_RATE_500:
		roccat_value = ROCCAT_POLLING_RATE_SELECTOR_500;
		break;
	case LUA_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 luaconfig_polling_rate_selector_get_value(LuaconfigPollingRateSelector *polling_rate_selector) {
	guint roccat_value;
	guint lua_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:
		lua_value = LUA_POLLING_RATE_125;
		break;
	case ROCCAT_POLLING_RATE_SELECTOR_250:
		lua_value = LUA_POLLING_RATE_250;
		break;
	case ROCCAT_POLLING_RATE_SELECTOR_500:
		lua_value = LUA_POLLING_RATE_500;
		break;
	case ROCCAT_POLLING_RATE_SELECTOR_1000:
		lua_value = LUA_POLLING_RATE_1000;
		break;
	case ROCCAT_POLLING_RATE_SELECTOR_NONE:
		lua_value = LUA_POLLING_RATE_1000;
		// TODO warning
		break;
	default:
		lua_value = LUA_POLLING_RATE_1000;
		// TODO error
		break;
	}

	return lua_polling_rate_to_value(lua_value);
}
