/* t2_Populate
 * Called by gddcalc when "one day" ("test" in the window header) is clicked.
 */

#include <stdlib.h>
#include <gtk/gtk.h>
#include "gddcalc.h"

void getDayBounds(int, int, int, float *, float *);     // prototype
float getDaysIrr(int, int, int);                        // prototype

void t2_Populate() {
	size_t lth = 64;
	char *bfr = malloc(lth);
	char *tstring = malloc(lth);
	int hr, minutes, sec;
	float startMinute, stopMinute, daysIrr;

	active_year = 1999;                // initialize Atlanta
	active_month = 7;
	active_day = 22;
	active_latitude = 33.65;
	active_longitude = -84.43;
	active_aspect = 135;
	active_tilt = 33.65;
	active_timezone = -5;
	active_hour = 9;
	active_minute = 45;
	active_second = 37;
	active_dt = 1;
	active_temp = 27;
	active_press = 1006;
	active_daynum = 203;

	strcpy(tstring, "(EST)");

/* Get the sunrise and sunset times and put them into the display */

	getDayBounds(active_day, active_month, active_year, &startMinute, &stopMinute);

	active_sunrise = startMinute;
	hr = (int)(active_sunrise/60);
	minutes = (int)(active_sunrise - (float)(hr*60));
	sec = (int)((active_sunrise - (float)(hr*60) - (float)minutes)*60);
	sprintf(bfr, "%2.2d:%2.2d:%2.2d %s", hr, minutes, sec, tstring);
	gtk_entry_set_text (GTK_ENTRY(t2_entry1), (gchar*)bfr);

	active_sunset = stopMinute;
	hr = (int)(active_sunset/60);
	minutes = (int)(active_sunset - (float)(hr*60));
	sec = (int)((active_sunset - (float)(hr*60) - (float)minutes)*60);
	sprintf(bfr, "%2.2d:%2.2d:%2.2d %s", hr, minutes, sec, tstring);
	gtk_entry_set_text (GTK_ENTRY(t2_entry2), (gchar*)bfr);

/* Put the day's accumulated irradiance into the display */

	daysIrr = getDaysIrr(active_day, active_month, active_year);
	sprintf(bfr, "%.2f mj/sqm", daysIrr);
	gtk_entry_set_text (GTK_ENTRY(t2_entry3), (gchar*)bfr);
	free(bfr);
	free(tstring);
}
