#include <gtk/gtk.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "gddcalc.h"
#include "manydays.h"

void c2_Initialize();                                          // prototype
void c2_ShowResult(c2_widgets *w);                             // prototype
int c2_CheckEntries(c2_widgets *w);                            // prototype

static const gchar *css_data =
"#grid_frame {"
"  border: 2px solid white;"
"  padding: 10px;"        /* space between border and grid */
"}";

static void on_c2_month_changed(GtkComboBox *combo, gpointer user_data) {
	c2_widgets *w = user_data;
	gint start_idx = gtk_combo_box_get_active(GTK_COMBO_BOX(w->cbtxt_startmonth));
	gint stop_idx  = gtk_combo_box_get_active(GTK_COMBO_BOX(w->cbtxt_stopmonth));
	gboolean both_set = (start_idx >= 0 && stop_idx >= 0);
	gtk_widget_set_sensitive(w->btn_start, both_set);
	gtk_widget_set_visible(GTK_WIDGET(w->lbl_month_error), FALSE);
}

/* ==================== Build the input grid (c2_grid1) ========================*/

static GtkWidget *create_c2_grid1(c2_widgets *w) {

	void c2_initialze();                                      // prototype

	GtkWidget *frame;
	GtkWidget *grid;
	GtkCssProvider *provider;
	GtkStyleContext *context;
	char *lbl1[] = {"latitude", "longitude", "elevation", "aspect", "tilt"};
	char *lbl2[] = {"day", "month", "year"};
	char *mthname[] = {"January", "February", "March", "April", "May",
		"June", "July", "August", "September", "October", "November",
		"December"};
	char *bfr = malloc(128);

/* Setup for labels in the upper grid (c2_grid1) */

	GtkWidget *label;
	GtkStyleContext *ctx;
	GtkCssProvider *title_provider = gtk_css_provider_new();
	gtk_css_provider_load_from_data(title_provider,
			".section-title { "
			"  font-family: Sans; "
			"  font-weight: bold; "
			"  font-size: 12pt; "
			"}\n", -1, NULL);

	GdkScreen *screen = gdk_screen_get_default();
	gtk_style_context_add_provider_for_screen(screen,
			GTK_STYLE_PROVIDER(title_provider),
			GTK_STYLE_PROVIDER_PRIORITY_APPLICATION
	);

	GtkCssProvider *label_provider = gtk_css_provider_new();
	gtk_css_provider_load_from_data(label_provider,
			".normal-label { "
			"  font-family: Sans; "
			"  font-weight: bold; "
			"  font-size: 10pt; "
			"}\n", -1, NULL);

	gtk_style_context_add_provider_for_screen(
			screen,
			GTK_STYLE_PROVIDER(label_provider),
			GTK_STYLE_PROVIDER_PRIORITY_APPLICATION
	);

/* Read in the active variables from locale.txt */
	c2_Initialize();
	
/* Frame will hold the grid and get the border */
	frame = gtk_frame_new(NULL);
	gtk_widget_set_name(frame, "grid_frame");  /* used by CSS selector */

/* Remove default frame shadow to avoid a double effect */
	gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_NONE);

/* Create the bordered grid */
	grid = gtk_grid_new();
	gtk_grid_set_row_spacing(GTK_GRID(grid), 10);
	gtk_grid_set_column_spacing(GTK_GRID(grid), 10);

/* Column 0 labels in rows 2 through 6 */
	for (int row = 2; row < 7; ++row) {
		label = gtk_label_new(lbl1[row - 2]);
		ctx = gtk_widget_get_style_context(label);
		gtk_style_context_add_class(ctx, "normal-label");
		gtk_grid_attach(GTK_GRID(grid), label, 0, row, 1, 1);
	}

/* Column 1 label in row 1 */
	label = gtk_label_new("location");
	ctx = gtk_widget_get_style_context(label);
	gtk_style_context_add_class(ctx, "normal-label");
	gtk_grid_attach(GTK_GRID(grid), label, 1, 1, 1, 1);

