/*  Audtool -- Audacious scripting tool
 *  Copyright (c) 2005  William Pitcock
 *
 *  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.
 */

#include <stdlib.h>
#include <glib.h>
#include <audacious/beepctrl.h>
#include "audtool.h"

struct commandhandler handlers[] = {
	{"current-song", get_current_song},
	{"current-song-length", get_current_song_length},
	{"current-song-length-seconds", get_current_song_length_seconds},
	{"current-song-length-frames", get_current_song_length_frames},
	{"current-song-output-length", get_current_song_output_length},
	{"current-song-output-length-seconds", get_current_song_output_length_seconds},
	{"current-song-output-length-frames", get_current_song_output_length_frames},
	{"current-song-bitrate", get_current_song_bitrate},
	{"current-song-frequency", get_current_song_frequency},
	{"current-song-channels", get_current_song_channels},
	{"playlist-advance", playlist_advance},
	{"playlist-reverse", playlist_reverse},
	{"playlist-addurl", playlist_add_url_string},
	{"playlist-length", playlist_length},
	{"playlist-display", playlist_display},
	{"playlist-position", playlist_position},
	{"playlist-jump", playlist_jump},
	{"playback-play", playback_play},
	{"playback-pause", playback_pause},
	{"playback-stop", playback_stop},
	{"get-volume", get_volume},
	{"set-volume", set_volume},
	{"preferences", show_preferences_window},
	{"shutdown", shutdown_audacious_server},
	{"list-handlers", get_handlers_list},
	{"help", get_handlers_list},
	{NULL, NULL}
};

gint main(gint argc, gchar **argv)
{
	gint i;

	if (argc < 2)
	{
		g_printf("%s: usage: %s <command>\n", argv[0], argv[0]);
		g_printf("%s: use `%s help' to get a listing of available commands.\n",
			argv[0], argv[0]);
		exit(0);
	}

	if (!xmms_remote_is_running(0) && strcasecmp("help", argv[1])
		&& strcasecmp("list-handlers", argv[1]))
	{
		g_printf("%s: audacious server is not running!\n", argv[0]);
		exit(0);
	}

	for (i = 0; handlers[i].name != NULL; i++)
	{
		if (!strcasecmp(handlers[i].name, argv[1]))
			handlers[i].handler(0, argc, argv);
	}

	return 0;
}

/*** MOVE TO HANDLERS.C ***/

void get_current_song(gint session, gint argc, gchar **argv)
{
	gint playpos = xmms_remote_get_playlist_pos(session);
	gchar *song = xmms_remote_get_playlist_title(session, playpos);

	if (!song)
	{
		g_printf("No song playing.\n");
		return;
	}

	g_printf("%s\n", song);
}

void get_current_song_output_length(gint session, gint argc, gchar **argv)
{
	gint frames = xmms_remote_get_output_time(session);
	gint length = frames / 1000;

	g_printf("%d:%.2d\n", length / 60, length % 60);
}

void get_current_song_output_length_seconds(gint session, gint argc, gchar **argv)
{
	gint frames = xmms_remote_get_output_time(session);
	gint length = frames / 1000;

	g_printf("%d\n", length);
}

void get_current_song_output_length_frames(gint session, gint argc, gchar **argv)
{
	gint frames = xmms_remote_get_output_time(session);

	g_printf("%d\n", frames);
}

void get_current_song_length(gint session, gint argc, gchar **argv)
{
	gint playpos = xmms_remote_get_playlist_pos(session);
	gint frames = xmms_remote_get_playlist_time(session, playpos);
	gint length = frames / 1000;

	g_printf("%d:%.2d\n", length / 60, length % 60);
}

void get_current_song_length_seconds(gint session, gint argc, gchar **argv)
{
	gint playpos = xmms_remote_get_playlist_pos(session);
	gint frames = xmms_remote_get_playlist_time(session, playpos);
	gint length = frames / 1000;

	g_printf("%d\n", length);
}

void get_current_song_length_frames(gint session, gint argc, gchar **argv)
{
	gint playpos = xmms_remote_get_playlist_pos(session);
	gint frames = xmms_remote_get_playlist_time(session, playpos);

	g_printf("%d\n", frames);
}

