/*
 * Copyright (C) 2009, 2010 by Nicole Faerber <nicole.faerber@dpin.de>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 of the License, or (at your option) any later version.
 *
 */

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

#include <gtk/gtk.h>

#include <sqlite3.h>

#include "rds.h"
#include "tmc.h"

sqlite3 *lcl_db;
int OutputFlags;

typedef struct {
	int radio_fd;
	guint r_input_id;
	GtkBuilder *builder;
	GtkWidget *MainWin;
	GtkWidget *RDS_TP_Label;
	GtkWidget *RDS_TA_Label;
	GtkWidget *RDS_M_Label;
	GtkWidget *RDS_S_Label;
	GtkWidget *RDS_SID_Label;
	GtkWidget *RDS_RT_Label;
	GtkWidget *RDS_PTY_Label;
	GtkWidget *RDS_Date_Time_Label;
	GtkWidget *RDS_PI_Label;
	GtkWidget *RDS_PType_Label;
	GtkWidget *TMC_View;
	GtkTextBuffer *TMC_MSG_Buffer;
} uberradio_ui;


int open_radio(const char *name)
{
int fd;

	fd = open(name, O_RDONLY | O_NONBLOCK);
	if (fd > 0)
		return fd;
	else
		return 0;
}


void handle_r_input(gpointer user_data, gint source, GdkInputCondition condition)
{
uberradio_ui *rui = (uberradio_ui *)user_data;
static unsigned short rdsgroup[4];

	if (rds_receive_group(rui->radio_fd, rdsgroup)) {
		rds_decode_group(rdsgroup);
	}
}

void rds_sname_cb(char *sname, void *user_data)
{
uberradio_ui *rui = (uberradio_ui *)user_data;
static char pbuf[128];

	snprintf(pbuf, 128, "<tt>%s</tt>", sname);
	gtk_label_set_markup(GTK_LABEL(rui->RDS_SID_Label), pbuf);
}


void rds_radiotext_cb(char *rtext, void *user_data)
{
uberradio_ui *rui = (uberradio_ui *)user_data;
static char pbuf[128];

	snprintf(pbuf, 128, "<tt>%s</tt>", rtext);
	gtk_label_set_markup(GTK_LABEL(rui->RDS_RT_Label), pbuf);
}

void rds_sinfo_cb(unsigned char TP, unsigned char TA, unsigned char MS, unsigned char PTY, void *user_data)
{
uberradio_ui *rui = (uberradio_ui *)user_data;

	if (TP) {
		gtk_widget_set_sensitive(rui->RDS_TP_Label, TRUE);
	} else {
		gtk_widget_set_sensitive(rui->RDS_TP_Label, FALSE);
	}
	if (TA) {
		gtk_widget_set_sensitive(rui->RDS_TA_Label, TRUE);
	} else {
		gtk_widget_set_sensitive(rui->RDS_TA_Label, FALSE);
	}
	if (MS) {
		gtk_widget_set_sensitive(rui->RDS_M_Label, TRUE);
		gtk_widget_set_sensitive(rui->RDS_S_Label, FALSE);
	} else {
		gtk_widget_set_sensitive(rui->RDS_M_Label, FALSE);
		gtk_widget_set_sensitive(rui->RDS_S_Label, TRUE);
	}
	gtk_label_set_text(GTK_LABEL(rui->RDS_PTY_Label), PTY_text[PTY]);
}

void rds_date_time_cb(struct tm *rds_time, void *user_data)
{
uberradio_ui *rui = (uberradio_ui *)user_data;
char pbuf[128];
char pbuf2[128];
gchar *pstr;
GDate mgdate;

	mgdate.julian = 0;
	mgdate.dmy = 1;
	mgdate.day = rds_time->tm_mday;
	mgdate.month = rds_time->tm_mon + 1;
	mgdate.year = rds_time->tm_year + 1900;
	g_date_strftime(pbuf, 128, "%A %d. %B %Y ", &mgdate);
	strftime(pbuf2, 128, "%R", rds_time);
	pstr = g_strconcat(pbuf, pbuf2, NULL);
	gtk_label_set_markup(GTK_LABEL(rui->RDS_Date_Time_Label), pstr);
	g_free(pstr);
}

