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

bool c2_LocaleOK(c2_widgets *);                          // prototype
int getMonthNumber(char *);                              // prototype
void setMonthLengths(int);                               // prototype

int c2_CheckEntries(c2_widgets *w) {

// Check the left side of the array. c2_LocaleOK returns FALSE when one
// or more of the location variables is unacceptable.
	
	if (!c2_LocaleOK(w)) {
		if (error_watch == 1) {return 1;}
	}

// Check the right side of the display.

	const gchar *t;
	t = gtk_entry_get_text(GTK_ENTRY(w->entry_startday));
	int startday = atoi(t);
	gchar *start_text = gtk_combo_box_text_get_active_text(w->cbtxt_startmonth);
	int startmonth = getMonthNumber(start_text);
	g_free(start_text);
	t = gtk_entry_get_text(GTK_ENTRY(w->entry_stopday));
	int stopday = atoi(t);
	gchar *stop_text = gtk_combo_box_text_get_active_text(w->cbtxt_stopmonth);
	int stopmonth = getMonthNumber(stop_text);
	g_free(stop_text);
	t = gtk_entry_get_text(GTK_ENTRY(w->entry_year));
	int year = atoi(t);

	if (year < 2000 || year > 2050) {return 4;}
	
	if (stopmonth <= startmonth) {return 5;}

	setMonthLengths(year);
	if (startday < 1 || startday > dayspermonth[startmonth - 1]) {return 2;}
	if (stopday < 1 || stopday > dayspermonth[stopmonth - 1]) {return 3;}

	return 0;
}
