/*
 * 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 "roccat_macro_editor_dialog.h"
#include "roccat_macro_editor_dialog_menu_bar.h"
#include "roccat_macro_editor.h"
#include "roccat_save_dialog.h"
#include "roccat_warning_dialog.h"

#define ROCCAT_MACRO_EDITOR_DIALOG_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), ROCCAT_MACRO_EDITOR_DIALOG_TYPE, RoccatMacroEditorDialogClass))
#define IS_ROCCAT_MACRO_EDITOR_DIALOG_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), ROCCAT_MACRO_EDITOR_DIALOG_TYPE))
#define ROCCAT_MACRO_EDITOR_DIALOG_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), ROCCAT_MACRO_EDITOR_DIALOG_TYPE, RoccatMacroEditorDialogPrivate))

typedef struct _RoccatMacroEditorDialogClass RoccatMacroEditorDialogClass;

struct _RoccatMacroEditorDialogClass {
	GtkDialogClass parent_class;
};

struct _RoccatMacroEditorDialogPrivate {
	RoccatMacroEditor *macro_editor;
};

G_DEFINE_TYPE(RoccatMacroEditorDialog, roccat_macro_editor_dialog, GTK_TYPE_DIALOG);

void roccat_macro_editor_dialog_add_macros(RoccatMacroEditorDialog *dialog, RoccatMacros *roccat_macros) {
	roccat_macro_editor_add_macros(dialog->priv->macro_editor, roccat_macros);
}

RoccatMacros *roccat_macro_editor_dialog_get_macros(RoccatMacroEditorDialog *dialog) {
	return roccat_macro_editor_get_macros(dialog->priv->macro_editor);
}

void roccat_macro_editor_dialog_load_macros(RoccatMacroEditorDialog *dialog) {
	RoccatMacros *roccat_macros;
	GError *error = NULL;

	roccat_macros = roccat_macros_load();
	if (error) {
		g_warning("Error loading macros: %s", error->message);
		g_error_free(error);
		return;
	}

	roccat_macro_editor_clear(dialog->priv->macro_editor);
	roccat_macro_editor_add_macros(dialog->priv->macro_editor, roccat_macros);
	roccat_macro_editor_set_modified(dialog->priv->macro_editor, FALSE);
	roccat_macros_free(roccat_macros);
}

gboolean roccat_macro_editor_dialog_save_macros(RoccatMacroEditorDialog *dialog) {
	RoccatMacros *roccat_macros;
	gboolean result;
	GError *error = NULL;

	roccat_macros = roccat_macro_editor_get_macros(dialog->priv->macro_editor);
	result = roccat_macros_save(roccat_macros, &error);
	roccat_macros_free(roccat_macros);

	if (error) {
		roccat_warning_dialog(GTK_WINDOW(dialog), "Error saving macros", error->message);
		g_clear_error(&error);
	} else
		roccat_macro_editor_set_modified(dialog->priv->macro_editor, FALSE);

	return result;
}

void roccat_macro_editor_dialog(GtkWindow *parent) {
	RoccatMacroEditorDialog *dialog;

	dialog = ROCCAT_MACRO_EDITOR_DIALOG(roccat_macro_editor_dialog_new(parent));

	roccat_macro_editor_dialog_load_macros(dialog);

	if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT)
		roccat_macro_editor_dialog_save_macros(dialog);
	else {
		if (roccat_macro_editor_get_modified(dialog->priv->macro_editor)) {
			if (roccat_save_unsaved_dialog(parent, FALSE) == GTK_RESPONSE_ACCEPT)
				roccat_macro_editor_dialog_save_macros(dialog);
		}
	}

	gtk_widget_destroy(GTK_WIDGET(dialog));
}

GtkWidget *roccat_macro_editor_dialog_new(GtkWindow *parent) {
	RoccatMacroEditorDialog *macro_editor_dialog;

	macro_editor_dialog = g_object_new(ROCCAT_MACRO_EDITOR_DIALOG_TYPE, NULL);

	gtk_dialog_add_buttons(GTK_DIALOG(macro_editor_dialog),
			GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
			GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
			NULL);
	gtk_window_set_title(GTK_WINDOW(macro_editor_dialog), "Edit macros");
	gtk_window_set_transient_for(GTK_WINDOW(macro_editor_dialog), parent);
	gtk_window_set_modal(GTK_WINDOW(macro_editor_dialog), TRUE);
	gtk_window_set_destroy_with_parent(GTK_WINDOW(macro_editor_dialog), TRUE);
	gtk_window_set_type_hint(GTK_WINDOW(macro_editor_dialog), GDK_WINDOW_TYPE_HINT_NORMAL);
	gtk_window_resize(GTK_WINDOW(macro_editor_dialog), 640, 480);

	return GTK_WIDGET(macro_editor_dialog);
}

static void roccat_macro_editor_dialog_init(RoccatMacroEditorDialog *macro_editor_dialog) {
	RoccatMacroEditorDialogPrivate *priv = ROCCAT_MACRO_EDITOR_DIALOG_GET_PRIVATE(macro_editor_dialog);
	GtkVBox *content_area;
	GtkWidget *menu_bar;

	macro_editor_dialog->priv = priv;

	content_area = GTK_VBOX(gtk_dialog_get_content_area(GTK_DIALOG(macro_editor_dialog)));

	priv->macro_editor = ROCCAT_MACRO_EDITOR(roccat_macro_editor_new());
	menu_bar = roccat_macro_editor_dialog_menu_bar_new(macro_editor_dialog);

	gtk_box_pack_start(GTK_BOX(content_area), menu_bar, FALSE, FALSE, 0);
	gtk_box_pack_start(GTK_BOX(content_area), GTK_WIDGET(priv->macro_editor), TRUE, TRUE, 0);

	gtk_widget_show_all(menu_bar);
	gtk_widget_show(GTK_WIDGET(priv->macro_editor));
}

static void roccat_macro_editor_dialog_class_init(RoccatMacroEditorDialogClass *klass) {
	g_type_class_add_private(klass, sizeof(RoccatMacroEditorDialogPrivate));
}
