#include <gtk/gtk.h>
#include <glib.h>
#include <glib/gprintf.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>

#include "mmyth_ui.h"
#include "mmyth_uicommon.h"
#include "mmyth_recordui.h"
#include "mmyth_schedulerui.h"

/* GMyth library includes */
#include <gmyth/gmyth_scheduler.h>
#include <gmyth/gmyth_common.h>
#include <gmyth/gmyth_epg.h>

static void run_calendar_dialog (GtkButton *button, gpointer data);

static void add_channel_field (MMythSchedulerUI *scheduler_ui, GtkWidget *vbox);
static void add_time_field (MMythSchedulerUI *scheduler_ui, GtkWidget *vbox);
static void add_date_field (MMythSchedulerUI *scheduler_ui, GtkWidget *vbox);
static void add_duration_field (MMythSchedulerUI *scheduler_ui, GtkWidget *vbox);
static void add_frequency_field (MMythSchedulerUI *scheduler_ui, GtkWidget *vbox);
static void add_title_field (MMythSchedulerUI *scheduler_ui, GtkWidget *vbox);

MMythSchedulerUI*
mmyth_schedulerui_new ( GMythBackendInfo *backend_info )
{
	GtkWidget *scrolledwindow;	
	GtkWidget *viewport;	
	GtkWidget *head_hbox;
	GtkWidget *fields_vbox;
	GtkWidget *hseparator;
	GtkWidget *label;

	MMythSchedulerUI *scheduler_ui = g_new0 (MMythSchedulerUI, 1);

	scheduler_ui->backend_info = backend_info;
	
	scrolledwindow = gtk_scrolled_window_new (NULL, NULL);
	scheduler_ui->main_widget = scrolledwindow;
	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwindow),
  			GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);

	//Is this needed?  
	viewport = gtk_viewport_new (NULL, NULL);
	gtk_container_add (GTK_CONTAINER (scrolledwindow), viewport);

	//Is this needed?
	head_hbox = gtk_hbox_new (FALSE, 0);
	gtk_container_add (GTK_CONTAINER (viewport), head_hbox);

	fields_vbox = gtk_vbox_new (FALSE, 0);
	gtk_box_pack_start (GTK_BOX (head_hbox), fields_vbox, TRUE, TRUE, 0);
	gtk_container_set_border_width (GTK_CONTAINER (fields_vbox), 10);

	label = gtk_label_new_with_mnemonic (("Manual Schedule Recording"));
	gtk_box_pack_start (GTK_BOX (fields_vbox), label, FALSE, FALSE, 0);
	gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);

	hseparator = gtk_hseparator_new ();
	gtk_box_pack_start (GTK_BOX (fields_vbox), hseparator, FALSE, TRUE, 0);

	add_channel_field (scheduler_ui, fields_vbox);
	add_time_field (scheduler_ui, fields_vbox);
	add_date_field (scheduler_ui, fields_vbox);
	add_duration_field (scheduler_ui, fields_vbox);
	add_frequency_field (scheduler_ui, fields_vbox);
	add_title_field (scheduler_ui, fields_vbox);
	
	return scheduler_ui;
}

static void
set_date_from_calendar (GtkCalendar *calendar, gpointer data)
{
	char sched_date[24];

	MMythSchedulerUI *scheduler_ui = (MMythSchedulerUI*) data;

	// FIXME: Change this, save another value instead of month_temp, day_temp, ...	
	gtk_calendar_get_date(GTK_CALENDAR(calendar), 
		&(scheduler_ui->year_temp), &(scheduler_ui->month_temp), &(scheduler_ui->day_temp));

	sched_date[23]='\0';
	g_sprintf(sched_date, "%04d %02d %02d (yyyy mm dd)",
		scheduler_ui->year_temp, scheduler_ui->month_temp+1, scheduler_ui->day_temp);

	gtk_button_set_label(GTK_BUTTON(scheduler_ui->date_button), sched_date);

	gtk_widget_destroy(scheduler_ui->calendar_dialog);
	scheduler_ui->calendar_dialog = NULL;
	scheduler_ui->calendar = NULL;
}

