/*
 * mandelbrot-zoom.c
 *
 *  Created on: 13.01.2018
 *      Author: Superleo1810
 */

#include "mandelbrot-zoom.h"

int main(int argc, char **argv)
{
	GtkBuilder *builder;
	gtk_init(&argc, &argv);
	glutInit(&argc, argv);
	builder = gtk_builder_new();
	gtk_builder_add_from_file (builder, "glade/settings.glade", NULL); // TODO: Glade-Zeug mit in die exe

	ui_settings.settings = GTK_WINDOW(gtk_builder_get_object(builder, "settings"));
	//gtk_builder_connect_signals(builder, NULL);
	ui_settings.setCombo = GTK_COMBO_BOX_TEXT(gtk_builder_get_object(builder, "setCombo"));
	ui_settings.iterationsSp = GTK_SPIN_BUTTON(gtk_builder_get_object(builder, "iterationsSp"));
	ui_settings.threadsSp = GTK_SPIN_BUTTON(gtk_builder_get_object(builder, "threadsSp"));
	ui_settings.colorFromBtn = GTK_BUTTON(gtk_builder_get_object(builder, "colorFromBtn"));
	ui_settings.colorToBtn = GTK_BUTTON(gtk_builder_get_object(builder, "colorToBtn"));
	ui_settings.zoomToXEntry = GTK_ENTRY(gtk_builder_get_object(builder, "zoomToXEntry"));
	ui_settings.zoomToYEntry = GTK_ENTRY(gtk_builder_get_object(builder, "zoomToYEntry"));
	ui_settings.speedEntry = GTK_ENTRY(gtk_builder_get_object(builder, "speedEntry"));
	ui_settings.modeCPURd = GTK_RADIO_BUTTON(gtk_builder_get_object(builder, "modeCPURd"));
	ui_settings.modeGPURd = GTK_RADIO_BUTTON(gtk_builder_get_object(builder, "modeGPURd"));
	ui_settings.fpuCombo = GTK_COMBO_BOX_TEXT(gtk_builder_get_object(builder, "fpuCombo"));
	ui_settings.fmaCb = GTK_COMBO_BOX_TEXT(gtk_builder_get_object(builder, "fmaCb"));
	ui_settings.exportCb = GTK_CHECK_BUTTON(gtk_builder_get_object(builder, "exportCb"));
	ui_settings.gifRd = GTK_RADIO_BUTTON(gtk_builder_get_object(builder, "gifRd"));
	ui_settings.widthSp = GTK_SPIN_BUTTON(gtk_builder_get_object(builder, "widthSp"));
	ui_settings.heightSp = GTK_SPIN_BUTTON(gtk_builder_get_object(builder, "heightSp"));
	ui_settings.fpsRenderSp = GTK_SPIN_BUTTON(gtk_builder_get_object(builder, "fpsRenderSp"));
	ui_settings.fpsVideoSp = GTK_SPIN_BUTTON(gtk_builder_get_object(builder, "fpsVideoSp"));
	ui_settings.bitrateSp = GTK_SPIN_BUTTON(gtk_builder_get_object(builder, "bitrateSp"));
	ui_settings.exportTf = GTK_ENTRY(gtk_builder_get_object(builder, "exportTf"));
	ui_settings.startBtn = GTK_BUTTON(gtk_builder_get_object(builder, "startBtn"));
	ui_settings.exitBtn = GTK_BUTTON(gtk_builder_get_object(builder, "exitBtn"));
	ui_settings.colorDialog = GTK_COLOR_SELECTION_DIALOG(gtk_builder_get_object(builder, "colorDialog"));

	gtk_spin_button_set_increments(ui_settings.iterationsSp, 1, 2);
	gtk_spin_button_set_increments(ui_settings.threadsSp, 1, 2);
	gtk_spin_button_set_increments(ui_settings.widthSp, 1, 2);
	gtk_spin_button_set_increments(ui_settings.heightSp, 1, 2);
	gtk_spin_button_set_increments(ui_settings.fpsRenderSp, 1, 2);
	gtk_spin_button_set_increments(ui_settings.fpsVideoSp, 1, 2);
	gtk_spin_button_set_increments(ui_settings.bitrateSp, 1, 2);

	gtk_spin_button_set_value(ui_settings.iterationsSp, 1000);
	gtk_spin_button_set_value(ui_settings.threadsSp, 4);
	gtk_spin_button_set_value(ui_settings.widthSp, 1920);
	gtk_spin_button_set_value(ui_settings.heightSp, 1080);
	gtk_spin_button_set_value(ui_settings.fpsRenderSp, 30);
	gtk_spin_button_set_value(ui_settings.fpsVideoSp, 30);
	gtk_spin_button_set_value(ui_settings.bitrateSp, 1000);

	gtk_spin_button_set_range(ui_settings.iterationsSp, 1, 16777215); // 3 bytes � 8 bit = 24 bit
	gtk_spin_button_set_range(ui_settings.threadsSp, 1, 255);
	gtk_spin_button_set_range(ui_settings.widthSp, 1, 1920);
	gtk_spin_button_set_range(ui_settings.heightSp, 1, 1080);
	gtk_spin_button_set_range(ui_settings.fpsRenderSp, 1, 60);
	gtk_spin_button_set_range(ui_settings.fpsVideoSp, 1, 60);
	gtk_spin_button_set_range(ui_settings.bitrateSp, 1, 65535);

	gtk_combo_box_text_append(ui_settings.setCombo, NULL, "Mandelbrot");
	gtk_combo_box_text_append(ui_settings.setCombo, NULL, "Julia");

	gtk_combo_box_text_append(ui_settings.fpuCombo, NULL, "32 bit");
	gtk_combo_box_text_append(ui_settings.fpuCombo, NULL, "64 bit");
	gtk_combo_box_text_append(ui_settings.fpuCombo, NULL, "128 bit");

	g_signal_connect(ui_settings.colorFromBtn, "clicked", G_CALLBACK(on_colorFromBtn_clicked), NULL);
	g_signal_connect(ui_settings.colorToBtn, "clicked", G_CALLBACK(on_colorToBtn_clicked), NULL);
	g_signal_connect(ui_settings.startBtn, "clicked", G_CALLBACK(on_startBtn_clicked), NULL);
	g_signal_connect(ui_settings.exitBtn, "clicked", G_CALLBACK(on_exitBtn_clicked), NULL);

	g_object_unref(builder);
	gtk_widget_show(GTK_WIDGET(ui_settings.settings));
	gtk_main();
	return 0;
}

