#include <gtk-3.0/gtk/gtk.h>
#include <gtk-3.0/gtk/gtkx.h>
#include <gtk-3.0/gdk/gdk.h>
#include <stdbool.h>
#include "gddcalc.h"
#include "manydays.h"

void buildWidgets(GtkBuilder *);                          // prototype
void colorWidgets();                                      // prototype
void populateLocale();                                   // prototype
void clearLocale();                                      // prototype
void saveLocale();                                       // prototype
void t1_Populate();                                       // prototype
void t1_Depopulate();                                     // prototype
void t2_Populate();                                       // prototype
void t2_Depopulate();                                     // prototype
void t3_Populate();                                       // prototype
void t3_Depopulate();                                     // prototype
void oneDay();                                            // prototype
void manyDays();                                          // prototype
void css_set(GtkCssProvider *, GtkWidget *);              // prototype
int readCBTAT(GtkComboBoxText *);                         // prototype
int getMonthNumber(char *);                               // prototype

int main(int argc, char *argv[]) {

	gtk_init(&argc, &argv);      // initialize Gtk
	builder = gtk_builder_new_from_file ("gddcalc.glade");
	window = GTK_WIDGET(gtk_builder_get_object(builder, "window"));
	g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
	gtk_builder_connect_signals(builder, NULL);
	buildWidgets(builder);      // build all the widgets
	colorWidgets();             // color them

	GtkCssProvider *the_css_provider  = gtk_css_provider_new();
	GFile *the_css_file = g_file_new_for_path("gddcalc.css");
	gtk_css_provider_load_from_file(the_css_provider, the_css_file, NULL);
	gtk_style_context_add_provider_for_screen(gdk_screen_get_default(),
		GTK_STYLE_PROVIDER(the_css_provider),
		GTK_STYLE_PROVIDER_PRIORITY_USER);

	gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
	gtk_widget_show(window);
	in_use = 0;
	error_watch = 1;
	gtk_widget_hide(GTK_WIDGET(t1_frame));
	gtk_widget_hide(GTK_WIDGET(t2_frame));
	gtk_widget_hide(GTK_WIDGET(t3_frame));
	gtk_widget_hide(GTK_WIDGET(locale_frame));
	gtk_widget_hide(GTK_WIDGET(tutorial_frame));
	gtk_widget_hide(GTK_WIDGET(about_frame));
	gtk_widget_hide(GTK_WIDGET(error_frame));
	
	gtk_main();
	return EXIT_SUCCESS;
}

/* Here follow the callback routines that respond to actions
 * initiated in the displays.
 */

//---------------------------------------------------------- locale
void on_locale_activate(GtkMenuItem *m) {
	if (in_use == 0) {
		gtk_widget_show(GTK_WIDGET(locale_frame));
		gtk_widget_set_sensitive(GTK_WIDGET(locale_clear), TRUE);
		gtk_widget_set_sensitive(GTK_WIDGET(locale_save), TRUE);
		gtk_widget_set_sensitive(GTK_WIDGET(locale_close), TRUE);
		populateLocale();
		in_use = 1;
	}
}

void on_locale_clear_clicked(GtkMenuItem *m) {
	clearLocale();
}

void on_locale_save_clicked(GtkMenuItem *q) {
	saveLocale();
}

void on_locale_close_clicked(GtkMenuItem *q) {
	 gtk_widget_hide(GTK_WIDGET(locale_frame));
	 in_use = 0;
}

void on_quit_activate(GtkMenuItem *q) {
	 gtk_main_quit();
}
// --------------------------------------------------------- calculate GDD
void on_oneday_activate(GtkMenuItem *q) {
	if (in_use == 0) oneDay();
	in_use = 1;
}