//calendar
static void
run_calendar_dialog (GtkButton *button, gpointer data)
{

	GtkWidget *dialog_vbox;
	MMythSchedulerUI *scheduler_ui = (MMythSchedulerUI*) data;

	// calendar_dialog and calendar are been released at set_date_from_calendar ()
	scheduler_ui->calendar_dialog = gtk_dialog_new ();
	gtk_container_set_border_width (GTK_CONTAINER (scheduler_ui->calendar_dialog), 1);
  	gtk_window_set_title (GTK_WINDOW (scheduler_ui->calendar_dialog), "Select starting date");
  	gtk_window_set_position (GTK_WINDOW (scheduler_ui->calendar_dialog), GTK_WIN_POS_CENTER);
	gtk_window_set_decorated (GTK_WINDOW (scheduler_ui->calendar_dialog), FALSE);

	dialog_vbox = GTK_DIALOG (scheduler_ui->calendar_dialog)->vbox;

	scheduler_ui->calendar = gtk_calendar_new ();

	gtk_box_pack_start (GTK_BOX (dialog_vbox), scheduler_ui->calendar, TRUE, TRUE, 0);
	gtk_calendar_display_options (GTK_CALENDAR (scheduler_ui->calendar),
        GTK_CALENDAR_SHOW_HEADING | GTK_CALENDAR_SHOW_DAY_NAMES);

	gtk_widget_show_all (scheduler_ui->calendar_dialog);

	g_signal_connect (G_OBJECT (scheduler_ui->calendar), "day-selected-double-click",
                      G_CALLBACK (set_date_from_calendar), data);
}


gboolean
mmyth_schedulerui_save (MMythSchedulerUI *scheduler_ui)
{
	GMythScheduler *scheduler;
	ScheduleInfo *schedule_info;
	GMythChannelInfo *channel_info;

	GList *clist;
	gint index, duration;
	gint frequency;
	struct tm start_tm;

	schedule_info = g_new0(ScheduleInfo, 1);
	if(schedule_info == NULL) {
		g_warning ("Error allocating memory");
		return FALSE;
	}

	clist = scheduler_ui->channel_list;

	index = gtk_combo_box_get_active(GTK_COMBO_BOX(scheduler_ui->channel_combobox));

	if (clist != NULL)
		clist = g_list_nth(clist, index);

	if (clist) {
		g_debug ("[%s] New schedule: %d", __FUNCTION__, index);
	} else {
		g_warning ("[%s] Error when adding new schedule", __FUNCTION__);
		return FALSE;
	}

	channel_info = clist->data;

	/* initialize schedule_info */
	schedule_info->channel_id = channel_info->channel_ID;

	start_tm.tm_hour = 
		(int)gtk_spin_button_get_value(GTK_SPIN_BUTTON(scheduler_ui->hour_spinbutton));
	start_tm.tm_min = 
		(int)gtk_spin_button_get_value(GTK_SPIN_BUTTON(scheduler_ui->min_spinbutton));
	start_tm.tm_sec = 0;

	start_tm.tm_mday = (gint)scheduler_ui->day_temp;
	start_tm.tm_mon =  (gint)scheduler_ui->month_temp;
	start_tm.tm_year = (gint)scheduler_ui->year_temp - 1900; //years since 1900
	GTimeVal* time_val_local = g_new0( GTimeVal, 1 );
	time_val_local->tv_sec = timelocal(&start_tm);

	schedule_info->start_time = time_val_local;
	if ( NULL == schedule_info->start_time ) {
		g_warning ("timelocal error!\n");
		return FALSE;
	}

	duration = (gint) gtk_spin_button_get_value(
			GTK_SPIN_BUTTON(scheduler_ui->duration_spinbutton));
	schedule_info->end_time = schedule_info->start_time + (duration*60);

	/* TODO: frequency is not implemented yet */
	frequency = gtk_combo_box_get_active(GTK_COMBO_BOX(scheduler_ui->freq_combobox));

	schedule_info->title = g_string_new("");
	g_string_printf(schedule_info->title, "%s", 
			gtk_entry_get_text(GTK_ENTRY(scheduler_ui->title_entry)));

	/* FIXME: Architecture change to reuse the scheduler created in the recordui! */
	scheduler = gmyth_scheduler_new ();

	gmyth_scheduler_connect(scheduler, scheduler->backend_info);

	/* FIXME: set record_id = -1 to add a new schedule */
	schedule_info->record_id = -1;
	gmyth_scheduler_add_schedule (scheduler, schedule_info);

	gmyth_scheduler_disconnect(scheduler);

	/* free allocated memory */
	g_object_unref (scheduler);
	g_free (schedule_info);

	return TRUE;
} 

