#include <noatun/engine.h>
#include <noatun/player.h>
#include <noatun/app.h>

#include <math.h>
#include <tqpainter.h>
#include <tqbitmap.h>
#include <tdelocale.h>
#include <tdeconfig.h>
#include <tqcursor.h>
#include <tqtooltip.h>
#include <twin.h>


#include "ffrs.h"

extern "C"
{
	TDE_EXPORT Plugin* create_plugin()
	{
		TDEGlobal::locale()->insertCatalogue("ffrs");
		return new FFRS();
	}
}


View::View(int width, int height, int block, int unblock, TQColor front, TQColor back, int channel)
	: TQWidget(0,0, TQt::WStyle_Customize | TQt::WStyle_NoBorder | TQt::WStyle_StaysOnTop | TQt::WType_TopLevel), mChannel(channel)
{
	fg = front;
	bg = back;
	resize(width, height);
	setPaletteBackgroundColor(back);
	KWin::setState(handle(), NET::SkipTaskbar);

	TDEConfig *c = napp->config();
	c->setGroup("FFRS");

	TQSize size = napp->desktop()->size();
	TQRect desktop(0,0, size.width(), size.height());

	TQPoint at;
	if (channel==0)
	{
		at = TQPoint(size.width()-width*4, size.height()-height-32);
		TQToolTip::add(this, i18n("Left"));
	}
	else // if (channel==1)
	{
		at = TQPoint(size.width()-width*2, size.height()-height-32);
		TQToolTip::add(this, i18n("Right"));
	}

	move(c->readPointEntry("at"+TQString::number(mChannel), &at));

	// make sure we're on the desktop
	if (
			!desktop.contains(rect().topLeft())
			 || !desktop.contains(rect().bottomRight())
		)
	{
		move(at);
	}


	TQBitmap mask(width, height);
	TQPainter p(&mask);

//	TQt::color0 = transparent
//	TQt::color1 = opaque
	p.fillRect(0, 0, width, height, TQt::color0);
	for (int i=0; i < height; )
	{
		p.fillRect(0, height-i-block, width, block, TQt::color1);
		i += block + unblock;
	}
	setMask(mask);
	units = block+unblock;
	show();

	moving=false;
}

View::~View()
{
	TDEConfig *c = napp->config();
	c->setGroup("FFRS");
	c->writeEntry("at"+TQString::number(mChannel), pos());
}

void View::mouseMoveEvent(TQMouseEvent *)
{
	if (moving)
	{
		move ( TQCursor::pos()-mMousePoint );
	}
}

void View::mousePressEvent(TQMouseEvent *)
{
	moving = true;
	mMousePoint = mapFromGlobal(TQCursor::pos());
}

void View::mouseReleaseEvent(TQMouseEvent *)
{
	moving = false;
}

void View::draw(float level)
{
	int w = width();
	int h = height();

	// now convert level to pixels

	static const float LEVEL_MIN = 1.0/(1<<20);
	if (level < LEVEL_MIN) level = LEVEL_MIN;
	level = (2.0/log(2.0))*log(level+1.0);

	float fpix = level * (float)h;
	fpix  = fabs(fpix);
	if (fpix - (int)fpix > .5) fpix += .5;

	int pix = (int)(fpix / units) * units;

	// and draw it (it updates too quickly for it to
	// need a paintEvent)
	TQPainter p(this);
	p.fillRect(0, 0, w, h-pix, bg);
	p.fillRect(0, h-pix, w, h - (h-pix), fg);
}



FFRS::FFRS() : TQObject(), Plugin()
{
	dpyleft = dpyright = 0;
	changed();
	prefs = new FFRSPrefs(this);
	connect(prefs, TQ_SIGNAL(changed()), TQ_SLOT(changed()));

	setSamples(256);

	start();
}

FFRS::~FFRS()
{
	delete dpyleft;
	delete dpyright;
}

void FFRS::scopeEvent(float *left, float *right, int len)
{
	float avl=0;
	float avr=0;

	for (int i=0; i < len; i++)
	{
		avl += fabs(left[i]);
		avr += fabs(right[i]);
	}
	avl /= len;
	avr /= len;

	dpyleft->draw(avl);
	if (dpyright)
		dpyright->draw(avr);
}