void on_months_activate(GtkMenuItem *q) {
	if (in_use == 0) manyDays();
	in_use = 1;
}
void on_error_close_clicked(GtkMenuItem *q) {
	gtk_widget_hide(GTK_WIDGET(error_frame));
	error_watch = 0;
	if (oneormany == 1)	gtk_widget_show(GTK_WIDGET(c1_window));
	if (oneormany == 2) gtk_widget_show(GTK_WIDGET(c2_window));
}
// --------------------------------------------------------- test 1 instant
void on_testoneinstant_activate(GtkMenuItem *q) {
	if (in_use == 0) {
		gtk_widget_show(GTK_WIDGET(t1_frame));
		gtk_widget_set_sensitive(GTK_WIDGET(t1_close), FALSE);
		gtk_widget_set_sensitive(GTK_WIDGET(t1_calculate), TRUE);
		gtk_label_set_text(GTK_LABEL(t1_explanation), (const gchar* ) "");
		in_use = 1;
	}
}

void on_t1_calculate_clicked(GtkMenuItem *q) {
	t1_Populate();
	gtk_widget_set_sensitive(GTK_WIDGET(t1_close), TRUE);
	gtk_widget_set_sensitive(GTK_WIDGET(t1_calculate), FALSE);
	char bfr[96];
	strcpy(bfr,
		"1st value above is calculated here, 2nd (in parens) is correct");
	gtk_label_set_text(GTK_LABEL(t1_explanation), (const gchar* ) bfr);
}

void on_t1_close_clicked(GtkMenuItem *q) {
	 t1_Depopulate();
	 gtk_widget_hide(GTK_WIDGET(t1_frame));
	 in_use = 0;
}
// ----------------------------------------------------------- test 1 day
void on_testoneday_activate(GtkMenuItem *q) {
	if (in_use == 0) {
		gtk_widget_show(GTK_WIDGET(t2_frame));
		gtk_widget_set_sensitive(GTK_WIDGET(t2_close), FALSE);
		in_use = 1;
	}
}

void on_t2_calculate_clicked(GtkMenuItem *q) {
	t2_Populate();
	gtk_widget_set_sensitive(GTK_WIDGET(t2_close), TRUE);
	gtk_widget_set_sensitive(GTK_WIDGET(t2_calculate), FALSE);
}

void on_t2_close_clicked(GtkMenuItem *q) {
	 t2_Depopulate();
	 gtk_widget_hide(GTK_WIDGET(t2_frame));
	 in_use = 0;
}
// --------------------------------------------------- test growing season
void on_testgrowingseason_activate(GtkMenuItem *q) {
	if (in_use == 0) {
		gtk_widget_show(GTK_WIDGET(t3_frame));
		gtk_widget_set_sensitive(GTK_WIDGET(t3_close), FALSE);
		in_use = 1;
	}
}

void on_t3_calculate_clicked(GtkMenuItem *q) {
	t3_Populate();
	gtk_widget_set_sensitive(GTK_WIDGET(t3_close), TRUE);
	gtk_widget_set_sensitive(GTK_WIDGET(t3_calculate), FALSE);
}

void on_t3_close_clicked(GtkMenuItem *q) {
	t3_Depopulate();
	gtk_widget_hide(GTK_WIDGET(t3_frame));
	in_use = 0;
}
// -------------------------------------------------------- tutorial
void on_tutorial_activate(GtkMenuItem *q) {
	 if (in_use == 0) {
		 gtk_widget_show(GTK_WIDGET(tutorial_frame));
		 gtk_widget_set_sensitive(GTK_WIDGET(tutorial_dismiss), TRUE);
		 in_use = 1;
	 }
}

void on_tutorial_dismiss_clicked(GtkMenuItem *q) {
	gtk_widget_hide(GTK_WIDGET(tutorial_frame));
	in_use = 0;
}
//--------------------------------------------------------- about
void on_about_activate(GtkMenuItem *q) {
	 if (in_use == 0) {
		 gtk_widget_show(GTK_WIDGET(about_frame));
		 gtk_widget_set_sensitive(GTK_WIDGET(about_dismiss), TRUE);
		 in_use = 1;
	 }
}

void on_about_dismiss_clicked(GtkMenuItem *q) {
	gtk_widget_hide(GTK_WIDGET(about_frame));
	in_use = 0;
}
