#ifndef MAP_H
#define MAP_H

#include "base_types.h"

// Maximum map is 65530 x 65530 because qHash may be repeat and towers has same Id

/// TODO: fix cell type - must be a union of int and bit flags

class Map : public QObject
{
Q_OBJECT
public:
	enum CellType
	{
		Free = 0,			// FIXME: not worked yet!!
							// FIXME: must add Free cell to all functions
		Player = 1,				// use Player + playerId for multiplayer
		AlienPath = 1000,		// use AlienPath + Player and + playerId
		PlayerTower = -1,		// use PlayerTower - Player - playerId
		Wall = -1000,
		Start = 9999,
		Finish = 8888
	};
	// 0 - free for any player
	// 1 - free for player 1
	// 2 - free for player 2
	// 3 - -/-
	// 1001 - player 1, free, but it is alien path (for optimization)
	// 1001 - player 2, free, but it is alien path (for optimization)
	// 1003 - -/-
	// 9999 - start field
	// 8888 - finish field
	// -1 - tower on free cell
	// -2 - player 1 tower
	// -3 - player 2 tower
	// -4 - -/-
	// -1000 - it is wall or some place where you can't build any towers


	explicit Map(QByteArray data, QSize size, QString name, int maxPlayers, int image, QObject *parent = 0);
	bool addTowerOnMap(int playerId, QPoint pos);
	//bool isFreePlace(QPoint pos);
	void delTowerOnMap(int playerId, QPoint pos);
	void removeAliensPath();
	QVector <QPointF> createPath(QPoint start, QPoint finish);
	QRect rect() const {return QRect(QPoint(),mapSize);}
    const QString &name() const {return mapName;}
    const QList <QPoint> &starts() const {return Starts;}
    const QList <QPoint> &finishs() const {return Finishs;}
    const QVector < QVector <int> > &cells() const {return Cells;}
	int image() const {return imageType;}
	int maxPlayers() const {return players;}
signals:
	void recreateAlienPath(bool * pathOK);
public slots:
	void printMap();

private:
	QSize mapSize;
	QString mapName;
	int players;
	int imageType;
	QVector < QVector <int> > Cells;
    QVector < QVector <int> > TmpCells;
    QList <QPoint> Starts;
	QList <QPoint> Finishs;

	QVector<QPoint> invWaveTrace(QPoint finish, int cnt);
	int waveTrace(QPoint start, QPoint finish);
	bool isReachable(int playerId, QPoint pos);
};

#endif // MAP_H
