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

/* prototypes */

void on_c1_cbtxt_month_changed(GtkComboBoxText *, gpointer user_data);
void on_c1_dismiss_clicked(GtkButton *button, gpointer user_data);

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

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

static GtkWidget *create_c1_grid1(c1_widgets *w)
{
	void c1_Initialize();                                        // prototype
	
	GtkWidget *frame;
	GtkWidget *grid;
	GtkCssProvider *provider;
	GtkStyleContext *context;
	char *lbl1[] = {"", "latitude", "longitude", "elevation", "aspect", "tilt"};
	char *lbl2[] = {"", "", "day", "month", "year"};
	char bfr[128];

/* Setup for labels in the upper grid (c1_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
	);

/* Populate the left side of the grid */
	c1_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 */
	for (int row = 1; row < 6; ++row) {
		label = gtk_label_new(lbl1[row]);
		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 */
	w->c1_entry1 = gtk_entry_new();
	gtk_grid_attach(GTK_GRID(grid), w->c1_entry1, 1, 1, 1, 1);
	if (haveprefs) {sprintf(bfr, "%8.4f", active_latitude);}
	else {strcpy(bfr, "");}
	gtk_entry_set_text (GTK_ENTRY (w->c1_entry1), (gchar*) bfr);   // lat

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

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

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

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

/* Column 2 label */
	label = gtk_label_new("     Input");
	ctx = gtk_widget_get_style_context(label);
	gtk_style_context_add_class(ctx, "section-title");
	gtk_grid_attach(GTK_GRID(grid), label, 2, 0, 1, 1);

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

/* Column 4 entries */
	w->c1_entry6 = gtk_entry_new();
	gtk_grid_attach(GTK_GRID(grid), w->c1_entry6, 4, 2, 1, 1);
	gtk_widget_grab_focus(w->c1_entry6);
	
	w->c1_cbtxt_month = GTK_COMBO_BOX_TEXT(gtk_combo_box_text_new());
	gtk_grid_attach(GTK_GRID(grid), GTK_WIDGET(w->c1_cbtxt_month), 4, 3, 1, 1);
	gtk_combo_box_text_append_text(w->c1_cbtxt_month, "January");
	gtk_combo_box_text_append_text(w->c1_cbtxt_month, "February");
	gtk_combo_box_text_append_text(w->c1_cbtxt_month, "March");
	gtk_combo_box_text_append_text(w->c1_cbtxt_month, "April");
	gtk_combo_box_text_append_text(w->c1_cbtxt_month, "May");
	gtk_combo_box_text_append_text(w->c1_cbtxt_month, "June");
	gtk_combo_box_text_append_text(w->c1_cbtxt_month, "July");
	gtk_combo_box_text_append_text(w->c1_cbtxt_month, "August");
	gtk_combo_box_text_append_text(w->c1_cbtxt_month, "September");
	gtk_combo_box_text_append_text(w->c1_cbtxt_month, "October");
	gtk_combo_box_text_append_text(w->c1_cbtxt_month, "November");
	gtk_combo_box_text_append_text(w->c1_cbtxt_month, "December");
	
	w->c1_entry8 = gtk_entry_new();
	gtk_grid_attach(GTK_GRID(grid), w->c1_entry8, 4, 4, 1, 1);

/* 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;
}

/* ================== Build the output grid (c2_grid2) ====================*/

static GtkWidget *create_c1_grid2(c1_widgets *w)
{
	GtkWidget *frame;
	GtkWidget *grid;
	GtkCssProvider *provider;
	GtkStyleContext *context;

	char *lbl1[] = {"", "sunrise", "sunset"};
	char *lbl2[] = {"", "irradiance", "GDD"};

/* Setup for labels in the lower grid (c1_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 */
	for (int row = 1; row < 3; ++row) {
		label = gtk_label_new(lbl1[row]);
		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 */
	w->c1_entry9 = gtk_entry_new();
	gtk_grid_attach(GTK_GRID(grid), w->c1_entry9, 1, 1, 1, 1);
	gtk_entry_set_text (GTK_ENTRY (w->c1_entry9), (gchar*) "  ");
	
	w->c1_entry10 = gtk_entry_new();
	gtk_grid_attach(GTK_GRID(grid), w->c1_entry10, 1, 2, 1, 1);
	gtk_entry_set_text (GTK_ENTRY (w->c1_entry10), (gchar*) "  ");

/* Column 2 label */
	label = gtk_label_new("     Output");
	ctx = gtk_widget_get_style_context(label);
	gtk_style_context_add_class(ctx, "section-title");
	gtk_grid_attach(GTK_GRID(grid), label, 2, 0, 1, 1);

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

/* column 4 entries */
	w->c1_entry11 = gtk_entry_new();
	gtk_grid_attach(GTK_GRID(grid), w->c1_entry11, 4, 1, 1, 1);
	gtk_entry_set_text (GTK_ENTRY (w->c1_entry11), (gchar*) "  ");

	w->c1_entry12 = gtk_entry_new();
	gtk_grid_attach(GTK_GRID(grid), w->c1_entry12, 4, 2, 1, 1);
	gtk_entry_set_text (GTK_ENTRY (w->c1_entry12), (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_c1_cbtxt_month_changed(GtkComboBoxText *combo, gpointer user_data) {
	GtkWidget *button = GTK_WIDGET(user_data);
	if (button) gtk_widget_set_sensitive(button, TRUE);
}

void on_c1_calculate_clicked(GtkButton *button, gpointer user_data) {

	c1_widgets *w = user_data;

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

	if (retval == 1 && error_watch == 1) {
			gtk_widget_hide(GTK_WIDGET(c1_window));
			gtk_widget_show(GTK_WIDGET(error_frame));
			gtk_widget_set_sensitive(GTK_WIDGET(error_close), TRUE);
	}
	if (retval == 2) {
		gtk_entry_set_text(GTK_ENTRY(w->c1_entry6), "BAD DAY");
	}
	if (retval == 3) {
		gtk_entry_set_text(GTK_ENTRY(w->c1_entry8), "BAD YEAR");
	}
	if (retval == 0) {
		c1_ShowResult(w);                      // calculate and display result
	}
}

void on_c1_dismiss_clicked(GtkButton *button, gpointer user_data) {
	gtk_widget_hide(c1_window);
	in_use = 0;
}

//-------------------------------------------------------------------------
void oneDay() {

	c1_widgets *w = g_new0(c1_widgets, 1);          /* allocate struct */

	GtkWidget *outer_box;
	GtkWidget *c1_grid1;
	GtkWidget *c1_grid2;
	GtkWidget *c1_grid3;
	GtkWidget *spacer_box;
	GtkWidget *c1_dismiss;

	GdkColor color;
	color.red = 0x3333;
	color.green = 0xeeee;
	color.blue = 0xcccc;
	
	oneormany = 1;        // An int defined in gddcalc.h

/* A window to hold it all */
	c1_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_window_set_title(GTK_WINDOW(c1_window), "GDD during one day");
	gtk_window_set_default_size(GTK_WINDOW(c1_window), 600, 470);
	gtk_window_set_position(GTK_WINDOW(c1_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(c1_window), outer_box);
	gtk_container_set_border_width(GTK_CONTAINER(outer_box), 10);

/* Create the grid wrapped in a frame that has the border */
	c1_grid1 = create_c1_grid1(w);
	gtk_box_pack_start(GTK_BOX(outer_box), c1_grid1, FALSE, FALSE, 0);

/* Create a second grid to hold the response */
	c1_grid2 = create_c1_grid2(w);
	gtk_box_pack_start(GTK_BOX(outer_box), c1_grid2, FALSE, FALSE, 0);

/* Color everything light green */
	gtk_widget_modify_bg(c1_window, GTK_STATE_NORMAL, &color);

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

	spacer_box = gtk_vbox_new(FALSE, 5);
	gtk_widget_set_size_request(spacer_box, 75, 30);
	gtk_grid_attach (GTK_GRID(c1_grid3), spacer_box, 0, 0, 1, 1);

	w->c1_calculate = gtk_button_new_with_label (" calculate ");
	gtk_widget_set_sensitive(w->c1_calculate, FALSE);
	gtk_grid_attach (GTK_GRID(c1_grid3), w->c1_calculate, 1, 0, 1, 1);
	g_signal_connect (w->c1_calculate, "clicked",
					G_CALLBACK(on_c1_calculate_clicked), w);
	g_signal_connect(w->c1_cbtxt_month, "changed",
					G_CALLBACK(on_c1_cbtxt_month_changed),
					w->c1_calculate);

	w->c1_dismiss = gtk_button_new_with_label ("  dismiss  ");
	gtk_grid_attach (GTK_GRID(c1_grid3), w->c1_dismiss, 2, 0, 1, 1);
	g_signal_connect (w->c1_dismiss, "clicked",
					G_CALLBACK(on_c1_dismiss_clicked), NULL);

	gtk_window_set_position(GTK_WINDOW(c1_window), GTK_WIN_POS_CENTER);
	gtk_widget_show_all(c1_window);
}
