#include <glib-object.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <gtk/gtk.h>
#include <gdk/gdkx.h>
#include <gst/interfaces/xoverlay.h>
#include <assert.h>

#include "mmyth_ui.h"
#include "mmyth_uicommon.h"
#include "mmyth_schedulerui.h"
#include "mmyth_recordui.h"
#include "mmyth_uisettings.h"
#include "mmyth_epg_grid_view.h"
#include "mmyth_tvplayer.h"

/* GMyth library includes */

#ifndef MAEMO_PLATFORM
static gint button_press_handler (GtkWidget *widget, GdkEvent *event);
#endif

static MMythUiCommon *create_main_view (MMythUi * mmyth_ui);
static MMythUiCommon *create_video_view (MMythUi * mmyth_ui);
static MMythUiCommon *create_epg_grid_view (MMythUi * mmyth_ui);
static MMythUiCommon *create_record_view (MMythUi * mmyth_ui);
static MMythUiCommon *create_schedule_view (MMythUi * mmyth_ui);

static void cb_video_close_button (GtkButton * button, gpointer user_data);
static void cb_record_button (GtkButton * button, gpointer user_data);
static void cb_menu_item_settings (GtkMenuItem *menuitem, gpointer user_data);

/* main box from button box separator*/
static GtkWidget *main_vseparator = NULL;

GdkPixbuf *icon_sports, *icon_news, 
          *icon_movies, *icon_shows, *icon_default;

#ifndef MAEMO_PLATFORM
/* FIXME: */
static MMythUi *popup_mmyth_ui;
#endif

MMythUi *
mmyth_ui_initialize (
#ifdef MAEMO_PLATFORM
	HildonProgram *program,
#endif
	GtkWidget * main_window)
{
    MMythUi *mmyth_ui;

    mmyth_ui = g_new0 (MMythUi, 1);

    mmyth_ui->backend_info = gmyth_backend_info_new_full( "localhost", "mythtv", "mythtv", "mythconverg", 6543 );

    mmyth_ui->main_window = main_window;    
    mmyth_ui->videow = NULL;
    mmyth_ui->mmyth_recordui = NULL;
    mmyth_ui->mmyth_schedulerui = NULL;
	
    /* Horizontal box that divides the view into control and video area */
    mmyth_ui->main_hbox = gtk_hbox_new (FALSE, 0);
    gtk_widget_show (mmyth_ui->main_hbox);
    g_object_ref (mmyth_ui->main_hbox);

    main_bg_color.red   = 65000;
    main_bg_color.green = 65000;
    main_bg_color.blue  = 65000;
    
    
#ifndef MAEMO_PLATFORM
    /* Popup menu */
    popup_mmyth_ui = mmyth_ui;
    g_signal_connect (G_OBJECT (mmyth_ui->main_hbox), "event",
                      G_CALLBACK (button_press_handler),
                      G_OBJECT (mmyth_ui->main_hbox));    

#else // #ifdef MAEMO

    mmyth_ui->main_menu = GTK_MENU(gtk_menu_new());
    hildon_program_set_common_menu(program, mmyth_ui->main_menu);

    mmyth_ui->menu_setup = gtk_menu_item_new_with_label("Setup");
    gtk_widget_set_size_request (GTK_WIDGET (mmyth_ui->menu_setup), 150, 40);
    gtk_menu_append(mmyth_ui->main_menu, mmyth_ui->menu_setup);

    g_signal_connect(G_OBJECT(mmyth_ui->menu_setup), "activate", G_CALLBACK(cb_menu_item_settings), mmyth_ui);

    gtk_widget_show_all (GTK_WIDGET (mmyth_ui->main_menu));
#endif
    
    // Main widget is mmyth_ui->main_hbox
    mmyth_ui->main_uicommon       = create_main_view (mmyth_ui);

    gtk_container_add (GTK_CONTAINER (mmyth_ui->main_window),
                       mmyth_ui->main_hbox);

    mmyth_ui_set_widget (mmyth_ui, mmyth_ui->main_uicommon);

    return mmyth_ui;
}