static GtkWidget*
add_line (GtkWidget *vbox, const gchar *str)
{
	GtkWidget *label;
	GtkWidget *hbox = gtk_hbox_new (FALSE, 0);

	gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
	gtk_container_set_border_width (GTK_CONTAINER (hbox), 3);
	
	label = gtk_label_new (str);
	gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
	gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);	

	return hbox;
}

static void
add_channel_field (MMythSchedulerUI *scheduler_ui, GtkWidget *vbox)
{
	GtkWidget *combobox;
	
	GtkWidget *hbox = add_line (vbox, "Channel:    ");
	
	combobox = gtk_combo_box_new_text ();

	scheduler_ui->channel_combobox = combobox;
	gtk_box_pack_start (GTK_BOX (hbox), combobox, FALSE, FALSE, 0);
			
	GMythEPG *mmyth_epg = gmyth_epg_new ();
	if (!gmyth_epg_connect (mmyth_epg, scheduler_ui->backend_info)) {
		// FIXME: Without this list the scheduler UI should not be shown!
		g_warning ("[%s] Error when getting list of channels", __FUNCTION__);
	}
  	
	if (gmyth_epg_get_channel_list (mmyth_epg, &(scheduler_ui->channel_list)) < 0) {
		g_debug ("[%s] Error while trying to retrieve channel list", __FUNCTION__);
	} else {
 		GList *clist =  scheduler_ui->channel_list;
		GMythChannelInfo *channel_info;

		while (clist != NULL) {
  	  		channel_info = clist->data;
  	  		clist = clist->next;
  	  		gtk_combo_box_append_text (GTK_COMBO_BOX (scheduler_ui->channel_combobox), 
                                       (channel_info->channel_name->str));
  		}

       gtk_combo_box_set_active(GTK_COMBO_BOX (scheduler_ui->channel_combobox), 0);
  	}
}

static void
add_time_field (MMythSchedulerUI *scheduler_ui, GtkWidget *vbox)
{
	GtkWidget *label;
	GtkObject *spinbutton_adj;
	GtkWidget *hbox = add_line (vbox, "Time:         ");

	time_t real_time;
	struct tm sched_time;

	time(&real_time);

    if (localtime_r((time_t *)&real_time, &sched_time) == NULL) {
        g_warning ("localtime_r error in mmyth_epg_grid_view!\n");
        return;
    }
 
	if (sched_time.tm_min>30){
  		sched_time.tm_hour = sched_time.tm_hour+1;
  		sched_time.tm_min = 0;
	} else if (sched_time.tm_min>0) {
  		sched_time.tm_min = 30;
	}

  	scheduler_ui->year_temp = (guint)sched_time.tm_year + 1900;
  	scheduler_ui->month_temp = (guint)sched_time.tm_mon;
  	scheduler_ui->day_temp = (guint)sched_time.tm_mday;
   
	//hour entry
	spinbutton_adj = gtk_adjustment_new (sched_time.tm_hour, 00, 23, 1, 10, 10);
	scheduler_ui->hour_spinbutton = gtk_spin_button_new (GTK_ADJUSTMENT (spinbutton_adj), 1, 0);
	gtk_box_pack_start (GTK_BOX (hbox), scheduler_ui->hour_spinbutton, FALSE, FALSE, 0);

	label = gtk_label_new ((" : "));
	gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
	gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_RIGHT);
	gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);

	//minute entry
	spinbutton_adj = gtk_adjustment_new (sched_time.tm_min, 0, 59, 1, 10, 10);
	scheduler_ui->min_spinbutton = gtk_spin_button_new (GTK_ADJUSTMENT (spinbutton_adj), 1, 0);
	gtk_box_pack_start (GTK_BOX (hbox), scheduler_ui->min_spinbutton, FALSE, FALSE, 0);

	label = gtk_label_new ((" (hh:mm)"));

	gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);

}	

