#include <gtk/gtk.h>
#include <glib.h>
#include <glib/gprintf.h>

#include "mmyth_uicommon.h"
#include "mmyth_ui.h"

static void
refresh_time_on_screen (GtkWidget *label)
{
	time_t real_time;
  	struct tm sched_time;
	GString *time_showed;

	time_showed = g_string_new("");
	time(&real_time);

    if (localtime_r((time_t *)&real_time, &sched_time) == NULL) {
        g_error ("localtime_r error in mmyth_epg_grid_view!\n");
        return;
    }

	g_string_printf(time_showed, "%d:%02d:%02d", 
		sched_time.tm_hour, 
		sched_time.tm_min, 
		sched_time.tm_sec);

	gtk_label_set_text (GTK_LABEL(label), time_showed->str);
}

MMythUiCommon *
mmyth_uicommon_new (GtkWidget * main_widget, const gchar * label1,
                    const gchar * label2, const gchar * label3)
{

    MMythUiCommon *ui_common = g_new0 (MMythUiCommon, 1); 
    
    if (!main_widget) {
    	g_warning ("MMythUICommon created with a NULL main widget\n");
    }
    
    ui_common->main_widget = main_widget;

    ui_common->event_box = gtk_event_box_new();
    
    /* Vertical box that divides the control area into two (buttons + clock) */
    ui_common->vbox = gtk_vbox_new (FALSE, 0);  /* spacing */
    gtk_container_set_border_width(GTK_CONTAINER (ui_common->vbox), 4);
    
    gtk_container_add (GTK_CONTAINER (ui_common->event_box),
                       ui_common->vbox);
    gtk_widget_modify_bg(ui_common->event_box, GTK_STATE_NORMAL, &main_bg_color);  
    
    /* Vertical box that divides the control area into four */
    ui_common->button_vbox = gtk_vbox_new (TRUE, 0);    /* spacing */

    gtk_container_set_border_width (GTK_CONTAINER (ui_common->button_vbox), 0);

    /* The button 1 */
    ui_common->button1 = gtk_button_new_with_label (label1);
    gtk_widget_modify_bg(ui_common->button1, GTK_STATE_NORMAL, &main_bg_color);  
    gtk_widget_set_size_request (ui_common->button1, BUTTON_WIDTH,
                                 BUTTON_HEIGHT);

    /* The button 2 */
    ui_common->button2 = gtk_button_new_with_label (label2);
    gtk_widget_modify_bg(ui_common->button2, GTK_STATE_NORMAL, &main_bg_color);  
    gtk_widget_set_size_request (ui_common->button2, BUTTON_WIDTH,
                                 BUTTON_HEIGHT);

    /* The button 3 */
    ui_common->button3 = gtk_button_new_with_label (label3);
    gtk_widget_modify_bg(ui_common->button3, GTK_STATE_NORMAL, &main_bg_color);  
    gtk_widget_set_size_request (ui_common->button3, BUTTON_WIDTH,
                                 BUTTON_HEIGHT);

    /* The clock label */
    ui_common->label = gtk_label_new ("Starting...");
    gtk_widget_show (ui_common->label);
    ui_common->source_id = g_timeout_add(500, (GSourceFunc) refresh_time_on_screen, ui_common->label);

    /* Packing components */
    gtk_box_pack_start (GTK_BOX (ui_common->vbox),
                        ui_common->button_vbox, TRUE, TRUE, 0);

    gtk_box_pack_start (GTK_BOX (ui_common->vbox),
                        ui_common->label, FALSE, FALSE, 0);

    gtk_box_pack_start (GTK_BOX (ui_common->button_vbox),
                        ui_common->button1, FALSE, FALSE, 0);

    gtk_box_pack_start (GTK_BOX (ui_common->button_vbox),
                        ui_common->button2, FALSE, FALSE, 0);

    gtk_box_pack_start (GTK_BOX (ui_common->button_vbox),
                        ui_common->button3, FALSE, FALSE, 0);

    gtk_widget_show_all (ui_common->event_box);

    /* FIXME: mmyth_ui->video_alignment == NULL when mmyth_player_init fails */
    if(ui_common->main_widget != NULL)
        g_object_ref (ui_common->main_widget);
        
    g_object_ref (ui_common->vbox);
    g_object_ref (ui_common->button_vbox);
    g_object_ref (ui_common->label);
    g_object_ref (ui_common->button1);
    g_object_ref (ui_common->button2);
    g_object_ref (ui_common->button3);
    g_object_ref (ui_common->event_box);
    return ui_common;
}

void
mmyth_uicommon_free (MMythUiCommon *ui_common)
{
	g_debug ("[%s] Clean uicommon used memory", __FUNCTION__);

    g_source_remove (ui_common->source_id);
    	
    g_object_unref (ui_common->main_widget);
    g_object_unref (ui_common->vbox);
    g_object_unref (ui_common->button_vbox);
    g_object_unref (ui_common->label);
    g_object_unref (ui_common->button1);
    g_object_unref (ui_common->button2);
    g_object_unref (ui_common->button3);
    g_object_unref (ui_common->event_box);    
    
    g_free (ui_common);
}