void
mmyth_ui_finalize (MMythUi * mmyth_ui)
{
    if (mmyth_ui != NULL) {
    	if (mmyth_ui->main_uicommon)
	    	mmyth_uicommon_free (mmyth_ui->main_uicommon);
    	if (mmyth_ui->video_uicommon)
		    mmyth_uicommon_free (mmyth_ui->video_uicommon);
		if (mmyth_ui->epg_grid_uicommon)
		    mmyth_uicommon_free (mmyth_ui->epg_grid_uicommon);
		if (mmyth_ui->record_uicommon)
		    mmyth_uicommon_free (mmyth_ui->record_uicommon);	    
		if (mmyth_ui->schedule_uicommon)
		    mmyth_uicommon_free (mmyth_ui->schedule_uicommon);

	    g_free (mmyth_ui);
    }
}

void
mmyth_ui_set_widget (MMythUi * mmyth_ui, MMythUiCommon * new_uicommon)
{
	if (new_uicommon == NULL) {
		g_warning ("MMythUI setting a NULL UI_Common as current display\n");
		return;
	}
		
    if (mmyth_ui->current_uicommon) {
        if (mmyth_ui->current_uicommon == mmyth_ui->video_uicommon) {
            gtk_widget_hide (mmyth_ui->current_uicommon->main_widget);
            gtk_widget_hide (mmyth_ui->videow);
        }
        else {
            gtk_container_remove (GTK_CONTAINER (mmyth_ui->main_hbox),
                                  mmyth_ui->current_uicommon->main_widget);
        }

        gtk_container_remove (GTK_CONTAINER (mmyth_ui->main_hbox),
                              mmyth_ui->current_uicommon->event_box);
        gtk_container_remove (GTK_CONTAINER (mmyth_ui->main_hbox),
                              main_vseparator);        

    }

    if (new_uicommon->main_widget == mmyth_ui->video_alignment) {
        //gst_player_video_show (GST_PLAYER_VIDEO(mmyth_ui->videow));
        gtk_widget_show (mmyth_ui->video_alignment);
        gtk_widget_show (mmyth_ui->videow);
    }
    else {
        /* FIXME: Fst call is NULL when mmyth_player_init fails */
        if(mmyth_ui->video_alignment != NULL)
            gtk_widget_hide (mmyth_ui->video_alignment);
        /* FIXME: Fst call is NULL when mmyth_player_init fails */
        if(mmyth_ui->videow != NULL)
            gtk_widget_hide (mmyth_ui->videow);

        gtk_box_pack_start (GTK_BOX (mmyth_ui->main_hbox),
                            new_uicommon->main_widget, TRUE, TRUE, 0);
    }

    if(main_vseparator == NULL) {
        /* FIXME: should free this variable*/
        main_vseparator = gtk_vseparator_new();
        g_object_ref (main_vseparator);                   
    }  
    gtk_box_pack_start (GTK_BOX (mmyth_ui->main_hbox),
                        main_vseparator, FALSE, FALSE, 0);    
    gtk_widget_show (main_vseparator);

    gtk_box_pack_end (GTK_BOX (mmyth_ui->main_hbox), new_uicommon->event_box, FALSE,
                      FALSE, 0);         

    mmyth_ui->current_uicommon = new_uicommon;

}

/* The close callback is the same for all windows*/
static void
cb_not_impl_button (GtkButton * button, gpointer user_data)
{
    MMythUi *mmyth_ui = (MMythUi *) user_data;

    GtkWidget *msg_dialog = gtk_message_dialog_new (
                                           GTK_WINDOW(mmyth_ui->main_window),
                                           GTK_DIALOG_MODAL |
                                           GTK_DIALOG_DESTROY_WITH_PARENT,
                                           GTK_MESSAGE_INFO,
                                           GTK_BUTTONS_OK,
                                           "Feature not implemented");
    gtk_widget_set_size_request (msg_dialog, 350, 120);

    gtk_dialog_run (GTK_DIALOG (msg_dialog));

    gtk_widget_destroy(GTK_WIDGET(msg_dialog)); 
}

