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

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

void c2_ShowResult(c2_widgets *w) {
	float daysIrr, monthsIrr, fullMonthsIrr;
	float monthsGDD, fullMonthsGDD;
	int day, month, year;
	int firstday, lastday;
	char bfr[128];
	char *mthname[] = {"January", "February", "March", "April", "May",
		"June", "July", "August", "September", "October", "November",
		"December"};

	if (pass == 1) {
		const gchar *t;

/* Although c2_Initialize reads in the locale, we do it again here because
 * the user may have changed its values by hand.
 */
		t = gtk_entry_get_text(GTK_ENTRY(w->entry_lat));
		active_latitude = atof(t);
		t = gtk_entry_get_text(GTK_ENTRY(w->entry_lon));
		active_longitude = atof(t);
		if (active_longitude >= 0) active_longitude = -active_longitude;
		t = gtk_entry_get_text(GTK_ENTRY(w->entry_elev));
		active_elev = atof(t);
		t = gtk_entry_get_text(GTK_ENTRY(w->entry_aspect));
		active_aspect = atof(t);
		t = gtk_entry_get_text(GTK_ENTRY(w->entry_tilt));
		active_tilt = atof(t);

		t = gtk_entry_get_text(GTK_ENTRY(w->entry_startday));
		startday = atoi(t);

		t = gtk_entry_get_text(GTK_ENTRY(w->entry_stopday));
		stopday = atoi(t);

		gchar *start_text = gtk_combo_box_text_get_active_text(w->cbtxt_startmonth);
		startmonth = getMonthNumber(start_text);
		g_free(start_text);

		gchar *stop_text = gtk_combo_box_text_get_active_text(w->cbtxt_stopmonth);
		stopmonth = getMonthNumber(stop_text);
		g_free(stop_text);

		t = gtk_entry_get_text(GTK_ENTRY(w->entry_year));
		active_year = atoi(t);

		setMonthLengths(active_year);
		
		active_day = startday;
		active_month = startmonth;
		
		if (active_latitude > 43.7)
			northsouth = 1;
		else
			northsouth = 2;

/* Freeze locale fields if desired */
		if (!haveprefs) {
			gtk_editable_set_editable(GTK_EDITABLE(w->entry_lat), FALSE);
			gtk_editable_set_editable(GTK_EDITABLE(w->entry_lon), FALSE);
			gtk_editable_set_editable(GTK_EDITABLE(w->entry_elev), FALSE);
			gtk_editable_set_editable(GTK_EDITABLE(w->entry_aspect), FALSE);
			gtk_editable_set_editable(GTK_EDITABLE(w->entry_tilt), FALSE);
		}

		month = startmonth - 1;
		accumulatedIrr = 0;
		accumulatedGDD = 0;
	}

/* Continuation path */
	if (pass != 1) month = active_month;
	month++;
	active_month = month;
	
/* Determine firstday/lastday as in your old code */
	if (month == startmonth) {
		firstday = startday;
		if (startmonth == stopmonth)
			lastday = stopday;
		else
			lastday = dayspermonth[month - 1];
	} else if (month == stopmonth) {
		lastday = stopday;
		if (startmonth == stopmonth)
			firstday = startday;
		else
			firstday = 1;
	} else {
		firstday = 1;
		lastday = dayspermonth[month - 1];
	}

/* Compute monthsIrr/monthsGDD as before using solpos helpers */
	if (firstday == 1 && lastday == dayspermonth[month-1]) {
		monthsIrr = getMonthsIrr(month);
		monthsGDD = getMonthsGDD(monthsIrr, month);
	} else {
		monthsIrr = 0;
		for (day = firstday; day <= lastday; day++) {
			daysIrr = getDaysIrr(day, month, active_year);
			monthsIrr += daysIrr;
		}
		fullMonthsIrr = getMonthsIrr(month);
		fullMonthsGDD = getMonthsGDD(fullMonthsIrr, month);
		monthsGDD = (monthsIrr / fullMonthsIrr) * fullMonthsGDD;
	}

/* Update grid2 entries via w */

	strcpy(bfr, mthname[month - 1]);
	gtk_entry_set_text(GTK_ENTRY(w->entry_month), bfr);
	
	sprintf(bfr, "%d - %d", firstday, lastday);
	gtk_entry_set_text(GTK_ENTRY(w->entry_ndays), bfr);
	
	sprintf(bfr, "%.1f mj/sqm", monthsIrr);
	gtk_entry_set_text(GTK_ENTRY(w->entry_mthirr), bfr);
	
	sprintf(bfr, "%.2f", monthsGDD);
	gtk_entry_set_text(GTK_ENTRY(w->entry_mthgdd), bfr);
	
	accumulatedIrr += monthsIrr;
	accumulatedGDD += monthsGDD;
	
	sprintf(bfr, "%.1f mj/sqm", accumulatedIrr);
	gtk_entry_set_text(GTK_ENTRY(w->entry_cumirr), bfr);
	
	sprintf(bfr, "%.2f", accumulatedGDD);
	gtk_entry_set_text(GTK_ENTRY(w->entry_cumgdd), bfr);

/* Button logic: disable continue at the final month */
	if (month == stopmonth) {
		gtk_widget_set_sensitive(w->btn_continue, FALSE);
		gtk_widget_set_sensitive(w->btn_start, TRUE);   /* allow new run */
	}
	
/* If you want locale editable again only at the end of the full run: */
	if (month == stopmonth) {
		gtk_editable_set_editable(GTK_EDITABLE(w->entry_lat), TRUE);
		gtk_editable_set_editable(GTK_EDITABLE(w->entry_lon), TRUE);
		gtk_editable_set_editable(GTK_EDITABLE(w->entry_elev), TRUE);
		gtk_editable_set_editable(GTK_EDITABLE(w->entry_aspect), TRUE);
		gtk_editable_set_editable(GTK_EDITABLE(w->entry_tilt), TRUE);
	}
}
