/* 
 *  beboCD  - GTK2 and libcdaudio based cdplayer
 *  
 *  Miroslav Yordanov 2003
 *  <mironcho@users.sourceforge.net>
 *
 *  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.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.*
 */


#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <gtk/gtk.h>
#include <cdaudio.h>
#include <string.h>
#include <unistd.h>

#include "callbacks.h"
#include "interface.h"
#include "support.h"


void
bebo_prev_track                        (GtkButton       *button,
                                        gpointer         user_data)
{
	POLL_CD;

	if ( (cdstatu.status_current_track > 1) 
			&& (cdstatu.status_current_track <= cdinf.disc_total_tracks) ) {
		
		PLAY_CD (cdstatu.status_current_track - 1);
	}
}


void
bebo_pause_cd                          (GtkButton       *button,
                                        gpointer         user_data)
{
	POLL_CD;

	if ( cdstatu.status_mode == CDAUDIO_PLAYING ) {
		PAUSE_CD;

	} else if ( cdstatu.status_mode == CDAUDIO_PAUSED ) {
		if ( -1 == cd_resume (cd) ) {
			g_print ("Couldn't resume cd !\n");
		}
	}
}


void
bebo_play_cd                           (GtkButton       *button,
                                        gpointer         user_data)
{
	POLL_CD;

	if ( cdstatu.status_mode == CDAUDIO_PLAYING ) {
		/*
		 *   nothing to do !!! allready playing...
		 */
	} else if ( cdstatu.status_mode == CDAUDIO_PAUSED ) {
		if ( -1 == cd_resume (cd) ) {
			g_print ("Couldn't resume cd !\n");
		}
	} else {
		if ( -1 == cd_play (cd, cdstatu.status_current_track) ) {
			g_print ("Couldn't play cd !\n");
		}
	}
}


void
bebo_stop_cd                           (GtkButton       *button,
                                        gpointer         user_data)
{
	STOP_CD;
}


void
bebo_next_track                        (GtkButton       *button,
                                        gpointer         user_data)
{
	POLL_CD;

	if ( (cdstatu.status_current_track >= 1) 
			&& (cdstatu.status_current_track < cdinf.disc_total_tracks) ) {
		
		PLAY_CD (cdstatu.status_current_track + 1);
	}
}


void
bebo_eject_cd                          (GtkButton       *button,
                                        gpointer         user_data)
{
	POLL_CD;
    
	if ( cdstatu.status_present ) {
		if ( -1 == cd_eject (cd) ) {
			g_print ("Couldn't eject cd !\n");
		}
	} else {
		if ( -1 == cd_close (cd) ) {
			g_print ("Couldn't close cd !\n");
		}
	}
}


gboolean
bebo_cd_quit                           (GtkWidget       *widget,
                                        GdkEvent        *event,
                                        gpointer         user_data)
{
	cd_finish (cd);
	gtk_main_quit();
	
	return FALSE;
}


/*
 * Actions taken every 500 miliseconds ;)
 */
