/*
    This file is part of the Boson game
    Copyright (C) 1999-2000,2001-2003 The Boson Team (boson-devel@lists.sourceforge.net)

    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; either version 2 of the License, or
    (at your option) any later version.

    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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifndef BOSONMINIMAP_H
#define BOSONMINIMAP_H

#include <qwidget.h>

class Player;
class Unit;
class BosonMap;
class BosonCanvas;
class Cell;

class QPixmap;
class QPainter;
class QPaintEvent;
class QMouseEvent;
template<class T> class QPtrVector;

/**
 * @author Thomas Capricelli <capricel@email.enst.fr>, Andreas Beckermann <b_mann@gmx.de>
 **/
class BosonMiniMap : public QWidget
{
	Q_OBJECT
public:
	BosonMiniMap(QWidget* parent, const char* name = 0);
	~BosonMiniMap();

	void setMap(BosonMap*);
	void initMap();

	void setLocalPlayer(Player*);
	
	/**
	 * Display only those parts of the map that are visible to the player
	 * @param p Display this players sight. If NULL show the entire map
	 **/
	void initFogOfWar(Player* p);

	double scale() const;
	double zoom() const;

signals:
	void signalReCenterView(const QPoint& pos);
	void signalMoveSelection(int cellX, int cellY);

public slots:
	/**
	 * @param x The x - coordinate of the cell
	 * @param y The x - coordinate of the cell
	 **/
	void slotUpdateCell(int x, int y);

	void slotAddUnit(Unit* unit, int x, int y);

	/**
	 * Change the appearance of the rect that displays the current viewport.
	 * All params are cell coordinates.
	 **/
	void slotMoveRect(const QPoint& topLeft, const QPoint& topRight, const QPoint& bottomLeft, const QPoint& bottomRight);

	void slotMoveUnit(Unit* unit, float oldX, float oldY);
	void slotUnitDestroyed(Unit* unit);

	void slotUnfog(int x, int y);
	void slotFog(int x, int y);

	/**
	 * Show or hide the minimap, depending on @p show. This slot is called when
	 * the radar station is constructed or destroyed.
	 **/
	void slotShowMap(bool show);

	/**
	 * Set the theme where to find the pixmaps (logo, zoom pixmaps, ...) in.
	 * Default is "standard".
	 **/
	void setPixmapTheme(const QString& theme);

protected slots:
	void slotZoomIn();
	void slotZoomOut();
	void slotZoomDefault();

protected:
	void setPoint(int x, int y, const QColor& color);
	virtual void mousePressEvent(QMouseEvent*);
	virtual bool eventFilter(QObject* o, QEvent* e);
	void createMap();
	void createGround();

	/**
	 * Move a unit. if oldCells is NULL they are ignored.
	 * (added a unit)
	 **/
	void moveUnit(Unit* unit, const QPtrVector<Cell>* newCells, const QPtrVector<Cell>* oldCells);

	/**
	 * Update the cell. Checks whether cell is fogged, checks for units on
	 * the cell, ...
	 **/
	void updateCell(int x, int y);

	/**
	 * Calculate the color of the cell at @p x, @p y accordint to the @ref
	 * BosonMap::texMap. This method doesn't check for units and will
	 * display the color of the ground at this point.
	 * @param x The x - coordinate of the cell
	 * @param y The x - coordinate of the cell
	 **/
	void calculateGround(int x, int y);

	/**
	 * @return Number of horizontal cells on this map
	 **/
	int mapWidth() const;
	/**
	 * @return Number of vertical cells on this map
	 **/
	int mapHeight() const;

	QPixmap* ground() const;

	/**
	 * @return The current map.
	 **/
	BosonMap* map() const;

	void makeCellList(QPtrVector<Cell>* cells, const Unit* unit, float x, float y);

	/**
	 * @return A pixmap for @p file in the theme @p theme if it is existing.
	 * If not it will search for @p file in the "standard" theme and if it's
	 * not there either it'll return a null pixmap.
	 **/
	QPixmap pixmapFromTheme(const QString& file, const QString& theme) const;

	/**
	 * Repaint the internal minimap pixmap, if updates are enabled.
	 * Otherwise this is a noop.
	 **/
	void repaintMiniMapPixmap();


private:
	class BosonMiniMapPrivate;
	BosonMiniMapPrivate* d;

	QPixmap* mGround;
	QPixmap* mUnZoomedGround;
	bool mUseFog; // useful for the editor to disable the fog of war
	
	BosonMap* mMap;
	Player* mLocalPlayer; // needed to distinguish between movements (->fog of war)
	
};
#endif
