/*
 * 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 "ryosconfig_key_mask_selector.h"
#include "roccat_key_mask_selector.h"

#define RYOSCONFIG_KEY_MASK_SELECTOR_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), RYOSCONFIG_KEY_MASK_SELECTOR_TYPE, RyosconfigKeyMaskSelectorClass))
#define IS_RYOSCONFIG_KEY_MASK_SELECTOR_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), RYOSCONFIG_KEY_MASK_SELECTOR_TYPE))

typedef struct _RyosconfigKeyMaskSelectorClass RyosconfigKeyMaskSelectorClass;

struct _RyosconfigKeyMaskSelector {
	RoccatKeyMaskSelector parent;
};

struct _RyosconfigKeyMaskSelectorClass {
	RoccatKeyMaskSelectorClass parent_class;
};

G_DEFINE_TYPE(RyosconfigKeyMaskSelector, ryosconfig_key_mask_selector, ROCCAT_KEY_MASK_SELECTOR_TYPE);

enum {
	COUNT = 5,
};

static gchar *names[COUNT] = {
	"Tab",
	"Left windows",
	"Right windows",
	"Application",
	"Left shift",
};

static guint values[COUNT] = {
	RYOS_KEY_MASK_BIT_TAB,
	RYOS_KEY_MASK_BIT_LEFT_WIN,
	RYOS_KEY_MASK_BIT_RIGHT_WIN,
	RYOS_KEY_MASK_BIT_APP,
	RYOS_KEY_MASK_BIT_LEFT_SHIFT,
};

void ryosconfig_key_mask_selector_set_from_rkp(RyosconfigKeyMaskSelector *selector, RyosRkp *rkp) {
	RoccatKeyMaskSelector *roccat_selector = ROCCAT_KEY_MASK_SELECTOR(selector);
	guint i;
	gboolean state;

	for (i = 0; i < COUNT; ++i) {
		state = roccat_get_bit(rkp->data.key_mask.mask, values[i]);
		roccat_key_mask_selector_set_active(roccat_selector, i, state);
	}
}

void ryosconfig_key_mask_selector_update_rkp(RyosconfigKeyMaskSelector *selector, RyosRkp *rkp) {
	RoccatKeyMaskSelector *roccat_selector = ROCCAT_KEY_MASK_SELECTOR(selector);
	guint i;
	gboolean state;
	RyosKeyMask *mask;

	mask = ryos_rkp_get_key_mask(rkp);

	for (i = 0; i < COUNT; ++i) {
		state = roccat_key_mask_selector_get_active(roccat_selector, i);
		roccat_set_bit(&mask->mask, values[i], state);
	}

	ryos_rkp_set_key_mask(rkp, mask);
	g_free(mask);
}

GtkWidget *ryosconfig_key_mask_selector_new(void) {
	RyosconfigKeyMaskSelector *ryosconfig_key_mask_selector;

	ryosconfig_key_mask_selector = RYOSCONFIG_KEY_MASK_SELECTOR(g_object_new(RYOSCONFIG_KEY_MASK_SELECTOR_TYPE,
			"count", COUNT,
			"values", names,
			NULL));

	return GTK_WIDGET(ryosconfig_key_mask_selector);
}

static void ryosconfig_key_mask_selector_init(RyosconfigKeyMaskSelector *ryosconfig_key_mask_selector) {}

static void ryosconfig_key_mask_selector_class_init(RyosconfigKeyMaskSelectorClass *klass) {}
