#include "tags.h"
#include "tagsgetter.h"
#include <tdelocale.h>
#include <tqslider.h>
#include <tqspinbox.h>
#include <tdeconfig.h>
#include <tqlayout.h>
#include <tqhbox.h>
#include <tqlabel.h>
#include <tqtimer.h>
#include <noatun/player.h>
#include <tqcheckbox.h>

TagsGetter *Tags::getter=0;

TagsGetter::TagsGetter()
{
	new Control(this);
	connect(napp->player(), TQ_SIGNAL(newSong()), TQ_SLOT(newSong()));
}

int TagsGetter::interval() const
{
	TDEGlobal::config()->setGroup("Tags");
	return TDEGlobal::config()->readNumEntry("interval", 250);
}

bool TagsGetter::loadAuto() const
{
	TDEGlobal::config()->setGroup("Tags");
	return TDEGlobal::config()->readBoolEntry("LoadAuto", true);
}

void TagsGetter::added(PlaylistItem &i)
{
	items += i;
	killTimers();
	startTimer(interval());
}

void TagsGetter::removed(PlaylistItem &i)
{
	items.remove(i);
}

void TagsGetter::getSongs()
{
	items = napp->playlist()->select("Tags::tagged_", "", -1, true, true);
	killTimers();
	startTimer(interval());
}

void TagsGetter::timerEvent(TQTimerEvent *)
{
	if (!items.size())
	{
		killTimers();
		return;
	}

	PlaylistItem item=items.first();
	for (Tags *i=tags.first(); i; i=tags.next())
	{
		if (i->update(item))
		{
			item.setProperty("Tags::tagged_", "1");
			if (item==napp->player()->current())
				napp->player()->handleButtons();
		}
	}

	items.remove(items.begin());
}

void TagsGetter::newSong()
{
	PlaylistItem item=napp->player()->current();
	if (!item) return;
	
	for (Tags *i=tags.first(); i; i=tags.next())
	{
		if (i->update(item))
		{
			item.setProperty("Tags::tagged_", "1");
			napp->player()->handleButtons();
		}
	}
	items.remove(item);
}

void TagsGetter::setInterval(int ms)
{
	killTimers();
	startTimer(ms);
	
	TDEGlobal::config()->setGroup("Tags");
	TDEGlobal::config()->writeEntry("interval", ms);
	TDEGlobal::config()->sync();
}

void TagsGetter::setLoadAuto(bool eh)
{
	
	TDEGlobal::config()->setGroup("Tags");
	TDEGlobal::config()->writeEntry("LoadAuto", eh);
	TDEGlobal::config()->sync();
	
	killTimers();
	
	if (eh) startTimer(interval());
}

void TagsGetter::associate(Tags *t)
{
	tags.append(t);
	sortPriority();
//	getSongs();
	TQTimer::singleShot(interval(), this, TQ_SLOT(getSongs()));
}

void TagsGetter::sortPriority()
{
	// find the lowest one, since it comes first
	
	int lowest=0;
	for (Tags *i=tags.first(); i; i=tags.next())
	{
		if (lowest>i->mPriority)
			lowest=i->mPriority;
	}
	
	TQPtrList<Tags> sorted;
	while (tags.count())
	{
		// find the one equal to myself
		for (Tags *i=tags.first(); i;)
		{
			if (lowest==i->mPriority)
			{
				sorted.append(i);
				tags.removeRef(i);
				i=tags.first();
			}
			else
			{
				i=tags.next();
			}
		}
		lowest++;
	}
	
	tags=sorted;
}

bool TagsGetter::unassociate(Tags *t)
{
	tags.removeRef(t);
	if (tags.count()==0)
	{
		delete this;
		return true;
	}
	return false;
}

Tags::Tags(int priority) : mPriority(priority)
{
	if (!getter)
		getter=new TagsGetter;
	getter->associate(this);
}

Tags::~Tags()
{
	if (getter->unassociate(this))
		getter=0;
}


Control::Control(TagsGetter *parent)
	: CModule(i18n("Tagging"), i18n("Settings for Tag Loaders"), "edit", parent)
{
	// todo
	(void)I18N_NOOP("Rescan All Tags");
	
	TQVBoxLayout *l=new TQVBoxLayout(this);
	TQCheckBox *onPlay;
	{
		onPlay=new TQCheckBox(i18n("Load tags &automatically"), this);
		l->addWidget(onPlay);
		onPlay->show();
	}
	
	{
		TQHBox *intervalLine=new TQHBox(this);
		l->addWidget(intervalLine);
		l->addStretch();
	
		new TQLabel(i18n(
				"The time between each time noatun scans for a new file"
				", and updates tags (e.g., ID3)",
				"Interval:"), intervalLine);
		TQSlider *slider=new TQSlider(
				0, 2000, 100, 0,TQt::Horizontal, intervalLine
			);
		TQSpinBox *spin=new TQSpinBox(
				0, 2000, 10, intervalLine
			);
		
		spin->setSuffix(i18n("Milliseconds", " ms"));
		
		
		connect(slider, TQ_SIGNAL(valueChanged(int)), spin, TQ_SLOT(setValue(int)));
		connect(spin, TQ_SIGNAL(valueChanged(int)), slider, TQ_SLOT(setValue(int)));
		slider->setValue(parent->interval());
		connect(slider, TQ_SIGNAL(valueChanged(int)), parent, TQ_SLOT(setInterval(int)));
	
		connect(onPlay, TQ_SIGNAL(toggled(bool)), intervalLine, TQ_SLOT(setEnabled(bool)));
	}
	connect(onPlay, TQ_SIGNAL(toggled(bool)), parent, TQ_SLOT(setLoadAuto(bool)));

	onPlay->setChecked(parent->loadAuto());
}




#include "tagsgetter.moc"