/* Column 1 entries in rows 2 through 6 */
	w->entry_lat = gtk_entry_new();
	gtk_grid_attach(GTK_GRID(grid), w->entry_lat, 1, 2, 1, 1);
	if (haveprefs) {sprintf(bfr, "%8.4f", active_latitude);}
	else {strcpy(bfr, "");}
	gtk_entry_set_text(GTK_ENTRY(w->entry_lat), (gchar*) bfr);      // lat

	w->entry_lon = gtk_entry_new();
	gtk_grid_attach(GTK_GRID(grid), w->entry_lon, 1, 3, 1, 1);
	if (haveprefs) {sprintf(bfr, "%8.4f", active_longitude);}
	else {strcpy(bfr, "");}
	gtk_entry_set_text(GTK_ENTRY(w->entry_lon), (gchar*) bfr);      // lon

	w->entry_elev = gtk_entry_new();
	gtk_grid_attach(GTK_GRID(grid), w->entry_elev, 1, 4, 1, 1);
	if (haveprefs) {sprintf(bfr, "%8.4f", active_elev);}
	else {strcpy(bfr, "");}
	gtk_entry_set_text(GTK_ENTRY(w->entry_elev), (gchar*) bfr);     // elev

	w->entry_aspect = gtk_entry_new();
	gtk_grid_attach(GTK_GRID(grid), w->entry_aspect, 1, 5, 1, 1);
	if (haveprefs) {sprintf(bfr, "%8.4f", active_aspect);}
	else {strcpy(bfr, "");}
	gtk_entry_set_text(GTK_ENTRY (w->entry_aspect), (gchar*) bfr);   // aspect

	w->entry_tilt = gtk_entry_new();
	gtk_grid_attach(GTK_GRID(grid), w->entry_tilt, 1, 6, 1, 1);
	if (haveprefs) {sprintf(bfr, "%8.4f", active_tilt);}
	else {strcpy(bfr, "");}
	gtk_entry_set_text(GTK_ENTRY (w->entry_tilt), (gchar*) bfr);     // tilt

/* Column 2 labels in rows 2, 3, and 4 */
	for (int row = 2; row < 5; ++row) {
		label = gtk_label_new(lbl2[row - 2]);
		ctx = gtk_widget_get_style_context(label);
		gtk_style_context_add_class(ctx, "normal-label");
		gtk_grid_attach(GTK_GRID(grid), label, 2, row, 1, 1);
	}

/* Column 3 labels in row 1 and row 6 */
	label = gtk_label_new("start");
	ctx = gtk_widget_get_style_context(label);
	gtk_style_context_add_class(ctx, "normal-label");
	gtk_grid_attach(GTK_GRID(grid), label, 3, 1, 1, 1);

	w->lbl_month_error = gtk_label_new("BAD MONTH ORDER");
	ctx = gtk_widget_get_style_context(w->lbl_month_error);
	gtk_style_context_add_class(ctx, "normal-label");
	gtk_grid_attach(GTK_GRID(grid), w->lbl_month_error, 3, 5, 2, 1);
	gtk_widget_set_visible(GTK_WIDGET(w->lbl_month_error), FALSE);

/* Column 3 entries in rows 2 through 5 */
	w->entry_startday = gtk_entry_new();
	gtk_entry_set_width_chars(GTK_ENTRY(w->entry_startday), 14);
	gtk_entry_set_max_width_chars(GTK_ENTRY(w->entry_startday), 14);
	gtk_grid_attach(GTK_GRID(grid), w->entry_startday, 3, 2, 1, 1);
	gtk_widget_grab_focus(w->entry_startday);

	w->cbtxt_startmonth = GTK_COMBO_BOX_TEXT(gtk_combo_box_text_new());
	gtk_grid_attach(GTK_GRID(grid), GTK_WIDGET(w->cbtxt_startmonth), 3, 3, 1, 1);

	w->entry_year = gtk_entry_new();
	gtk_entry_set_alignment(GTK_ENTRY(w->entry_year), 0.5f);
	gtk_grid_attach(GTK_GRID(grid), w->entry_year, 3, 4, 2, 1);

/* Column 4 entries in rows 2 through 4; column 4 label in row 1 */

	label = gtk_label_new("stop");
	ctx = gtk_widget_get_style_context(label);
	gtk_style_context_add_class(ctx, "normal-label");
	gtk_grid_attach(GTK_GRID(grid), label, 4, 1, 1, 1);

	w->entry_stopday = gtk_entry_new();
	gtk_entry_set_width_chars(GTK_ENTRY(w->entry_stopday), 14);
	gtk_entry_set_max_width_chars(GTK_ENTRY(w->entry_stopday), 14);
	gtk_grid_attach(GTK_GRID(grid), w->entry_stopday, 4, 2, 1, 1);
	
	w->cbtxt_stopmonth = GTK_COMBO_BOX_TEXT(gtk_combo_box_text_new());
	gtk_grid_attach(GTK_GRID(grid), GTK_WIDGET(w->cbtxt_stopmonth), 4, 3, 1, 1);