void rds_PI_cb(unsigned short PI, unsigned char ccode, unsigned char ptype, unsigned char pref, void *user_data)
{
uberradio_ui *rui = (uberradio_ui *)user_data;
char pbuf[128];

	// we get this callback if PI changed which also means that
	// the station changed, which again means that all previous
	// information in the display is invalid now

	gtk_widget_set_sensitive(rui->RDS_TP_Label, FALSE);
	gtk_widget_set_sensitive(rui->RDS_TA_Label, FALSE);
	gtk_widget_set_sensitive(rui->RDS_M_Label, FALSE);
	gtk_widget_set_sensitive(rui->RDS_S_Label, FALSE);
	gtk_label_set_markup(GTK_LABEL(rui->RDS_RT_Label), "");
	gtk_label_set_text(GTK_LABEL(rui->RDS_PTY_Label), "");
	gtk_label_set_markup(GTK_LABEL(rui->RDS_SID_Label), "");
	gtk_text_buffer_set_text(rui->TMC_MSG_Buffer, "", -1);

	snprintf(pbuf, 128, "<b>PI</b> %d", PI);
	gtk_label_set_markup(GTK_LABEL(rui->RDS_PI_Label), pbuf);
	gtk_label_set_markup(GTK_LABEL(rui->RDS_PType_Label), ptype_ltext[ptype]);
}

void tmc_msg_cb(char *msg, void *user_data)
{
uberradio_ui *rui = (uberradio_ui *)user_data;

	gtk_text_buffer_set_text(rui->TMC_MSG_Buffer, msg, -1);
}

int main(int argc, char **argv)
{
GtkWidget *w;
uberradio_ui rui;

	gtk_init (&argc, &argv);

	rui.builder = gtk_builder_new();
	if (!gtk_builder_add_from_file(rui.builder, "uberradio.ui", NULL)) {
		fprintf(stderr, "Error creating GtkBuilder\n");
		return 0;
	}
	gtk_builder_connect_signals(rui.builder, NULL);

	rui.RDS_TP_Label = GTK_WIDGET(gtk_builder_get_object(rui.builder, "RDS_TP_Label"));
	gtk_widget_set_sensitive(rui.RDS_TP_Label, FALSE);
	rui.RDS_TA_Label = GTK_WIDGET(gtk_builder_get_object(rui.builder, "RDS_TA_Label"));
	gtk_widget_set_sensitive(rui.RDS_TA_Label, FALSE);
	rui.RDS_M_Label = GTK_WIDGET(gtk_builder_get_object(rui.builder, "RDS_M_Label"));
	gtk_widget_set_sensitive(rui.RDS_M_Label, FALSE);
	rui.RDS_S_Label = GTK_WIDGET(gtk_builder_get_object(rui.builder, "RDS_S_Label"));
	gtk_widget_set_sensitive(rui.RDS_S_Label, FALSE);
	rui.RDS_SID_Label = GTK_WIDGET(gtk_builder_get_object(rui.builder, "RDS_SID_Label"));
	rui.RDS_RT_Label = GTK_WIDGET(gtk_builder_get_object(rui.builder, "RDS_RT_Label"));
	rui.RDS_PTY_Label = GTK_WIDGET(gtk_builder_get_object(rui.builder, "RDS_PTY_Label"));
	rui.RDS_Date_Time_Label = GTK_WIDGET(gtk_builder_get_object(rui.builder, "RDS_Date_Time_Label"));
	rui.RDS_PI_Label = GTK_WIDGET(gtk_builder_get_object(rui.builder, "RDS_PI_Label"));
	rui.RDS_PType_Label = GTK_WIDGET(gtk_builder_get_object(rui.builder, "RDS_PType_Label"));

	rui.TMC_View = GTK_WIDGET(gtk_builder_get_object(rui.builder, "TMC_View"));
	rui.TMC_MSG_Buffer = GTK_TEXT_BUFFER(gtk_builder_get_object(rui.builder, "TMC_MSG_Buffer"));

	rui.MainWin = GTK_WIDGET(gtk_builder_get_object(rui.builder, "MainWin"));

	gtk_widget_show_all(rui.MainWin);

	if (!(rui.radio_fd = open_radio("/dev/radio0"))) {
		perror("open radio:");
		return -1;
	}

	if (sqlite3_open("lcl.db", &lcl_db) != SQLITE_OK) {
		perror("open lcl.db");
		close(rui.radio_fd);
		return -1;
	}

	rds_init();
	tmc_init();

	rui.r_input_id = gdk_input_add (rui.radio_fd, GDK_INPUT_READ, handle_r_input, &rui);

	rds_set_sname_cb(rds_sname_cb, &rui);
	rds_set_radiotext_cb(rds_radiotext_cb, &rui);
	rds_set_date_time_cb(rds_date_time_cb, &rui);
	rds_set_sinfo_cb(rds_sinfo_cb, &rui);
	rds_set_PI_cb(rds_PI_cb, &rui);

	tmc_set_msg_cb(tmc_msg_cb, &rui);

	gtk_main();

return 0;
}
