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

void setMonthLengths(int);                            // prototype
float getDaysIrr(int, int, int);                      // prototype
float getMonthsGDD(float, int);                       // prototype
int isLeapYear(int);                                  // prototype
int getMonthNumber(char *);                           // prototype

void c1_ShowResult(c1_widgets *w) {

	float daysIrr, monthsIrr, fullMonthsIrr;
	float daysGDD, activeDaysGDD, fullMonthsGDD;
	int day, month, year;
	int k, hr, minutes, sec;
	long retval;
	char *mthname[] = {"January", "February", "March", "April", "May",
		"June", "July", "August", "September", "October", "November",
		"December"};
	char bfr[64], tstring[6], srisebfr[16], ssetbfr[16];
	char *zone[] = {"(EST)", "(Z-8)", ""};

/* Read the left side of the display and put whatever it contains into
 * the active variables. If we have a preferred locale, the active variables
 * will have been read from locale.text in c1_Initialize and then copied into
 * the 5 text boxes on the left side of the display. Here we read them back
 * from the text boxes into the active variables. This is necessary because
 * the user may change what is in the textboxes, and we want whatever is in
 * the text boxes to correspond to the active variables. If we don't have
 * a preferred locale, we are copying whatever the user has typed into the
 * textboxes.
 */

	const gchar *t;
	t = gtk_entry_get_text(GTK_ENTRY(w->c1_entry1));
	active_latitude = atof(t);
	t = gtk_entry_get_text(GTK_ENTRY(w->c1_entry2));
	active_longitude = atof(t);
	if (active_longitude >= 0) active_longitude = -active_longitude;
	t = gtk_entry_get_text(GTK_ENTRY(w->c1_entry3));
	active_elev = atof(t);
	t = gtk_entry_get_text(GTK_ENTRY(w->c1_entry4));
	active_aspect = atof(t);
	t = gtk_entry_get_text(GTK_ENTRY(w->c1_entry5));
	active_tilt = atof(t);

/* Read the day, month, and year from the right side of the display */

	t = gtk_entry_get_text(GTK_ENTRY(w->c1_entry6));
	active_day = atoi(t);
	
	gchar *month_text = gtk_combo_box_text_get_active_text(w->c1_cbtxt_month);
	month = getMonthNumber(month_text);
	active_month = getMonthNumber(month_text);
	g_free(month_text);
	
	t = gtk_entry_get_text(GTK_ENTRY(w->c1_entry8));
	active_year = atoi(t);
	setMonthLengths(active_year);
	
/* Latitude is determined at this point, so set North or South */
	
	if (active_latitude > 43.7)
		northsouth = 1;
	else
		northsouth = 2;

/* Calculate the times of sunrise and sunset. The posdata structure
 * is defined in solpos.h and pd is defined there as a posdata structure.
 * pdat is a pointer to pd.
 */
	pdat = &pd;
	S_init(pdat);                              // initialization of pdat
	pdat->latitude = active_latitude;
	pdat->longitude = active_longitude;
	pdat->aspect = active_aspect;
	pdat->tilt = active_tilt;
	pdat->year = active_year;
	pdat->month = active_month;
	pdat->day = active_day;
	pdat->hour = active_hour;
	pdat->minute = active_minute;
	pdat->second = active_second;
	pdat->temp = active_temp;
	pdat->press = active_press;
	pdat->timezone = active_timezone;

	if (isLeapYear(active_year))
		pdat->daynum = active_day + month_days[1][pdat->month];
	else
		pdat->daynum = active_day + month_days[0][pdat->month];

	retval = S_solpos(pdat);
	active_sunrise = pdat->sretr;
	active_sunset = pdat->ssetr;

/* Construct the string that indicates time zone */
	
	if (active_timezone == -5)                  // Atlanta
		k = 0;
	else if (active_timezone == -8)             // Oregon
		k = 1;
	else                                        // anywhere else
		k = 2;

	strcpy(tstring, zone[k]);

/* Put the sunrise and sunset times into the output window */

	hr = (int)(active_sunrise/60);
	minutes = (int)(active_sunrise - (float)(hr*60));
	sec = (int)((active_sunrise - (float)(hr*60) - (float)minutes)*60);
	sprintf(srisebfr, "%2.2d:%2.2d:%2.2d %s", hr, minutes, sec, tstring);
	gtk_entry_set_text(GTK_ENTRY(w->c1_entry9), srisebfr);

	hr = (int)(active_sunset/60);
	minutes = (int)(active_sunset - (float)(hr*60));
	sec = (int)((active_sunset - (float)(hr*60) - (float)minutes)*60);
	sprintf(ssetbfr, "%2.2d:%2.2d:%2.2d %s", hr, minutes, sec, tstring);
	gtk_entry_set_text(GTK_ENTRY(w->c1_entry10), ssetbfr);

/* Get the target day's irradiance and put it into the output window */
	
	active_daysIrr = getDaysIrr(active_day, active_month, active_year);
	sprintf(bfr, "%6.2f mj/sqm", active_daysIrr);
	gtk_entry_set_text(GTK_ENTRY(w->c1_entry11), bfr);
	
/* Now get the accumulated irradiance for this month */
	
	monthsIrr = 0;
	for (day = 1; day <= dayspermonth[active_month-1]; day++)
	{
		daysIrr = getDaysIrr(day, active_month, active_year);
		monthsIrr = monthsIrr + daysIrr;
	}
	fullMonthsIrr = monthsIrr;	
	
// And use it to calculate the day's GDD
	
	if (fullMonthsIrr <= 0.01)
		activeDaysGDD = 0;
	else
	{
		daysIrr = getDaysIrr(active_day, active_month, active_year);
		fullMonthsGDD = getMonthsGDD(fullMonthsIrr, active_month);
		activeDaysGDD = (daysIrr/fullMonthsIrr)*fullMonthsGDD;
	}
	sprintf(bfr, "%5.2f deg days", activeDaysGDD);
	gtk_entry_set_text(GTK_ENTRY(w->c1_entry12), bfr);
}