gint
loop_action				(gpointer	data)
{
	GList *bebo_list = NULL;
	
	static gboolean bebo_have_cd = FALSE;
	static guint bc_track = 1;
	gchar cmin[4], csec[4];
	gchar lbuff[100][254], buff[1024];
	guchar i;

	static struct disc_data data;


	POLL_CD;
	
	if ( !cdstatu.status_present ) {
		/* no cd in cdrom (hope so ;) */
		bebo_have_cd = FALSE;
		gtk_label_set_text(GTK_LABEL (lookup_widget(bebocd, "label4")), "NO DISC");
		
	} else {
		if ( !bebo_have_cd ) {
			STAT_CD;
			bebo_have_cd = TRUE;
			bc_track = 1;

			//gtk_label_set_text(GTK_LABEL (lookup_widget(bebocd, "label4")), "Looking CDDB..");

			if ( -1 == cddb_read_disc_data (cd, &data)) {
				g_print ("CDDB  error!\n");
			}
			if ( !strncmp (data.data_title, "", 2) ) {
				for (i = 0; i < cdinf.disc_total_tracks; i++) {
					strncpy (data.data_track[i].track_name, "Unknown", 8);
					strncpy (data.data_track[i].track_artist, "Unknown", 8);
				}
				strncpy (data.data_artist, "Unknown artist", 15);
				strncpy (data.data_title, "Unknown", 8);
			}
			
			
			/* 
			 * well, we have cdinfo, now we can create 
			 * song list...
			 */
			for (i = 0; i < cdinf.disc_total_tracks; i++) {
				snprintf (lbuff[i], 253, _("%d. %s"), i + 1,
						bebo_to_utf (data.data_track[i].track_name) );
				bebo_list = g_list_append (bebo_list, lbuff[i]);
			}
			gtk_combo_set_popdown_strings (GTK_COMBO 
					(lookup_widget (bebocd, "combo1")), bebo_list);

			//GTK_ENTRY (lookup_widget (bebocd, "combo_entry1"))->overwrite_mode = 0;
		}


		if ( 10 > cdstatu.status_track_time.minutes ) {
			snprintf (cmin, 3, "0%d", cdstatu.status_track_time.minutes);
		} else {
			snprintf (cmin, 3, "%d", cdstatu.status_track_time.minutes);
		}
		
		if ( 10 > cdstatu.status_track_time.seconds ) {
			snprintf (csec, 3, "0%d", cdstatu.status_track_time.seconds);
		} else {
			snprintf (csec, 3, "%d", cdstatu.status_track_time.seconds);
		}
		
		sprintf ( buff,
				" Track %d / %d\t\t%s : %s \n\n %s \n %s ", 
				cdstatu.status_current_track,
				cdinf.disc_total_tracks,
				cmin, csec,
				bebo_to_utf (data.data_artist),
				bebo_to_utf (data.data_title) ); 
		gtk_label_set_text(GTK_LABEL (lookup_widget(bebocd, "label4")), buff);
		
		/* we'll update track info in combo only track changed... */
		if ( bc_track != cdstatu.status_current_track ) {
			dont_change_song = TRUE;
			BEBO_VIEW_TRACK_INFO (cdstatu.status_current_track - 1);
			dont_change_song = FALSE;

			bc_track = cdstatu.status_current_track;
		}
	}

	/* we need to return TRUE */
	return TRUE;
}


void
bebo_comboentry_activate               (GtkEditable     *editable,
                                        gpointer         user_data)
{	
	static gboolean bebo_have_cd_first = FALSE;
	guint i, num;
	gchar *e, *buff;

	if ( !bebo_have_cd_first || dont_change_song ) {
		bebo_have_cd_first = TRUE;
		return;
	}

	STOP_CD;
	
	buff = g_malloc0 (sizeof(gchar) * 6);
	e = gtk_editable_get_chars (GTK_EDITABLE (editable), 0, 254);

	/* 
	 * Well. I think I'm shure there is "."
	 * I'll re-think it.
	 * TODO HERE.....
	 */
	for (i = 0; *e, i < 4; e++, i++ ) {
		if ( !strncmp (e, ".", 1) ) {
			break;
		} else {
			strncat (buff, e, 1);
		}
	}
	num = atoi (buff);

	if ( -1 == cd_play (cd, num) ) {
		g_print ("Couldn't play cd !\n");
	}
	
	g_free (buff);
}


void
bebo_show_aboutdlg                     (GtkButton       *button,
                                        gpointer         user_data)
{
	if ( aboutdlg_is_shown) {
		return;
	}
	
	aboutdlg = create_dialog1 ();
	gtk_widget_show (aboutdlg);

	aboutdlg_is_shown = TRUE;
}


gboolean
bebo_hide_aboutdlg                     (GtkWidget       *widget,
                                        GdkEvent        *event,
                                        gpointer         user_data)
{
	gtk_widget_destroy (aboutdlg);
	aboutdlg_is_shown = FALSE;

	return FALSE;
}




/* 
 *  Volume get/set functions... preaty easy with libcdaudio ;)
 */
void
bebo_set_mixer                         (GtkRange        *range,
                                        gpointer         user_data)
{
	struct disc_volume volume;
	guint i;

	if ( -1 == cd_get_volume (cd, &volume) ) {
		g_print ("Couldn't get volume !\n");
		return;
	}
	
	i = (gint) gtk_range_get_value (range);
	volume.vol_front.right = i;
	volume.vol_front.left = i;

	if ( -1 == cd_set_volume (cd, volume) ) {
		g_print ("Couldn't set volume !\n");
	}
}


void
bebo_get_current_cdvolume		(void)
{
	struct disc_volume volume;

	if ( -1 == cd_get_volume (cd, &volume) ) {
		g_print ("Couldn't get volume !\n");
		return;
	}

	gtk_range_set_value (GTK_RANGE (lookup_widget(bebocd, "hscale1")), 150);
}


gchar
*bebo_to_utf 				(gchar *str)
{
	gchar *charset;
	gint len = strlen (str);

	/* FIXME - this function must check for text charset
	 * not for locale.... 
	 *
	 */
	
	return (gchar*) g_locale_to_utf8 (str, len, NULL, NULL, NULL);
}