/******************************************************************************
 *                        POPUP MENU WIDGET METHODS                           *
 *****************************************************************************/

static void 
cb_menu_item_settings (GtkMenuItem *menuitem,
                            gpointer user_data)
{
    
    MMythUi *mmyth_ui = (MMythUi*) user_data;
   
    if (mmyth_uisettings_run (GTK_WINDOW (mmyth_ui->main_window))) {
    	// If user changes the settings, we restart the context
    	g_debug ("[%s] Restarting mmyth_context to new settings", __FUNCTION__);
    	//gmyth_context_initialize();
    }
}

#ifndef MAEMO_PLATFORM

static void 
detacher (GtkWidget *attach_widget,GtkMenu *menu) 
{
	
}

static void
do_popup_menu (GtkWidget *my_widget, GdkEventButton *event)
{
    GtkWidget *popup;

  int button, event_time;

  GtkWidget *item_general;
  GtkWidget *image;

  popup = gtk_menu_new ();

  item_general = gtk_image_menu_item_new_with_mnemonic ("Setup");
  gtk_widget_show (item_general);
  gtk_container_add (GTK_CONTAINER (popup), item_general);

  image = gtk_image_new_from_stock ("gtk-edit", GTK_ICON_SIZE_MENU);
  gtk_widget_show (image);
  gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item_general), image);

  g_signal_connect (G_OBJECT(item_general), "activate", G_CALLBACK (cb_menu_item_settings), popup_mmyth_ui);

    if (event) {
      button = event->button;
      event_time = event->time;
    } else {
      button = 0;
      event_time = gtk_get_current_event_time ();
    }
    
    gtk_menu_attach_to_widget (GTK_MENU (popup), my_widget, detacher);
    gtk_menu_popup (GTK_MENU (popup), NULL, NULL, NULL, NULL, 
                  button, event_time);
    gtk_widget_show_all(popup);
}

/* Respond to a button-press by posting a menu passed in as widget.
 *
 * Note that the "widget" argument is the menu being posted, NOT
 * the button that was pressed.
 */
static gint 
button_press_handler (GtkWidget *widget, GdkEvent *event)
{

    if (event->type == GDK_BUTTON_PRESS) {
      GdkEventButton *bevent = (GdkEventButton *) event; 
          /* Ignore double-clicks and triple-clicks */
      if (bevent->button == 3)
        {
          do_popup_menu (widget, bevent);
          return TRUE;
        }
    }

    /* Tell calling code that we have not handled this event; pass it on. */
    return FALSE;
}
#endif

/******************************************************************************
 *                          MAIN APP VIEW METHODS                             *
 *****************************************************************************/

static void
cb_close_button (GtkButton * button, gpointer user_data)
{
    MMythUi *mmyth_ui = (MMythUi *) user_data;

    mmyth_ui_set_widget (mmyth_ui, mmyth_ui->main_uicommon);
}