void on_colorFromBtn_clicked()
{
	currentColor = COLOR_FROM;
	gtk_dialog_run(ui_settings.colorDialog);
}

void on_colorToBtn_clicked()
{
	currentColor = COLOR_TO;
	gtk_dialog_run(ui_settings.colorDialog);
}

void on_startBtn_clicked()
{
	config.config_cpu.iterations = gtk_spin_button_get_value(ui_settings.iterationsSp);
	config.config_opencl.iterations = gtk_spin_button_get_value(ui_settings.iterationsSp);

	config.config_cpu.threads = gtk_spin_button_get_value(ui_settings.threadsSp);

	config.config_opencl.fma = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(ui_settings.fmaCb));

	config.video = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(ui_settings.exportCb));

	config.path = CHAR_PTR(gtk_entry_get_text(ui_settings.exportTf));

	config.width = gtk_spin_button_get_value(ui_settings.widthSp);
	config.config_cpu.width = gtk_spin_button_get_value(ui_settings.widthSp);
	config.config_opencl.width = gtk_spin_button_get_value(ui_settings.widthSp);

	config.height = gtk_spin_button_get_value(ui_settings.heightSp);
	config.config_cpu.height = gtk_spin_button_get_value(ui_settings.heightSp);
	config.config_opencl.height = gtk_spin_button_get_value(ui_settings.heightSp);

	config.config_cpu.renderFPS = gtk_spin_button_get_value(ui_settings.fpsRenderSp);
	config.config_opencl.renderFPS = gtk_spin_button_get_value(ui_settings.fpsRenderSp);

	config.videoFPS = gtk_spin_button_get_value(ui_settings.fpsVideoSp);

	config.bitrate = gtk_spin_button_get_value(ui_settings.bitrateSp);

	double x, y, speed;
	sscanf(gtk_entry_get_text(ui_settings.zoomToXEntry), "%lf", &x);
	sscanf(gtk_entry_get_text(ui_settings.zoomToYEntry), "%lf", &y);
	sscanf(gtk_entry_get_text(ui_settings.speedEntry), "%lf", &speed);
	config.config_cpu.to_x = x;
	config.config_cpu.to_y = y;
	config.config_cpu.speed = speed;
	config.config_opencl.to_x = x;
	config.config_opencl.to_y = y;
	config.config_opencl.speed = speed;

	config.mode = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(ui_settings.modeGPURd));
	//config.mode = MODE_CPU;

	if (strcmp(gtk_combo_box_text_get_active_text(ui_settings.setCombo), "Mandelbrot") == 0)
	{
		config.config_cpu.set_func = mandelbrot_r;
		config.config_opencl.set_func = SFUNC_MANDELBROT;
	}
	else if (strcmp(gtk_combo_box_text_get_active_text(ui_settings.setCombo), "Julia") == 0)
	{
		config.config_cpu.set_func = julia;
		config.config_opencl.set_func = SFUNC_JULIA;
	}

	if (strcmp(gtk_combo_box_text_get_active_text(ui_settings.fpuCombo), "32 bit") == 0)
	{
		config.config_opencl.fpu = OPENCL_FPU_32;
	}
	else if (strcmp(gtk_combo_box_text_get_active_text(ui_settings.fpuCombo), "64 bit") == 0)
	{
		config.config_opencl.fpu = OPENCL_FPU_64;
	}
	else if (strcmp(gtk_combo_box_text_get_active_text(ui_settings.fpuCombo), "128 bit") == 0)
	{
		config.config_opencl.fpu = OPENCL_FPU_128;
	}

	init_render(&config);
	show_render();
}

void on_exitBtn_clicked()
{
	gtk_main_quit();
}