#include "errordialog.h"

#include <iostream>

namespace kek::ErrorDialog {

#ifndef KEK_DISABLE_ERRORDIALOG

#ifdef KEKENGINE_TARGET_WINDOWS

#define WIN32_LEAN_AND_MEAN
#include <windows.h>

void showInfo(std::string message) {
	UINT flags = MB_TASKMODAL | MB_ICONINFORMATION | MB_OK;
	MessageBox(nullptr, message.c_str(), "KekEngine Info", flags);
}

void showWarning(std::string message) {
	UINT flags = MB_TASKMODAL | MB_ICONWARNING | MB_OK;
	MessageBox(nullptr, message.c_str(), "KekEngine Warning", flags);
}

void showError(std::string message) {
	UINT flags = MB_TASKMODAL | MB_ICONERROR | MB_OK;
	MessageBox(nullptr, message.c_str(), "KekEngine Error", flags);
}

#endif

#ifdef KEKENGINE_TARGET_LINUX

#include <gtk/gtk.h>

static void show(GtkMessageType type, GtkButtonsType buttons, const char *title, const char *message) {
	if(!gtk_init_check(0, nullptr)) {
		std::cout << "Failed init GTK for dialogs. Dialog message: " << message << std::endl;
		return;
	}

	GtkWidget *w = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	GtkWidget *d = gtk_message_dialog_new(GTK_WINDOW(w), GTK_DIALOG_MODAL, type, buttons, "%s", message);
	gtk_window_set_title(GTK_WINDOW(w), title);
	gtk_dialog_run(GTK_DIALOG(d));
	gtk_widget_destroy(w);
	gtk_widget_destroy(d);
	while(g_main_context_iteration(nullptr, false));
}

void showInfo(std::string message) {
	show(GTK_MESSAGE_INFO, GTK_BUTTONS_OK, "KekEngine Info", message.c_str());
}

void showWarning(std::string message) {
	show(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, "KekEngine Warning", message.c_str());
}

void showError(std::string message) {
	show(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "KekEngine Error", message.c_str());
}

#endif

#else // ifndef KEK_DISABLE_ERRORDIALOG

void showInfo(std::string message) {
	std::cout << "Info: " << message << std::endl;
}

void showWarning(std::string message) {
	std::cout << "Warn: " << message << std::endl;
}

void showError(std::string message) {
	std::cerr << "Error: " << message << std::endl;
}

#endif

}