static void
cb_watch_tv_button (GtkButton * button, gpointer user_data)
{
    MMythUi *mmyth_ui = (MMythUi *) user_data;
	gboolean res = FALSE;
	
	if (!(mmyth_ui->video_uicommon))
		mmyth_ui->video_uicommon = create_video_view (mmyth_ui);
	
	// Creates the tv player that will retrieve the mythtv content, decode and show it
	mmyth_ui->tvplayer = mmyth_tvplayer_new ();
	/* choose here if this is a LiveTV session */
	mmyth_ui->tvplayer->is_livetv = TRUE;

	res = mmyth_tvplayer_initialize (mmyth_ui->tvplayer, mmyth_ui->backend_info);

	if (!res) {
		g_warning ("[%s] Could not initialize tvplayer", __FUNCTION__);

		g_object_unref (mmyth_ui->tvplayer);
		mmyth_ui->tvplayer = NULL;

		GtkWidget *dialog = gtk_message_dialog_new (
				GTK_WINDOW(mmyth_ui->main_window),
				GTK_DIALOG_DESTROY_WITH_PARENT,
                GTK_MESSAGE_ERROR,
                GTK_BUTTONS_CLOSE,
			 	"MMyth found errors while starting TV Player, please check "
			 	"the GStreamer installation");

		gtk_dialog_run (GTK_DIALOG (dialog));
		gtk_widget_destroy (dialog);
		
		return;
	}
	//res = mmyth_tvplayer_livetv_setup (mmyth_ui->tvplayer);
	//
	if (mmyth_ui && mmyth_ui->tvplayer && res) {
		mmyth_tvplayer_set_widget (mmyth_ui->tvplayer, mmyth_ui->videow);
		mmyth_tvplayer_start_playing (mmyth_ui->tvplayer);
	} else {
		// TODO: Show Alert with error description!
		g_warning ("[%s] MMythUI can't initialize tv_player", __FUNCTION__);
		g_object_unref (mmyth_ui->tvplayer);
		mmyth_ui->tvplayer = NULL;
		// FIXME: Show the exact error that happened
		GtkWidget *dialog = gtk_message_dialog_new (
				GTK_WINDOW(mmyth_ui->main_window),
				GTK_DIALOG_DESTROY_WITH_PARENT,
                GTK_MESSAGE_ERROR,
                GTK_BUTTONS_CLOSE,
                "Error while starting TV Player, please check if the backend"
                " is running properly and a tv card is available!");
		gtk_dialog_run (GTK_DIALOG (dialog));
		gtk_widget_destroy (dialog);		
		return;
	}
	mmyth_ui_set_widget (mmyth_ui, mmyth_ui->video_uicommon);
	
}

static void 
cb_epg_button (GtkButton * button, gpointer user_data)
{
    MMythUi *mmyth_ui = (MMythUi *) user_data;
    
    if (!(mmyth_ui->epg_grid_uicommon))
    	mmyth_ui->epg_grid_uicommon = create_epg_grid_view(mmyth_ui);
    	
    mmyth_ui_set_widget (mmyth_ui, mmyth_ui->epg_grid_uicommon);
}

static MMythUiCommon *
create_main_view (MMythUi * mmyth_ui)
{
    MMythUiCommon *ui_common;
    GtkWidget *main_widget;
    GtkWidget *image;

	g_debug ("Creating Main UI Common");
	
    // FIXME: file path
#ifdef MMYTH_DEV
    image = gtk_image_new_from_file ("../pixmaps/mmyth_logo.png");
#else
    image = gtk_image_new_from_file ( PIX_DIR "mmyth_logo.png");
#endif
    
    main_widget = gtk_event_box_new();
    
    gtk_container_add (GTK_CONTAINER (main_widget),
                       image);


    gtk_widget_show_all (main_widget);
    g_object_ref (main_widget);

    ui_common = mmyth_uicommon_new (main_widget,
                                    "Watch TV", "EPG", "Recording");

    /* Button signals */
    // FIXME
    g_signal_connect (G_OBJECT (ui_common->button1), "clicked",
                      G_CALLBACK (cb_watch_tv_button), mmyth_ui);
    g_signal_connect (G_OBJECT (ui_common->button2), "clicked",
                      G_CALLBACK (cb_epg_button), mmyth_ui);
    g_signal_connect (G_OBJECT (ui_common->button3), "clicked",
                      G_CALLBACK (cb_record_button), mmyth_ui);

    return ui_common;

}

/******************************************************************************
 *                         epg GRID VIEW METHODS                              *
 *****************************************************************************/