/* Fill in the month combos */
	for (int i = 0; i < 12; i++) {
		gtk_combo_box_text_append_text(w->cbtxt_startmonth, mthname[i]);
		gtk_combo_box_text_append_text(w->cbtxt_stopmonth, mthname[i]);
	}

/* Connect combo changed signals using w as user_data */
	g_signal_connect(w->cbtxt_startmonth, "changed",
					 G_CALLBACK(on_c2_month_changed), w);
	g_signal_connect(w->cbtxt_stopmonth, "changed",
					 G_CALLBACK(on_c2_month_changed), w);

/* Put the grid inside the frame */
	gtk_container_add(GTK_CONTAINER(frame), grid);

/* Apply CSS to give the frame a border */
	provider = gtk_css_provider_new();
	gtk_css_provider_load_from_data(provider, css_data, -1, NULL);

	context = gtk_widget_get_style_context(frame);
	gtk_style_context_add_provider(
		context,
		GTK_STYLE_PROVIDER(provider),
		GTK_STYLE_PROVIDER_PRIORITY_USER
	);
	g_object_unref(provider);

	free(bfr);
	return frame;
}
/* ================== Build the output grid (c2_grid2) ====================*/

static GtkWidget *create_c2_grid2(c2_widgets *w) {
	GtkWidget *frame;
	GtkWidget *grid;
	GtkCssProvider *provider;
	GtkStyleContext *context;
	
	char *lbl1[] = {"month", "days"};
	char *lbl2[] = {"irradiance", "deg days"};

/* Setup for labels in the lower grid (c2_grid2) */
	
	GtkWidget *label;
	GtkStyleContext *ctx;
	GtkCssProvider *title_provider = gtk_css_provider_new();
	gtk_css_provider_load_from_data(title_provider,
			".section-title { "
			"  font-family: Sans; "
			"  font-weight: bold; "
			"  font-size: 12pt; "
			"}\n", -1, NULL);
	
	GdkScreen *screen = gdk_screen_get_default();
	gtk_style_context_add_provider_for_screen(
			screen,
			GTK_STYLE_PROVIDER(title_provider),
			GTK_STYLE_PROVIDER_PRIORITY_APPLICATION
	);
	
	GtkCssProvider *label_provider = gtk_css_provider_new();
	gtk_css_provider_load_from_data(label_provider,
			".normal-label { "
			"  font-family: Sans; "
			"  font-weight: bold; "
			"  font-size: 10pt; "
			"}\n", -1, NULL);
	
	gtk_style_context_add_provider_for_screen(
			screen,
			GTK_STYLE_PROVIDER(label_provider),
			GTK_STYLE_PROVIDER_PRIORITY_APPLICATION
	);
	
/* Frame will hold the grid and get the border */
	frame = gtk_frame_new(NULL);
	gtk_widget_set_name(frame, "grid_frame");  /* used by CSS selector */
	
/* Remove default frame shadow to avoid a double effect */
	gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_NONE);
	
/* Create the bordered grid */
	grid = gtk_grid_new();
	gtk_grid_set_row_spacing(GTK_GRID(grid), 10);
	gtk_grid_set_column_spacing(GTK_GRID(grid), 10);
	
/* Column 0 labels in rows 1 and 2 */
	for (int row = 1; row < 3; ++row) {
		label = gtk_label_new(lbl1[row - 1]);
		ctx = gtk_widget_get_style_context(label);
		gtk_style_context_add_class(ctx, "normal-label");
		gtk_grid_attach(GTK_GRID(grid), label, 0, row, 1, 1);
	}

/* Column 1 entries in rows 1 and 2 */
	w->entry_month = gtk_entry_new();
	gtk_entry_set_width_chars(GTK_ENTRY(w->entry_month), 12);
	gtk_entry_set_max_width_chars(GTK_ENTRY(w->entry_month), 12);
	gtk_grid_attach(GTK_GRID(grid), w->entry_month, 1, 1, 1, 1);
	gtk_entry_set_text(GTK_ENTRY(w->entry_month), (gchar*) "");
	
	w->entry_ndays = gtk_entry_new();
	gtk_entry_set_width_chars(GTK_ENTRY(w->entry_ndays), 12);
	gtk_entry_set_max_width_chars(GTK_ENTRY(w->entry_ndays), 12);
	gtk_grid_attach(GTK_GRID(grid), w->entry_ndays, 1, 2, 1, 1);
	gtk_entry_set_text(GTK_ENTRY(w->entry_ndays), (gchar*) "");

