/*  
 *  Copyright (C) 2004 Guillaume Cottenceau
 *
 *  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, version 2.
 *
 *  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 "config.h"

#include <stdio.h>
#include <string.h>

#include <gtk/gtk.h>
#include "xmms/i18n.h"
#include <xmms/plugin.h>
#include <xmms/util.h>
#include <xmms/configfile.h>
#include <xmms/xmmsctrl.h>

static void init(void);
static void about(void);
static void configure(void);
static int mod_samples(gpointer *d, gint length, AFormat afmt, gint srate, gint nch);

EffectPlugin stereo_ep =
{
	NULL,
	NULL,
	NULL, /* Description */
	init,
	NULL,
	about,
	configure,
	mod_samples
};

static const char *about_text = N_("Remove Advertisements Plugin\n"
                                   "\n"
                                   "Reduce volume of advertisements, detecting them by the\n"
                                   "presence of special words in title.\n"
                                   "\n"
				   "Copyright (c) Guillaume Cottenceau, 2004.\n"
                                   "This is free software under the GPL license.");

static GtkWidget *conf_dialog = NULL;
static GtkObject *adjustment;
static gint volume;
static int inited = 0;

gint ctrlsocket_get_session_id(void);           /* FIXME (coming from diskwriter output plugin) */

EffectPlugin *get_eplugin_info(void)
{
	stereo_ep.description =
		g_strdup_printf(_("Remove Advertisements Plugin %s"), VERSION);
	return &stereo_ep;
}

static void init(void)
{
	ConfigFile *cfg;
        cfg = xmms_cfg_open_default_file();
	if (!xmms_cfg_read_int(cfg, "remove_advertisement", "volume", &volume))
                volume = 2;
	xmms_cfg_free(cfg);
        inited = 1;
}

static void about(void)
{
	static GtkWidget *about_dialog = NULL;
	
	if (about_dialog != NULL)
		return;

	about_dialog = xmms_show_message(_("About Remove Advertisements Plugin"),
					 _(about_text), _("Ok"), FALSE,
					 NULL, NULL);
	gtk_signal_connect(GTK_OBJECT(about_dialog), "destroy",
			   GTK_SIGNAL_FUNC(gtk_widget_destroyed),
			   &about_dialog);
}

static void conf_ok_cb(GtkButton * button, gpointer data)
{
	ConfigFile *cfg;

	volume = (gint) GTK_ADJUSTMENT(adjustment)->value;
	
	cfg = xmms_cfg_open_default_file();
	xmms_cfg_write_int(cfg, "remove_advertisement", "volume", volume);
	xmms_cfg_write_default_file(cfg);
	xmms_cfg_free(cfg);
	gtk_widget_destroy(conf_dialog);
}

static void conf_cancel_cb(GtkButton * button, gpointer data)
{
	gtk_widget_destroy(conf_dialog);
}

static void conf_apply_cb(GtkButton *button, gpointer data)
{
	volume = (gint) GTK_ADJUSTMENT(adjustment)->value;
}

static void configure(void)
{
	GtkWidget *hbox, *label, *scale, *button, *bbox;

        if (!inited)
                init();

	if (conf_dialog != NULL)
		return;

	conf_dialog = gtk_dialog_new();
	gtk_signal_connect(GTK_OBJECT(conf_dialog), "destroy",
			   GTK_SIGNAL_FUNC(gtk_widget_destroyed), &conf_dialog);
	gtk_window_set_title(GTK_WINDOW(conf_dialog), _("Configure Extra Stereo"));

	label = gtk_label_new(_("Volume of advertisements:"));
	gtk_box_pack_start(GTK_BOX(GTK_DIALOG(conf_dialog)->vbox), label,
			   TRUE, TRUE, 0);
	gtk_widget_show(label);

	hbox = gtk_hbox_new(FALSE, 10);
	gtk_box_pack_start(GTK_BOX(GTK_DIALOG(conf_dialog)->vbox), hbox,
			   TRUE, TRUE, 10);
	gtk_widget_show(hbox);

	adjustment = gtk_adjustment_new(volume, 0, 100 + 10, 2, 10, 10);
	scale = gtk_hscale_new(GTK_ADJUSTMENT(adjustment));
	gtk_box_pack_start(GTK_BOX(hbox), scale, TRUE, TRUE, 10);
	gtk_widget_show(scale);

	bbox = gtk_hbutton_box_new();
	gtk_button_box_set_layout(GTK_BUTTON_BOX(bbox), GTK_BUTTONBOX_END);
	gtk_button_box_set_spacing(GTK_BUTTON_BOX(bbox), 5);
	gtk_box_pack_start(GTK_BOX((GTK_DIALOG(conf_dialog)->action_area)),
			   bbox, TRUE, TRUE, 0);

	button = gtk_button_new_with_label(_("Ok"));
	GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
	gtk_box_pack_start(GTK_BOX(bbox), button, TRUE, TRUE, 0);
	gtk_signal_connect(GTK_OBJECT(button), "clicked",
			   GTK_SIGNAL_FUNC(conf_ok_cb),
			   NULL);
	gtk_widget_grab_default(button);
	gtk_widget_show(button);

	button = gtk_button_new_with_label(_("Cancel"));
	GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
	gtk_box_pack_start(GTK_BOX(bbox), button, TRUE, TRUE, 0);
	gtk_signal_connect(GTK_OBJECT(button), "clicked",
			   GTK_SIGNAL_FUNC(conf_cancel_cb), NULL);
	gtk_widget_show(button);

	button = gtk_button_new_with_label(_("Apply"));
	GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
	gtk_box_pack_start(GTK_BOX(bbox), button, TRUE, TRUE, 0);
	gtk_signal_connect(GTK_OBJECT(button), "clicked",
			   GTK_SIGNAL_FUNC(conf_apply_cb),
                           NULL);
	gtk_widget_show(button);

	gtk_widget_show(bbox);

	gtk_widget_show(conf_dialog);
}

static int mod_samples(gpointer *d, gint length, AFormat afmt, gint srate, gint nch)
{
	gint i;
	gint16 *data = (gint16 *)*d;
        gfloat mul = ((gfloat)volume)/100;

        static int advertisement;
        static gchar* title = NULL;
        int remote_pos = xmms_remote_get_playlist_pos(ctrlsocket_get_session_id());
        gchar *remote_title = xmms_remote_get_playlist_title(ctrlsocket_get_session_id(), remote_pos);

        if (title == NULL || strcmp(title, remote_title)) {
                if (title)
                        g_free(title);
                title = g_strdup(remote_title);
                printf("title: %s\n", title);
                if (strstr(title, "Virgin Radio")) {
                        printf("shutting down for advertisement: %s\n", title);
                        advertisement = 1;
                } else {
                        printf("this is no advert: %s\n", title);
                        advertisement = 0;
                }
        }

        if (!advertisement)
                return length;

	if (!(afmt == FMT_S16_NE ||
	      (afmt == FMT_S16_LE && G_BYTE_ORDER == G_LITTLE_ENDIAN) ||
	      (afmt == FMT_S16_BE && G_BYTE_ORDER == G_BIG_ENDIAN))) {
                printf("unsupported audio format: %d\n", afmt);
		return length;
        }

        for (i = 0; i < length / 2; i ++)
                data[i] *= mul;

	return length;
}