static MMythUiCommon *
create_epg_grid_view (MMythUi * mmyth_ui)
{
    MMythUiCommon *ui_common;        

	g_debug ("Creating EPG Grid UI Common");
	
    GtkWidget *epg_grid_view = GTK_WIDGET (epg_grid_view_new (mmyth_ui));
    
    ui_common = mmyth_uicommon_new (epg_grid_view,
                                    "Play", "Record",
                                    "Close");    
    
    /* Button signals */
    g_signal_connect (G_OBJECT (ui_common->button1), "clicked",
                      G_CALLBACK (cb_not_impl_button), mmyth_ui);
    g_signal_connect (G_OBJECT (ui_common->button2), "clicked",
                      G_CALLBACK (cb_record_button), mmyth_ui);
    g_signal_connect (G_OBJECT (ui_common->button3), "clicked",
                      G_CALLBACK (cb_close_button), mmyth_ui);
    
    return ui_common;
}
/******************************************************************************
 *                         SCHEDULE VIEW METHODS                                *
 ******************************************************************************/

static void
cb_save_new_schedule (GtkButton *button, gpointer user_data)
{
    MMythUi *mmyth_ui = (MMythUi *) user_data;

	if (!(mmyth_ui->schedule_uicommon))
		mmyth_ui->schedule_uicommon = create_schedule_view(mmyth_ui);
		
    mmyth_schedulerui_save (mmyth_ui->mmyth_schedulerui);
    
    mmyth_recordui_reload_schedule (mmyth_ui->mmyth_recordui);
    
    mmyth_ui_set_widget (mmyth_ui, mmyth_ui->record_uicommon);
    
}

static void
cb_edit_scheduled (GtkTreeView * tree_view, GtkTreePath *path, 
		           GtkTreeViewColumn *column, gpointer user_data)
{
    MMythUi *mmyth_ui = (MMythUi *) user_data;
    GtkTreeSelection *selection;
	GtkTreeModel *list_store;
	GtkTreeIter iter;
	int index;
    //gint new_row = 0, record_id = 0;
	ScheduleInfo *schedule_info;
	GList *schedule_list;
	//GtkTreeIter iter;
	gint res;

	//gtk_tree_store_clear(recordui->sch_tree_store);	

	res = gmyth_scheduler_get_schedule_list(mmyth_ui->mmyth_recordui->scheduler, &(schedule_list));
	if (res < 0) {
		g_warning ("[%s] Retrieved NULL list of scheduled data from database", __FUNCTION__);
		//return FALSE;
	}
	printf("\nX %d", res); fflush(stdout);
    
   	selection = 
        gtk_tree_view_get_selection(GTK_TREE_VIEW(mmyth_ui->mmyth_recordui->sch_treeview));
    
    gtk_tree_selection_get_selected(selection, &list_store, &iter);
	gtk_tree_model_get(list_store, &iter, 4, &index, -1);
    
    mmyth_ui_set_widget (mmyth_ui, mmyth_ui->schedule_uicommon);
    
	if (!(mmyth_ui->schedule_uicommon))
		mmyth_ui->schedule_uicommon = create_schedule_view(mmyth_ui);
		
	schedule_list = g_list_nth(schedule_list, g_ascii_strtoull( gtk_tree_path_to_string(path), NULL, 10 ));
	schedule_info = (ScheduleInfo*) schedule_list->data;

    printf("\nstarttime: %ld", schedule_info->start_time->tv_sec); fflush(stdout);
}

static MMythUiCommon *
create_schedule_view (MMythUi * mmyth_ui)
{
    MMythUiCommon *ui_common;        
    GtkWidget *schedule_widget;
    
    g_debug ("Creating Schedule UI Common");

	mmyth_ui->mmyth_schedulerui = mmyth_schedulerui_new ( mmyth_ui->backend_info );
	if (mmyth_ui->mmyth_schedulerui == NULL) {
		g_warning ("[%s] Error while creating scheduler ui", __FUNCTION__);
		return NULL;
	}
	
	schedule_widget = mmyth_ui->mmyth_schedulerui->main_widget;

    gtk_widget_show_all (schedule_widget);
    g_object_ref (schedule_widget);
    
    ui_common = mmyth_uicommon_new (schedule_widget,
                                    "Save", "Clear",
                                    "Cancel");    

    /* Button signals */
    g_signal_connect (G_OBJECT (ui_common->button1), "clicked",
                      G_CALLBACK (cb_save_new_schedule), mmyth_ui);
    g_signal_connect (G_OBJECT (ui_common->button2), "clicked",
                      G_CALLBACK (cb_not_impl_button), mmyth_ui);
    g_signal_connect (G_OBJECT (ui_common->button3), "clicked",
                      G_CALLBACK (cb_record_button), mmyth_ui);
    
    return ui_common;
}
/******************************************************************************
 *                         RECORD VIEW METHODS                                *
 ******************************************************************************/