// maybe a spacer in column 2?
	label = gtk_label_new("          ");
	gtk_grid_attach(GTK_GRID(grid), label, 2, 1, 1, 1);

/* Column 3 and column 5 labels in row 0 */
	label = gtk_label_new("output by month");
	ctx = gtk_widget_get_style_context(label);
	gtk_style_context_add_class(ctx, "normal-label");
	gtk_grid_attach(GTK_GRID(grid), label, 3, 0, 1, 1);
	label = gtk_label_new("cumulative output");
	ctx = gtk_widget_get_style_context(label);
	gtk_style_context_add_class(ctx, "normal-label");
	gtk_grid_attach(GTK_GRID(grid), label, 5, 0, 1, 1);

/* Column 3 entries in rows 1 and 2 */
	w->entry_mthirr = gtk_entry_new();
	gtk_entry_set_width_chars(GTK_ENTRY(w->entry_mthirr), 14);
	gtk_entry_set_max_width_chars(GTK_ENTRY(w->entry_mthirr), 14);
	gtk_grid_attach(GTK_GRID(grid), w->entry_mthirr, 3, 1, 1, 1);
	gtk_entry_set_text(GTK_ENTRY(w->entry_mthirr), (gchar*) "  ");
	
	w->entry_mthgdd = gtk_entry_new();
	gtk_entry_set_width_chars(GTK_ENTRY(w->entry_mthgdd), 14);
	gtk_entry_set_max_width_chars(GTK_ENTRY(w->entry_mthgdd), 14);
	gtk_grid_attach(GTK_GRID(grid), w->entry_mthgdd, 3, 2, 1, 1);
	gtk_entry_set_text(GTK_ENTRY(w->entry_mthgdd), (gchar*) "  ");

/* Column 4 labels in rows 1 and 2 */
	for (int row = 1; row < 3; ++row) {
		label = gtk_label_new(lbl2[row - 1]);
		ctx = gtk_widget_get_style_context(label);
		gtk_style_context_add_class(ctx, "normal-label");
		gtk_grid_attach(GTK_GRID(grid), label, 4, row, 1, 1);
	}
	
/* Column 5 entries in rows 1 and 2 */
	w->entry_cumirr = gtk_entry_new();
	gtk_grid_attach(GTK_GRID(grid), w->entry_cumirr, 5, 1, 1, 1);
	gtk_entry_set_width_chars(GTK_ENTRY(w->entry_cumirr), 14);
	gtk_entry_set_max_width_chars(GTK_ENTRY(w->entry_cumirr), 14);
	gtk_entry_set_text (GTK_ENTRY (w->entry_cumirr), (gchar*) "  ");

	w->entry_cumgdd = gtk_entry_new();
	gtk_grid_attach(GTK_GRID(grid), w->entry_cumgdd, 5, 2, 1, 1);
	gtk_entry_set_width_chars(GTK_ENTRY(w->entry_cumgdd), 14);
	gtk_entry_set_max_width_chars(GTK_ENTRY(w->entry_cumgdd), 14);
	gtk_entry_set_text (GTK_ENTRY (w->entry_cumgdd), (gchar*) "  ");

/* Put the grid inside the frame */
	gtk_container_add(GTK_CONTAINER(frame), grid);
	
/* Apply CSS to give the frame a border */
	provider = gtk_css_provider_new();
	gtk_css_provider_load_from_data(provider, css_data, -1, NULL);
	
	context = gtk_widget_get_style_context(frame);
	gtk_style_context_add_provider(
		context,
		GTK_STYLE_PROVIDER(provider), GTK_STYLE_PROVIDER_PRIORITY_USER);
	g_object_unref(provider);
	
	return frame;
}
void on_c2_start_clicked(GtkButton *button, gpointer user_data) {

	c2_widgets *w = user_data;

	int retval = c2_CheckEntries(w);                           // check entries

	printf("retval is %d and error_watch is %d\n", retval, error_watch);
	if (retval == 1 && error_watch == 1) {
		gtk_widget_hide(c2_window);
		gtk_widget_show(error_frame);
		gtk_widget_set_sensitive(error_close, TRUE);
		return;
	}

	if (retval == 2) {
		gtk_entry_set_text(GTK_ENTRY(w->entry_startday), "BAD DAY");
		return;
	}
	if (retval == 3) {
		gtk_entry_set_text(GTK_ENTRY(w->entry_stopday), "BAD DAY");
		return;
	}
	if (retval == 4) {
		gtk_entry_set_text(GTK_ENTRY(w->entry_year), "BAD YEAR");
		return;
	}
	if (retval == 5) {
		gtk_widget_set_visible(GTK_WIDGET(w->lbl_month_error), TRUE);
		gtk_widget_set_sensitive(w->btn_start, FALSE);
		return;
	}

	pass = 1;
	c2_ShowResult(w);                                          // calculate results
	gtk_widget_set_sensitive(w->btn_start, FALSE);
	gtk_widget_set_sensitive(w->btn_continue, TRUE);
}
void on_c2_continue_clicked(GtkButton *button, gpointer user_data) {
	c2_widgets *w = user_data;
	pass++;
	c2_ShowResult(w);                                          // continued results
}
void on_c2_close_clicked(GtkButton *button, gpointer user_data) {
	gtk_widget_hide(c2_window);
	in_use = 0;
}

