#include "misc_ui.h"
#include "misc_ui.moc"

#include <qpainter.h>

#include "base/piece.h"
#include "base/board.h"
#include "factory.h"


const uint GIFT_SHOWER_TIMEOUT = 800;
const uint GIFT_POOL_TIMEOUT = 2000;

const uint SHADOW_HEIGHT = 10;

const uint GI_WIDTH = 15;
const uint GI_HEIGHT = 11;
const uint ARROW_HEIGHT = 3;
const uint ARROW_WIDTH = 7;

const uint LED_WIDTH = 15;
const uint LED_HEIGHT = 15;
const uint LED_SPACING = 5;

/*****************************************************************************/
ShowNextPiece::ShowNextPiece(BaseBoard *board, QWidget *parent)
    : QCanvasView(board->next(), parent, "show_next_piece")
{
	setFrameStyle(QFrame::Panel | QFrame::Sunken);
    connect(board, SIGNAL(blockSizeChanged()), SLOT(blockSizeChanged()));
}

QSize ShowNextPiece::sizeHint() const
{
	return QSize(2*frameWidth() + canvas()->width(),
				 2*frameWidth() + canvas()->height());
}

QSizePolicy ShowNextPiece::sizePolicy() const
{
	return QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
}


/*****************************************************************************/
Shadow::Shadow(BaseBoard *board, QWidget *parent)
    : QWidget(parent, "shadow"), _xOffset(board->frameWidth()),
     _board(board), _show(false)
{
    connect(board,  SIGNAL(updatePieceConfigSignal()), SLOT(update()));
	connect(board, SIGNAL(blockSizeChanged()), SLOT(blockSizeChanged()));
}

QSize Shadow::sizeHint() const
{
	return QSize(_xOffset + _board->matrix().width() * _board->blockSize(),
				 SHADOW_HEIGHT);
}

QSizePolicy Shadow::sizePolicy() const
{
	return QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
}

void Shadow::setDisplay(bool show)
{
    _show = show;
    update();
}

void Shadow::paintEvent(QPaintEvent *)
{
	if ( !_show ) return;

	const Piece *piece = _board->currentPiece();
	uint pf = piece->min().first + _board->currentPos().first;
	uint pl = pf + piece->size().first - 1;

    QPainter p(this);
    p.setBrush(black);
    p.setPen(black);
	for (uint i=pf; i<=pl; i++) {
		QRect r(_xOffset + i * _board->blockSize() + 1 , 0,
				_board->blockSize() - 2, SHADOW_HEIGHT);
		p.drawRect(r);
	}
}


/*****************************************************************************/
GiftShower::GiftShower(QWidget *parent)
: QWidget(parent, "gift_shower"), _state(FALSE)
{}

QSize GiftShower::sizeHint() const
{
	return QSize(GI_WIDTH, GI_HEIGHT);
}

QSizePolicy GiftShower::sizePolicy() const
{
	return QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
}

void GiftShower::set()
{
	if (_state) return;
	_state = TRUE;
	repaint();
	QTimer::singleShot(cfactory->cbi.giftShowerTimeout,
                       this, SLOT(timeout()));
}

void GiftShower::timeout()
{
	_state = FALSE;
	repaint();
}

void GiftShower::paintEvent(QPaintEvent *)
{
	QPainter p(this);
	p.setBrush((_state ? yellow : black));
	p.setPen(black);
	p.drawRect(0, (height()-ARROW_HEIGHT)/2, ARROW_WIDTH, ARROW_HEIGHT);
	QPointArray a(3);
	a.setPoint(0, ARROW_WIDTH, 0);
	a.setPoint(1, width()-1, height()/2);
	a.setPoint(2, ARROW_WIDTH, height()-1);
	p.drawPolygon(a);
}


/*****************************************************************************/
class Led : public QWidget
{
 public:
	Led(const QColor &c, QWidget *parent)
		: QWidget(parent), col(c), _on(FALSE) {}

	QSizePolicy sizePolicy() const
		{ return QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); }
	QSize sizeHint() const { return QSize(LED_WIDTH, LED_HEIGHT); }

	void on()  { if (!_on) { _on = TRUE; repaint(); } }
	void off() { if (_on)  {_on = FALSE; repaint(); } }
	void setColor(const QColor &c) { if (c!=col) { col = c; repaint(); } }

 protected:
	void paintEvent(QPaintEvent *) {
		QPainter p(this);
		p.setBrush((_on ? col.light() : col.dark()));
		p.setPen(black);
		p.drawEllipse(0, 0, width(), height());
	}

 private:
	QColor col;
	bool   _on;
};

GiftPool::GiftPool(QWidget *parent)
    : QHBox(parent, "gift_pool"), nb(0), ready(false)
{
	setSpacing(LED_SPACING);
	leds.resize(cfactory->cbi.nbGiftLeds);
	for (uint i=0; i<leds.size(); i++)
        leds.insert(i, new Led(yellow, this));
}

QSize GiftPool::sizeHint() const
{
	QSize s = (leds.size() ? leds[0]->sizeHint() : QSize());
	return QSize((s.width()+LED_SPACING)*leds.size()-LED_SPACING, s.height());
}

QSizePolicy GiftPool::sizePolicy() const
{
	return QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
}

void GiftPool::put(uint n)
{
	if ( n==0 ) return;
	if ( nb==0 && !ready )
        QTimer::singleShot(cfactory->cbi.giftPoolTimeout,
                           this, SLOT(timeout()));
	uint e = QMIN(nb+n, leds.size());
	for (uint i=nb; i<e; i++) leds[i]->on();
	uint f = QMIN(nb+n-e, leds.size());
	for (uint i=0; i<f; i++) leds[i]->setColor(red);
	nb += n;
}

uint GiftPool::take()
{
	Q_ASSERT(ready);
	for (uint i=0; i<leds.size(); i++) {
		leds[i]->setColor(yellow);
		leds[i]->off();
	}
    uint max = cfactory->cbi.maxGiftsToSend;
	if ( nb>max ) {
		uint p = nb - max;
		nb = 0;
		put(p);
		return max;
	} else {
		ready = false;
		uint t = nb;
		nb = 0;
		return t;
	}
}

void GiftPool::reset()
{
    killTimers();
    ready = false;
    nb = 0;
    for (uint i=0; i<leds.size(); i++) {
		leds[i]->setColor(yellow);
		leds[i]->off();
	}
}