static void
cb_record_button (GtkButton * button, gpointer user_data)
{
    MMythUi *mmyth_ui = (MMythUi *) user_data;

	if (!(mmyth_ui->record_uicommon)) {
		mmyth_ui->record_uicommon = create_record_view (mmyth_ui);
		mmyth_ui->schedule_uicommon = create_schedule_view (mmyth_ui);
	}
	
	mmyth_recordui_reload_all (mmyth_ui->mmyth_recordui);
	
    mmyth_ui_set_widget (mmyth_ui, mmyth_ui->record_uicommon);

} 

static void
cb_record_close_button (GtkButton * button, gpointer user_data)
{
    MMythUi *mmyth_ui = (MMythUi *) user_data;

	mmyth_ui_set_widget(mmyth_ui, mmyth_ui->main_uicommon);

	mmyth_recordui_free(mmyth_ui->mmyth_recordui);
	
	if (mmyth_ui->record_uicommon) {
		gtk_widget_destroy (mmyth_ui->record_uicommon->main_widget);
		mmyth_uicommon_free(mmyth_ui->record_uicommon);
		mmyth_ui->record_uicommon = NULL;
	}

	if (mmyth_ui->schedule_uicommon) {
	//	mmyth_uicommon_free(mmyth_ui->schedule_uicommon);
	}	
} 




static void
play_selected_recorded (gpointer user_data)
{
    MMythUi *mmyth_ui = (MMythUi *) user_data;
    gboolean res = FALSE;

    gchar *path = mmyth_recordui_get_selected_recorded (mmyth_ui->mmyth_recordui);
    
    if (path == NULL) {
    	// This should never happens. Play button is just activated when
    	// a recording is selected.
    	g_warning ("[%s] Play button pressed while none recorded is selected", __FUNCTION__);
    	return;
    }

	if (!(mmyth_ui->video_uicommon))
		mmyth_ui->video_uicommon = create_video_view (mmyth_ui);
	
	// Creates the tv player that will retrieve the mythtv content, decode and show it
	mmyth_ui->tvplayer = mmyth_tvplayer_new ();
	g_debug ("[%s] New TV Player created: %d\n", __FUNCTION__, (int) (mmyth_ui->tvplayer));
	res = mmyth_tvplayer_initialize (mmyth_ui->tvplayer, mmyth_ui->backend_info);
	if (!res) {
		g_warning ("[%s] Could not initialize tvplayer", __FUNCTION__);

		g_object_unref (mmyth_ui->tvplayer);
		mmyth_ui->tvplayer = NULL;

		GtkWidget *dialog = gtk_message_dialog_new (
				GTK_WINDOW(mmyth_ui->main_window),
				GTK_DIALOG_DESTROY_WITH_PARENT,
                GTK_MESSAGE_ERROR,
                GTK_BUTTONS_CLOSE,
			 	"MMyth found errors while starting TV Player, please check "
			 	"the GStreamer installation");

		gtk_dialog_run (GTK_DIALOG (dialog));
		gtk_widget_destroy (dialog);
		
		return;
	}		

	res = mmyth_tvplayer_record_setup (mmyth_ui->tvplayer, path);
	
	if (mmyth_ui && mmyth_ui->tvplayer && res) {
		mmyth_tvplayer_set_widget (mmyth_ui->tvplayer, mmyth_ui->videow);
		mmyth_tvplayer_start_playing (mmyth_ui->tvplayer);
	} else {
		// TODO: Show Alert with error description!
		g_warning ("[%s] MMythUI can't initialize tv_player", __FUNCTION__);
		g_object_unref (mmyth_ui->tvplayer);
		mmyth_ui->tvplayer = NULL;
		// FIXME: Show the exact error that happened
		GtkWidget *dialog = gtk_message_dialog_new (
				GTK_WINDOW(mmyth_ui->main_window),
				GTK_DIALOG_DESTROY_WITH_PARENT,
                GTK_MESSAGE_ERROR,
                GTK_BUTTONS_CLOSE,
                "Error while starting TV Player, please check if the backend"
                " is running properly and a tv card is available!");
		gtk_dialog_run (GTK_DIALOG (dialog));
		gtk_widget_destroy (dialog);		
		return;
	}
	
	mmyth_ui_set_widget (mmyth_ui, mmyth_ui->video_uicommon);    
}