//-------------------------------------------------------------------------
void manyDays() {

	c2_widgets *w = g_new0(c2_widgets, 1);          /* allocate struct */
	
	GtkWidget *outer_box;
	GtkWidget *c2_grid1;
	GtkWidget *c2_grid2;
	GtkWidget *c2_grid3;
	GtkWidget *c2_close;
	GdkColor color;

	color.red = 0x3333;
	color.green = 0xeeee;
	color.blue = 0xcccc;
	
	oneormany = 2;

/* A window to hold it all */
	c2_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_window_set_title(GTK_WINDOW(c2_window), "GDD during months");
	gtk_window_set_default_size(GTK_WINDOW(c2_window), 600, 470);
	gtk_window_set_position(GTK_WINDOW(c2_window), GTK_WIN_POS_CENTER);

/* A box to center things a bit and add margins */
	outer_box = gtk_vbox_new(FALSE, 5);
	gtk_container_add(GTK_CONTAINER(c2_window), outer_box);
	gtk_container_set_border_width(GTK_CONTAINER(outer_box), 10);

/* Color everything light green */
	gtk_widget_modify_bg(c2_window, GTK_STATE_NORMAL, &color);
	
/* Create the top (input) grid wrapped in a frame that has the border */
	c2_grid1 = create_c2_grid1(w);
	gtk_box_pack_start(GTK_BOX(outer_box), c2_grid1, FALSE, FALSE, 0);
	
/* Create the lower (output) grid that is also wrapped in a border */
	c2_grid2 = create_c2_grid2(w);
	gtk_box_pack_start(GTK_BOX(outer_box), c2_grid2, FALSE, FALSE, 0);

/* Create a third grid to hold buttons */
	c2_grid3 = gtk_grid_new();
	gtk_widget_set_size_request(c2_grid3, 600, 30);
	gtk_grid_set_row_spacing(GTK_GRID(c2_grid3), 10);
	gtk_grid_set_column_spacing(GTK_GRID(c2_grid3), 100);
	gtk_box_pack_start(GTK_BOX(outer_box), c2_grid3, FALSE, FALSE, 0);

/* Blank labels provide spacing to center the buttons */
	GtkWidget *label = gtk_label_new("");
	gtk_grid_attach(GTK_GRID(c2_grid3), label, 0, 0, 1, 1);
	label = gtk_label_new("");
	gtk_grid_attach(GTK_GRID(c2_grid3), label, 4, 0, 1, 1);

	w->btn_start = gtk_button_new_with_label("     start     ");
	gtk_widget_set_sensitive(w->btn_start, FALSE);
	gtk_grid_attach(GTK_GRID(c2_grid3), w->btn_start, 1, 0, 1, 1);
	g_signal_connect(w->btn_start, "clicked",
					 G_CALLBACK(on_c2_start_clicked), w);
	
	w->btn_continue = gtk_button_new_with_label("continue");
	gtk_widget_set_sensitive(w->btn_continue, FALSE);
	gtk_grid_attach(GTK_GRID(c2_grid3), w->btn_continue, 2, 0, 1, 1);
	g_signal_connect(w->btn_continue, "clicked",
					 G_CALLBACK(on_c2_continue_clicked), w);
	
	w->btn_close = gtk_button_new_with_label("     close     ");
	gtk_grid_attach(GTK_GRID(c2_grid3), w->btn_close, 3, 0, 1, 1);
	g_signal_connect(w->btn_close, "clicked",
					 G_CALLBACK(on_c2_close_clicked), w);

	gtk_widget_show_all(c2_window);
	gtk_widget_set_visible(GTK_WIDGET(w->lbl_month_error), FALSE);
}