static void
add_date_field (MMythSchedulerUI *scheduler_ui, GtkWidget *vbox)
{
	char sched_date[24];
	GtkWidget *hbox = add_line (vbox, "Date:        ");
		
	//sched_date = ctime(&real_time);
  	g_sprintf (sched_date, "%04d %02d %02d (yyyy mm dd)", scheduler_ui->year_temp, scheduler_ui->month_temp+1, scheduler_ui->day_temp);
  	sched_date[23]='\0';
  
  	scheduler_ui->date_button = gtk_button_new_with_label (sched_date);
  	gtk_box_pack_start (GTK_BOX (hbox), scheduler_ui->date_button, FALSE, FALSE, 0);
  	gtk_button_set_relief (GTK_BUTTON (scheduler_ui->date_button), GTK_RELIEF_NONE);

	g_signal_connect (G_OBJECT (scheduler_ui->date_button), "clicked",
                      G_CALLBACK (run_calendar_dialog), scheduler_ui);

}

static void
add_duration_field (MMythSchedulerUI *scheduler_ui, GtkWidget *vbox)
{
	GtkWidget *hbox = add_line (vbox, "Duration:   ");
	GtkWidget *label;
	GtkObject *spinbutton_adj;
	
	spinbutton_adj = gtk_adjustment_new (60, 5, 360, 5, 60, 60);
  	scheduler_ui->duration_spinbutton = gtk_spin_button_new (GTK_ADJUSTMENT (spinbutton_adj), 1, 0);
  	gtk_box_pack_start (GTK_BOX (hbox), scheduler_ui->duration_spinbutton, FALSE, TRUE, 0);
  	gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (scheduler_ui->duration_spinbutton), TRUE);

	label = gtk_label_new ((" (minutes)     "));
  	gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
}

static void
add_frequency_field (MMythSchedulerUI *scheduler_ui, GtkWidget *vbox)
{
	
	GtkWidget *hbox = add_line (vbox, "Frequency: ");	
	
	scheduler_ui->freq_combobox = gtk_combo_box_new_text ();
	gtk_box_pack_start (GTK_BOX (hbox), scheduler_ui->freq_combobox, FALSE, FALSE, 0);
	gtk_combo_box_append_text (GTK_COMBO_BOX (scheduler_ui->freq_combobox), ("Only this day               "));
	gtk_combo_box_append_text (GTK_COMBO_BOX (scheduler_ui->freq_combobox), ("Daily                            "));
	gtk_combo_box_append_text (GTK_COMBO_BOX (scheduler_ui->freq_combobox), ("Weekly                         "));
	gtk_combo_box_set_active(GTK_COMBO_BOX (scheduler_ui->freq_combobox), 0);

}

static void
add_title_field (MMythSchedulerUI *scheduler_ui, GtkWidget *vbox)
{
	GtkWidget *hbox = add_line (vbox, "Title:           ");
	
  	scheduler_ui->title_entry = gtk_entry_new ();
  	gtk_box_pack_start (GTK_BOX (hbox), scheduler_ui->title_entry, FALSE, FALSE, 0);
  	gtk_entry_set_text (GTK_ENTRY (scheduler_ui->title_entry), "(Optional)");
	
}