static void
cb_play_clicked_recorded (GtkTreeView * tree_view, GtkTreePath *path, 
		GtkTreeViewColumn *column, gpointer user_data)
{
	play_selected_recorded (user_data);
}

static void
cb_play_selected (GtkButton * button, gpointer user_data)
{
	play_selected_recorded (user_data);
}

static void
cb_schedule_button (GtkButton * button, gpointer user_data)
{
    MMythUi *mmyth_ui = (MMythUi *) user_data;

    mmyth_ui_set_widget (mmyth_ui, mmyth_ui->schedule_uicommon);
}

void
cb_switch_page (GtkNotebook *notebook, GtkNotebookPage *page,
		guint page_num, gpointer user_data)
{
    MMythUi *mmyth_ui = (MMythUi *) user_data;
    MMythUiCommon *ui_common; 
    
	assert (mmyth_ui);
	assert (mmyth_ui->record_uicommon);
	
    ui_common = mmyth_ui->record_uicommon;
    
    if (page_num == 0) { // Switched to Schedule list
    	gtk_button_set_label (GTK_BUTTON (ui_common->button1), "New");
		g_signal_handlers_disconnect_by_func (
			G_OBJECT (ui_common->button1), G_CALLBACK (cb_play_selected), mmyth_ui);
	    g_signal_connect (G_OBJECT (ui_common->button1), "clicked",
            G_CALLBACK (cb_schedule_button), mmyth_ui);
    } else if (page_num == 1) {
    	gtk_button_set_label (GTK_BUTTON (ui_common->button1), "Play");
		g_signal_handlers_disconnect_by_func (
			G_OBJECT (ui_common->button1), G_CALLBACK (cb_schedule_button), mmyth_ui);
	    g_signal_connect (G_OBJECT (ui_common->button1), "clicked",
            G_CALLBACK (cb_play_selected), mmyth_ui);
    }
}
                                            
                                            
static MMythUiCommon *
create_record_view (MMythUi * mmyth_ui)
{
    MMythUiCommon *ui_common;        
    
   	g_debug ("Creating Record UI Common");
   	
	mmyth_ui->mmyth_recordui = mmyth_recordui_new ( mmyth_ui->backend_info );

	// FIXME: Change MMythRecordUI to a GtkWidget child and avoid this call!
    gtk_widget_show_all (mmyth_ui->mmyth_recordui->scrolled_window);
    
    ui_common = mmyth_uicommon_new (mmyth_ui->mmyth_recordui->scrolled_window, "New", "Delete", "<<Back");    
    g_object_ref (mmyth_ui->mmyth_recordui->scrolled_window);
        
    /* Button signals */
    g_signal_connect (G_OBJECT (ui_common->button1), "clicked",
                      G_CALLBACK (cb_schedule_button), mmyth_ui);
    g_signal_connect (G_OBJECT (ui_common->button2), "clicked",
                      G_CALLBACK (mmyth_recordui_delete_selected), mmyth_ui->mmyth_recordui);
    g_signal_connect (G_OBJECT (ui_common->button3), "clicked",
                      G_CALLBACK (cb_record_close_button), mmyth_ui);
    g_signal_connect (G_OBJECT (mmyth_ui->mmyth_recordui->notebook),
		      "switch-page", G_CALLBACK (cb_switch_page), mmyth_ui);
    g_signal_connect (G_OBJECT (mmyth_ui->mmyth_recordui->rec_treeview),
		      "row-activated", G_CALLBACK (cb_play_clicked_recorded), mmyth_ui);
    g_signal_connect (G_OBJECT (mmyth_ui->mmyth_recordui->sch_treeview),
		      "row-activated", G_CALLBACK (cb_edit_scheduled), mmyth_ui);
    return ui_common;
}


