// Copyright (c) 2002-2003 Rob Kaper <cap@capsi.com>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License version 2.1 as published by the Free Software Foundation.
//
// This library 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this library; see the file COPYING.LIB.  If not, write to
// the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
// Boston, MA 02110-1301, USA.

#include <tqpainter.h>
#include <tqcursor.h>

#include <kdebug.h>
#include <kdialogbase.h>
#include <kiconeffect.h>
#include <tdeglobalsettings.h>
#include <tdelocale.h>
#include <tdepopupmenu.h>
#include <kstandarddirs.h>

#include <atlantic_core.h>
#include <config.h>
#include <player.h>
#include <estate.h>
#include <estategroup.h>

#include "portfolioview.moc"

#define PE_DISTW	4
#define	PE_DISTH	4
#define PE_SPACE	2
#define PE_MARGINW	5
#define PE_MARGINH	2
#define ICONSIZE	48

PortfolioView::PortfolioView(AtlanticCore *core, Player *player, TQColor activeColor, TQColor inactiveColor, TQWidget *parent, const char *name) : TQWidget(parent, name)
{
	m_atlanticCore = core;
	m_player = player;
	m_activeColor = activeColor;
	m_inactiveColor = inactiveColor;
	m_lastPE = 0;

	qpixmap = 0;
	b_recreate = true;

	m_portfolioEstates.setAutoDelete(true);
	setBackgroundColor(TQt::white);
	setMinimumHeight(ICONSIZE);

	// Init icon
	m_image = 0;
	m_imageName = "hamburger.png";
	loadIcon();
}

PortfolioView::~PortfolioView()
{
	clearPortfolio();
	delete m_image;
	delete qpixmap;
}

Player *PortfolioView::player()
{
	return m_player;
}

void PortfolioView::buildPortfolio()
{
	if ( m_portfolioEstates.count() )
		clearPortfolio();

	// Loop through estate groups in order
	TQPtrList<EstateGroup> estateGroups = m_atlanticCore->estateGroups();
	PortfolioEstate *lastPE = 0, *firstPEprevGroup = 0;

	int x = 100, y = 25, marginHint = 5, bottom;
	bottom = ICONSIZE - PE_HEIGHT - marginHint;

	EstateGroup *estateGroup;
	for (TQPtrListIterator<EstateGroup> it(estateGroups); *it; ++it)
	{
		if ((estateGroup = *it))
		{
			// New group
			lastPE = 0;

			// Loop through estates
			TQPtrList<Estate> estates = m_atlanticCore->estates();
			Estate *estate;
			for (TQPtrListIterator<Estate> it(estates); *it; ++it)
			{
				if ((estate = *it) && estate->estateGroup() == estateGroup)
				{
					// Create PE
					PortfolioEstate *portfolioEstate = new PortfolioEstate(estate, m_player, false, this, "portfolioestate");
					m_portfolioEstates.append(portfolioEstate);

 					connect(portfolioEstate, TQT_SIGNAL(estateClicked(Estate *)), this, TQT_SIGNAL(estateClicked(Estate *)));
					if (lastPE)
					{
						x = lastPE->x() + 2;
						y = lastPE->y() + 4;
						if (y > bottom)
							bottom = y;
					}
					else if (firstPEprevGroup)
					{
						x = firstPEprevGroup->x() + PE_WIDTH + 8;
						y = 20 + marginHint;
						firstPEprevGroup = portfolioEstate;
					}
					else
					{
						x = ICONSIZE + marginHint;
						y = 20 + marginHint;
						if (y > bottom)
							bottom = y;
						firstPEprevGroup = portfolioEstate;
					}

					portfolioEstate->setGeometry(x, y, portfolioEstate->width(), portfolioEstate->height());
					portfolioEstate->show();

					connect(estate, TQT_SIGNAL(changed()), portfolioEstate, TQT_SLOT(estateChanged()));

					lastPE = portfolioEstate;
				}
			}
		}
	}
	setMinimumWidth(x + PE_WIDTH + marginHint);
	int minHeight = bottom + PE_HEIGHT + marginHint;
	if (minHeight > minimumHeight())
		setMinimumHeight(minHeight);
}

void PortfolioView::clearPortfolio()
{
	m_portfolioEstates.clear();
}