void get_current_song_bitrate(gint session, gint argc, gchar **argv)
{
	gint rate, freq, nch;
	
	xmms_remote_get_info(session, &rate, &freq, &nch);

	g_printf("%d\n", rate);
}

void get_current_song_frequency(gint session, gint argc, gchar **argv)
{
	gint rate, freq, nch;

	xmms_remote_get_info(session, &rate, &freq, &nch);

	g_printf("%d\n", freq);
}

void get_current_song_channels(gint session, gint argc, gchar **argv)
{
	gint rate, freq, nch;

	xmms_remote_get_info(session, &rate, &freq, &nch);

	g_printf("%d\n", nch);
}

void playlist_reverse(gint session, gint argc, gchar **argv)
{
	xmms_remote_playlist_prev(session);
}

void playlist_advance(gint session, gint argc, gchar **argv)
{
	xmms_remote_playlist_next(session);
}

void playback_play(gint session, gint argc, gchar **argv)
{
	xmms_remote_play(session);
}

void playback_pause(gint session, gint argc, gchar **argv)
{
	xmms_remote_pause(session);
}

void playback_stop(gint session, gint argc, gchar **argv)
{
	xmms_remote_stop(session);
}

void playlist_add_url_string(gint session, gint argc, gchar **argv)
{
	if (argc < 3)
	{
		g_printf("%s: invalid parameters for playlist-addurl.\n", argv[0]);
		g_printf("%s: syntax: %s playlist-addurl <url>\n", argv[0], argv[0]);
		return;
	}

	xmms_remote_playlist_add_url_string(session, argv[2]);
}

void playlist_length(gint session, gint argc, gchar **argv)
{
	gint i;

	i = xmms_remote_get_playlist_length(session);

	g_printf("%d\n", i);
}

void playlist_display(gint session, gint argc, gchar **argv)
{
	gint i, ii, frames, length, total;
	gchar *songname;
	
	i = xmms_remote_get_playlist_length(session);

	g_printf("%d tracks.\n", i);

	total = 0;

	for (ii = 0; ii < i; ii++)
	{
		songname = xmms_remote_get_playlist_title(session, ii);
		frames = xmms_remote_get_playlist_time(session, ii);
		length = frames / 1000;
		total += length;

		g_printf("%-4d | %-60s | %d:%.2d\n",
			ii + 1, songname, length / 60, length % 60);
	}

	g_printf("Total length: %d:%.2d\n", total / 60, total % 60);
}

void playlist_position(gint session, gint argc, gchar **argv)
{
	gint i;

	i = xmms_remote_get_playlist_pos(session);

	g_printf("%d\n", i+1);
}

void playlist_jump(gint session, gint argc, gchar **argv)
{
	gint i;

        if (argc < 3)
        {
                g_printf("%s: invalid parameters for playlist-jump.\n", argv[0]);
                g_printf("%s: syntax: %s playlist-jump <slot>\n", argv[0], argv[0]);
                return;
        }

	i = atoi(argv[2]);

	if (i < 1)
	{
                g_printf("%s: invalid playlist position %d", argv[0], i);
		return;
	}

       	xmms_remote_set_playlist_pos(session, i-1);
}

void get_volume(gint session, gint argc, gchar **argv)
{
	gint i;

	i = xmms_remote_get_main_volume(session);

	g_printf("%d\n", i);
}

void set_volume(gint session, gint argc, gchar **argv)
{
	gint i;

	if (argc < 3)
	{
		g_printf("%s: invalid parameters for set-volume.\n", argv[0]);
		g_printf("%s: syntax: %s set-volume <level>\n", argv[0], argv[0]);
		return;
	}		

	i = atoi(argv[2]);

	xmms_remote_set_main_volume(session, i);
}

void show_preferences_window(gint session, gint argc, gchar **argv)
{
	xmms_remote_show_prefs_box(session);
}

void shutdown_audacious_server(gint session, gint argc, gchar **argv)
{
	xmms_remote_quit(session);
}

void get_handlers_list(gint session, gint argc, gchar **argv)
{
	gint i;

	g_printf("Available handlers:\n\n");

	for (i = 0; handlers[i].name != NULL; i++)
		g_printf("\t%s\n", handlers[i].name);
}