/******************************************************************************
 *                         GST VIDEO WIDGET METHODS                           *
 *****************************************************************************/

static void
cb_video_close_button (GtkButton * button, gpointer user_data)
{
    MMythUi *mmyth_ui = (MMythUi *) user_data;
	
	g_debug ("MMythUI video close button pressed");
	
	if (mmyth_ui && mmyth_ui->tvplayer) {
	    mmyth_tvplayer_stop_playing (mmyth_ui->tvplayer);

		g_object_unref (mmyth_ui->tvplayer);
		mmyth_ui->tvplayer = NULL;	    
	} else {
		g_warning ("cb_video_close_button called with NULL pointer\n");
	}
	
    mmyth_ui_set_widget (mmyth_ui, mmyth_ui->main_uicommon);
}


static MMythUiCommon *
create_video_view (MMythUi * mmyth_ui)
{

    MMythUiCommon *ui_common;

	g_debug ("Creating Video UI Common");
	
	/* Creates widget to be user by MMythTVPlayer to draw the video */
    mmyth_ui->videow = gtk_drawing_area_new ();
    // FIXME: Get the widget size from settings
    gtk_widget_set_size_request (mmyth_ui->videow, 300, 240);

    //mmiptv_ui->logo = gdk_pixbuf_new_from_file ("logo.png", NULL);

	// Creates an alignment to place the video widget inside
    mmyth_ui->video_alignment = gtk_alignment_new (0.5, 0.5, 1, 1);
    gtk_widget_hide (mmyth_ui->video_alignment);

    gtk_container_add (GTK_CONTAINER (mmyth_ui->video_alignment),
                       mmyth_ui->videow);

    /* Add the gst video widget to hbox. It should never be removed. */    
    /* FIXME: mmyth_ui->video_alignment == NULL when mmyth_player_init fails */
    if((mmyth_ui->main_hbox != NULL) && (mmyth_ui->video_alignment != NULL)) {
        gtk_box_pack_start (GTK_BOX (mmyth_ui->main_hbox),
                            mmyth_ui->video_alignment, TRUE, TRUE, 0);    
	} else {
		g_warning ("[%s] Error while adding video_alignment to main widget", __FUNCTION__);	
    }

    g_object_ref (mmyth_ui->videow);
    g_object_ref (mmyth_ui->video_alignment);

    ui_common = mmyth_uicommon_new (mmyth_ui->video_alignment,
                                    "  Full\nScreen", "Other\nServices",
                                    "Close");
                                    
    
    g_signal_connect (G_OBJECT (ui_common->button1), "clicked",
                      G_CALLBACK (cb_not_impl_button), mmyth_ui);
    g_signal_connect (G_OBJECT (ui_common->button2), "clicked",
                      G_CALLBACK (cb_not_impl_button), mmyth_ui);    
    g_signal_connect (G_OBJECT (ui_common->button3), "clicked",
                      G_CALLBACK (cb_video_close_button), mmyth_ui);

	if (ui_common)
		g_debug ("Video UI_Common sucessfull created");
		
    return ui_common;
}



GtkWidget*
mmyth_ui_get_video_widget (MMythUi *mmyth_ui) {
	
	if (mmyth_ui && mmyth_ui->videow) {
	
		return mmyth_ui->videow;
	}
	
	return NULL;	
}