void PortfolioView::loadIcon()
{
	if (m_imageName == m_player->image())
		return;
	m_imageName = m_player->image();

	delete m_image;
	m_image = 0;

	if (!m_imageName.isEmpty())
	{
		TQString filename = locate("data", "atlantik/themes/default/tokens/" + m_imageName);
		if (TDEStandardDirs::exists(filename))
			m_image = new TQPixmap(filename);
	}

	if (!m_image)
	{
		return;

/*
		m_imageName = "hamburger.png";

		TQString filename = locate("data", "atlantik/themes/default/tokens/" + m_imageName);
		if (TDEStandardDirs::exists(filename))
			m_image = new TQPixmap(filename);
*/
	}
	else if (ICONSIZE > minimumHeight())
		setMinimumHeight(ICONSIZE);

	TQWMatrix m;
	m.scale(double(ICONSIZE) / m_image->width(), double(ICONSIZE) / m_image->height());
	TQPixmap *scaledPixmap = new TQPixmap(ICONSIZE, ICONSIZE);
	*scaledPixmap = m_image->xForm(m);

	delete m_image;
	m_image = scaledPixmap;
}

void PortfolioView::paintEvent(TQPaintEvent *)
{
	if (b_recreate)
	{
		delete qpixmap;
		qpixmap = new TQPixmap(width(), height());

		TQPainter painter;
		painter.begin(TQT_TQPAINTDEVICE(qpixmap), this);

		painter.setPen(TQt::white);
		painter.setBrush(TQt::white);
		painter.drawRect(rect());

		painter.setPen(m_player->hasTurn() ? m_activeColor : TQt::black);
		painter.setBrush(m_player->hasTurn() ? m_activeColor : TQt::black);
		painter.drawRect(0, 0, width(), 20);

		if (m_image)
		{
			painter.setPen(TQt::black);
			painter.setBrush(TQt::white);
			painter.drawRect(0, 0, ICONSIZE, ICONSIZE);

			painter.drawPixmap(0, 0, *m_image);
		}

		painter.setPen(TQt::white);
		painter.setFont(TQFont(TDEGlobalSettings::generalFont().family(), TDEGlobalSettings::generalFont().pointSize(), TQFont::Bold));
		painter.drawText(ICONSIZE + KDialog::marginHint(), 15, m_player->name());

		if ( m_portfolioEstates.count() )
			painter.drawText(width() - 50, 15, TQString::number(m_player->money()));
		else
		{
			painter.setPen(TQt::black);
			painter.setBrush(TQt::white);

			painter.setFont(TQFont(TDEGlobalSettings::generalFont().family(), TDEGlobalSettings::generalFont().pointSize(), TQFont::Normal));
			painter.drawText(ICONSIZE + KDialog::marginHint(), 30, m_player->host());
		}

		b_recreate = false;
	}
	bitBlt(this, 0, 0, qpixmap);
}

void PortfolioView::resizeEvent(TQResizeEvent *)
{
	b_recreate = true;
}

void PortfolioView::playerChanged()
{
	loadIcon();

	b_recreate = true;
	update();
}

void PortfolioView::mousePressEvent(TQMouseEvent *e)
{
	Player *playerSelf = m_atlanticCore->playerSelf();

	if ( e->button()==Qt::RightButton && (m_player != playerSelf) )
	{
		TDEPopupMenu *rmbMenu = new TDEPopupMenu(this);
		rmbMenu->insertTitle(m_player->name());

		if ( m_portfolioEstates.count() )
		{
			// Start trade
			rmbMenu->insertItem(i18n("Request Trade with %1").arg(m_player->name()), 0);
		}
		else
		{
			// Kick player
			rmbMenu->insertItem(i18n("Boot Player %1 to Lounge").arg(m_player->name()), 0);
			rmbMenu->setItemEnabled( 0, m_atlanticCore->selfIsMaster() );
		}

		connect(rmbMenu, TQT_SIGNAL(activated(int)), this, TQT_SLOT(slotMenuAction(int)));
		TQPoint g = TQCursor::pos();
		rmbMenu->exec(g);
	}
}

void PortfolioView::slotMenuAction(int item)
{
	switch (item)
	{
	case 0:
		if ( m_portfolioEstates.count() )
			emit newTrade(m_player);
		else
			emit kickPlayer(m_player);
		break;
	}
}
#undef PE_DISTW
#undef PE_DISTH
#undef PE_SPACE
#undef PE_MARGINW
#undef PE_MARGINH
#undef ICONSIZE