#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <poll.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <time.h>

#include <gtk/gtk.h>

#include "oswald-ui.h"
#include "Fonts.h" // the MetaWatch fonts

#define BITMAP_WIDTH	192
#define BITMAP_HEIGHT	192

void set_pixel(oswald_ui *ui, gint x, gint y, gboolean state)
{
	GdkRectangle update_rect;
	gint ix, iy;

	ix = x*2;
	iy = y*2;

	update_rect.x = ix - 5;
	update_rect.y = iy - 5;
	update_rect.width = 10;
	update_rect.height = 10;

	gdk_draw_point(GDK_DRAWABLE(ui->pixmap), state ? ui->darea->style->black_gc : ui->darea->style->white_gc, ix, iy);
	gdk_draw_point(GDK_DRAWABLE(ui->pixmap), state ? ui->darea->style->black_gc : ui->darea->style->white_gc, ix+1, iy);
	gdk_draw_point(GDK_DRAWABLE(ui->pixmap), state ? ui->darea->style->black_gc : ui->darea->style->white_gc, ix, iy+1);
	gdk_draw_point(GDK_DRAWABLE(ui->pixmap), state ? ui->darea->style->black_gc : ui->darea->style->white_gc, ix+1, iy+1);

	gtk_widget_draw (GTK_WIDGET(ui->darea), &update_rect);
}

void clear_display(oswald_ui *ui)
{
	GdkRectangle update_rect;

	update_rect.x = 0;
	update_rect.y = 0;
	update_rect.width = BITMAP_WIDTH;
	update_rect.height = BITMAP_HEIGHT;

	gdk_draw_rectangle (ui->pixmap,
		ui->darea->style->white_gc,
		TRUE,
		0, 0,
		ui->darea->allocation.width,
		ui->darea->allocation.height);

	gtk_widget_draw (GTK_WIDGET(ui->darea), &update_rect);
}

static gint
configure_event (GtkWidget *widget, GdkEventConfigure *event, gpointer user_data)
{
	oswald_ui *ui = (oswald_ui *)user_data;

	if (ui->pixmap)
		gdk_pixmap_unref(ui->pixmap);

	ui->pixmap = gdk_pixmap_new(widget->window,
		widget->allocation.width,
		widget->allocation.height,
		-1);
	gdk_draw_rectangle (ui->pixmap,
		widget->style->white_gc,
		TRUE,
		0, 0,
		widget->allocation.width,
		widget->allocation.height);

	return TRUE;
}

static gint
expose_event (GtkWidget *widget, GdkEventExpose *event, gpointer user_data)
{
	oswald_ui *ui = (oswald_ui *)user_data;

	gdk_draw_pixmap(widget->window,
		widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
		ui->pixmap,
		event->area.x, event->area.y,
		event->area.x, event->area.y,
		event->area.width, event->area.height);

	return FALSE;
}

static void create_mainwin(oswald_ui *ui)
{
	GtkWidget *hb, *vb, *btn;

	ui->pixmap = NULL;

	ui->mainwin = gtk_window_new (GTK_WINDOW_TOPLEVEL);
	// gtk_window_set_default_size (GTK_WINDOW (ui->mainwin), 440, 240);
	g_signal_connect(G_OBJECT(ui->mainwin), "destroy", gtk_main_quit, NULL);

	hb = gtk_hbox_new(FALSE, 5);
	gtk_container_add(GTK_CONTAINER(ui->mainwin), hb);

	vb = gtk_vbox_new(FALSE, 5);
	gtk_box_pack_start (GTK_BOX(hb), vb, FALSE, FALSE, 5);

	btn = gtk_button_new_with_label(" D ");
	gtk_box_pack_start (GTK_BOX(vb), btn, FALSE, FALSE, 10);

	btn = gtk_button_new_with_label(" E ");
	gtk_box_pack_start (GTK_BOX(vb), btn, FALSE, FALSE, 10);

	btn = gtk_button_new_with_label(" F ");
	gtk_box_pack_start (GTK_BOX(vb), btn, FALSE, FALSE, 10);

	ui->darea = gtk_drawing_area_new ();
	gtk_box_pack_start (GTK_BOX(hb), GTK_WIDGET(ui->darea), FALSE, FALSE, 5);
	gtk_drawing_area_size (GTK_DRAWING_AREA(ui->darea), BITMAP_WIDTH, BITMAP_HEIGHT);

	gtk_signal_connect (GTK_OBJECT (ui->darea), "expose_event", (GtkSignalFunc) expose_event, ui);
	gtk_signal_connect (GTK_OBJECT (ui->darea), "configure_event", (GtkSignalFunc) configure_event, ui);
	// gtk_signal_connect (GTK_OBJECT (drawing_area), "motion_notify_event", (GtkSignalFunc) motion_notify_event, ui);
	// gtk_signal_connect (GTK_OBJECT (drawing_area), "button_press_event", (GtkSignalFunc) button_press_event, ui);

	gtk_widget_set_events (GTK_WIDGET(ui->darea), GDK_EXPOSURE_MASK
		| GDK_LEAVE_NOTIFY_MASK
		| GDK_BUTTON_PRESS_MASK
		| GDK_POINTER_MOTION_MASK
		| GDK_POINTER_MOTION_HINT_MASK);

	vb = gtk_vbox_new(FALSE, 5);
	gtk_box_pack_start (GTK_BOX(hb), vb, FALSE, FALSE, 5);

	btn = gtk_button_new_with_label(" A ");
	gtk_box_pack_start (GTK_BOX(vb), btn, FALSE, FALSE, 10);

	btn = gtk_button_new_with_label(" B ");
	gtk_box_pack_start (GTK_BOX(vb), btn, FALSE, FALSE, 10);

	btn = gtk_button_new_with_label(" C ");
	gtk_box_pack_start (GTK_BOX(vb), btn, FALSE, FALSE, 10);

	gtk_widget_show_all(ui->mainwin);
}

gboolean app_tmo_handler (gpointer userdata)
{
	oswald_ui *ui = (oswald_ui *)userdata;

	// fprintf(stderr, "tmo...\n");

	if (ui->OnIdleScreen) {
		update_idle_time_date(ui);
#if 0
	} else {
		// go back to idle screen after IDLE_TIMEOUT seconds
		if ((time(NULL) - ui->idle_tmo) > ui->conf.idle_tmo /*IDLE_TIMEOUT*/)
			create_main_screen(ui);
#endif
	}

	return TRUE;
}

int main(int argc , char ** argv)
{
	oswald_ui ui;

	gtk_init (&argc, &argv);

	create_mainwin(&ui);

	//set_pixel(&ui, 48, 48, TRUE);
	// demo_time(&ui);
	ui.OnIdleScreen = TRUE;
	g_timeout_add_seconds(1, app_tmo_handler, &ui);

	gtk_main ();
	return 0;
}