void FFRS::changed()
{
	delete dpyleft;
	delete dpyright;

	dpyleft = new View(prefs->width(), prefs->height(), prefs->fgblock(), prefs->bgblock(), prefs->fgcolor(), prefs->bgcolor(), 0);
	dpyright = new View(prefs->width(), prefs->height(), prefs->fgblock(), prefs->bgblock(), prefs->fgcolor(), prefs->bgcolor(), 1);

	setInterval(prefs->rate());
}


#include <knuminput.h>
#include <kcolorbutton.h>
#include <tqlayout.h>
#include <tqhbox.h>
#include <tqlabel.h>

FFRSPrefs::FFRSPrefs( TQObject *parent )
	: CModule(i18n("Foreign Region"), i18n("French Foreign Region"),"",parent)
{
	TQVBoxLayout *layout = new TQVBoxLayout(this);

	TQHBox *box = new TQHBox(this);
	layout->addWidget(box);
	new TQLabel(i18n("Width:"), box);
	mWidth = new KIntNumInput(width(), box);
	mWidth->setMinValue(0);

	box = new TQHBox(this);
	layout->addWidget(box);
	new TQLabel(i18n("Height:"), box);
	mHeight = new KIntNumInput(height(), box);
	mHeight->setMinValue(0);

	box = new TQHBox(this);
	layout->addWidget(box);
	new TQLabel(i18n("Visible block size:"), box);
	mFgblock = new KIntNumInput(fgblock(), box);
	mFgblock->setMinValue(0);

	box = new TQHBox(this);
	layout->addWidget(box);
	new TQLabel(i18n("Transparent block size:"), box);
	mBgblock = new KIntNumInput(bgblock(), box);
	mBgblock->setMinValue(0);

	box = new TQHBox(this);
	layout->addWidget(box);
	new TQLabel(i18n("Update interval:"), box);
	mRate = new KIntNumInput(rate(), box);
	mRate->setMinValue(0);

	box = new TQHBox(this);
	layout->addWidget(box);
	new TQLabel(i18n("Foreground color:"), box);
	mFgcolor = new KColorButton(fgcolor(), box);

	box = new TQHBox(this);
	layout->addWidget(box);
	new TQLabel(i18n("Background color:"), box);
	mBgcolor = new KColorButton(bgcolor(), box);

	layout->addStretch();
}

void FFRSPrefs::save()
{
	TDEConfig *c = napp->config();

	c->setGroup("FFRS");
	c->writeEntry("width", mWidth->value());
	c->writeEntry("height", mHeight->value());
	c->writeEntry("fgblock", mFgblock->value());
	c->writeEntry("bgblock", mBgblock->value());
	c->writeEntry("rate", mRate->value());

	c->writeEntry("bgcolor", mBgcolor->color());
	c->writeEntry("fgcolor", mFgcolor->color());

	emit changed();
}

int FFRSPrefs::width()
{
	TDEConfig *c = napp->config();
	c->setGroup("FFRS");
	return c->readNumEntry("width", 22);
}

int FFRSPrefs::height()
{
	TDEConfig *c = napp->config();
	c->setGroup("FFRS");
	return c->readNumEntry("height", 162);
}

int FFRSPrefs::fgblock()
{
	TDEConfig *c = napp->config();
	c->setGroup("FFRS");
	return c->readNumEntry("fgblock", 27-12);
}

int FFRSPrefs::bgblock()
{
	TDEConfig *c = napp->config();
	c->setGroup("FFRS");
	return c->readNumEntry("bgblock", 12);
}

int FFRSPrefs::rate()
{
	TDEConfig *c = napp->config();
	c->setGroup("FFRS");
	return c->readNumEntry("rate", 110);
}


TQColor FFRSPrefs::bgcolor()
{
	TDEConfig *c = napp->config();
	c->setGroup("FFRS");
	TQColor dumbass(0, 64, 0);
	return c->readColorEntry("bgcolor", &dumbass);
}

TQColor FFRSPrefs::fgcolor()
{
	TDEConfig *c = napp->config();
	c->setGroup("FFRS");
	TQColor dumbass(0, 255, 0);
	return c->readColorEntry("fgcolor", &dumbass);
}

#include "ffrs.moc"

