#include <QFile>
#include <QPainter>
#include <QFileInfo>
#include <QDir>
#include <QtMath>
#include "common/wgs84.h"
#include "common/programpaths.h"
#include "IMG/imgdata.h"
#include "IMG/gmapdata.h"
#include "IMG/demtree.h"
#include "osm.h"
#include "pcs.h"
#include "rectd.h"
#include "imgjob.h"
#include "imgmap.h"

using namespace IMG;

#define EPSILON    1e-6
#define TILE_SIZE  384
#define DELTA      1e-3

static RectC limitBounds(const RectC &bounds, const Projection &proj)
{
	/* Limit the bounds for some well known projections
	   (world maps have N/S bounds up to 90/-90!) */

	if (proj == PCS::pcs(3857) || proj == PCS::pcs(3395))
		return bounds & OSM::BOUNDS;
	else if (proj == PCS::pcs(3031) || proj == PCS::pcs(3976))
		return bounds & RectC(Coordinates(-180, -60), Coordinates(180, -90));
	else if (proj == PCS::pcs(3995) || proj == PCS::pcs(3413))
		return bounds & RectC(Coordinates(-180, 90), Coordinates(180, 60));
	else
		return bounds;
}

QList<MapData*> IMGMap::overlays(const QString &fileName)
{
	QList<MapData*> list;

	for (int i = 1; i < 32; i++) {
		QString ol(fileName + "." + QString::number(i));
		if (QFileInfo(ol).isFile()) {
			MapData *data = new IMGData(ol, _polyCache, _pointCache, _demCache,
			  _lock, _demLock);
			if (data->isValid())
				list.append(data);
			else {
				qWarning("%s: %s", qUtf8Printable(data->fileName()),
				  qUtf8Printable(data->errorString()));
				delete data;
			}
		} else
			break;
	}

	return list;
}

IMGMap::IMGMap(const QString &fileName, bool GMAP, QObject *parent)
  : Map(fileName, parent), _projection(PCS::pcs(3857)), _tileRatio(1.0),
  _layer(All), _hillShading(false), _valid(false)
{
	if (GMAP)
		_data.append(new GMAPData(fileName, _polyCache, _pointCache, _demCache,
		  _lock, _demLock));
	else {
		_data.append(new IMGData(fileName, _polyCache, _pointCache, _demCache,
		  _lock, _demLock));
		_data.append(overlays(fileName));
	}

	if (!_data.first()->isValid()) {
		_errorString = _data.first()->errorString();
		return;
	}

	_zoom = _data.first()->zooms().min();

	_valid = true;
}

IMGMap::~IMGMap()
{
	qDeleteAll(_data);
	qDeleteAll(_styles);
}

IMG::Style *IMGMap::createStyle(IMG::MapData *data, const QString *typFile)
{
	if (typFile) {
		SubFile typ(*typFile);
		return new Style(_tileRatio, &typ);
	} else
		return new Style(_tileRatio, data->typ());
}

void IMGMap::load(const Projection &in, const Projection &out,
  qreal devicelRatio, bool hidpi, bool hillShading, int style, int layer)
{
	Q_UNUSED(in);
	Q_UNUSED(hidpi);

	_tileRatio = devicelRatio;
	_projection = out;
	_dataBounds = limitBounds(_data.first()->bounds(), _projection);

	switch (layer) {
		case 1:
			_layer = Vector;
			break;
		case 2:
			_layer = Raster;
			break;
		default:
			_layer = All;
	}

	const QString *typ = 0;
	if (style < 0 || style >= styles().size())
		typ = (_data.first()->typ() || styles().isEmpty())
		  ? 0 : &(styles().first());
	else
		typ = &(styles().at(style));

	_styles.reserve(_data.size());
	for (int i = 0; i < _data.size(); i++)
		_styles.append(createStyle(_data.at(i), i ? 0 : typ));

	_hillShading = IMGMap::hillShading() & hillShading;

	updateTransform();

	TileCache::clear();
}

void IMGMap::unload()
{
	cancelJobs(true);

	for (int i = 0; i < _data.size(); i++)
		_data.at(i)->clear();

	qDeleteAll(_styles);
	_styles = QList<IMG::Style*>();
}

int IMGMap::zoomFit(const QSize &size, const RectC &rect)
{
	const Range &zooms = _data.first()->zooms();

	if (rect.isValid()) {
		RectD pr(rect, _projection, 10);

		_zoom = zooms.min();
		for (int i = zooms.min() + 1; i <= zooms.max(); i++) {
			Transform t(transform(i));
			QRectF r(t.proj2img(pr.topLeft()), t.proj2img(pr.bottomRight()));
			if (size.width() + EPSILON < r.width()
			  || size.height() + EPSILON < r.height())
				break;
			_zoom = i;
		}
	} else
		_zoom = zooms.max();

	updateTransform();

	return _zoom;
}

int IMGMap::zoomIn()
{
	cancelJobs(false);

	_zoom = qMin(_zoom + 1, _data.first()->zooms().max());
	updateTransform();
	return _zoom;
}

int IMGMap::zoomOut()
{
	cancelJobs(false);

	_zoom = qMax(_zoom - 1, _data.first()->zooms().min());
	updateTransform();
	return _zoom;
}

void IMGMap::setZoom(int zoom)
{
	_zoom = zoom;
	updateTransform();
}

Transform IMGMap::transform(int zoom) const
{
	double scale = _projection.isGeographic()
	  ? 360.0 / (1<<zoom) : (2.0 * M_PI * WGS84_RADIUS) / (1<<zoom);
	PointD topLeft(_projection.ll2xy(_dataBounds.topLeft()));
	return Transform(ReferencePoint(PointD(0, 0), topLeft),
	  PointD(scale, scale));
}

void IMGMap::updateTransform()
{
	_transform = transform(_zoom);

	RectD prect(_dataBounds, _projection);
	_bounds = QRectF(_transform.proj2img(prect.topLeft()),
	  _transform.proj2img(prect.bottomRight()));
	// Adjust the bounds of world maps to avoid problems with wrapping
	if (_dataBounds.left() == -180.0 || _dataBounds.right() == 180.0)
		_bounds.adjust(0.5, 0, -0.5, 0);
}

bool IMGMap::isRunning(const TileCache::Key &key) const
{
	for (int i = 0; i < _jobs.size(); i++) {
		const QList<IMG::RasterTile> &tiles = _jobs.at(i)->tiles();
		for (int j = 0; j < tiles.size(); j++) {
			const IMG::RasterTile &t = tiles.at(j);
			if (TileCache::Key(t.data().first(), t.zoom(), t.xy()) == key)
				return true;
		}
	}

	return false;
}

void IMGMap::runJob(IMGJob *job)
{
	_jobs.append(job);

	connect(job, &IMGJob::finished, this, &IMGMap::jobFinished);
	job->run();
}

void IMGMap::removeJob(IMGJob *job)
{
	_jobs.removeOne(job);
	job->deleteLater();
}

void IMGMap::jobFinished(IMGJob *job)
{
	const QList<IMG::RasterTile> &tiles = job->tiles();

	for (int i = 0; i < tiles.size(); i++) {
		const IMG::RasterTile &mt = tiles.at(i);
		if (!mt.pixmap().isNull())
			TileCache::insert(TileCache::Key(mt.data().first(), mt.zoom(),
			  mt.xy()), new QPixmap(mt.pixmap()));
	}

	removeJob(job);

	emit tilesLoaded();
}

void IMGMap::cancelJobs(bool wait)
{
	for (int i = 0; i < _jobs.size(); i++)
		_jobs.at(i)->cancel(wait);
}

void IMGMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
{
	QPoint tl(qFloor(rect.left() / TILE_SIZE)
	  * TILE_SIZE, qFloor(rect.top() / TILE_SIZE) * TILE_SIZE);
	QSizeF s(rect.right() - tl.x(), rect.bottom() - tl.y());
	int width = ceil(s.width() / TILE_SIZE);
	int height = ceil(s.height() / TILE_SIZE);
	QList<RasterTile> tiles;

	for (int n = 0; n < _data.size(); n++) {
		for (int i = 0; i < width; i++) {
			for (int j = 0; j < height; j++) {
				QPoint ttl(tl.x() + i * TILE_SIZE, tl.y() + j * TILE_SIZE);
				TileCache::Key key(_data.at(n), _zoom, ttl);
				QPixmap *pm = TileCache::object(key);

				if (pm)
					painter->drawPixmap(ttl, *pm);
				else if (!isRunning(key)) {
					tiles.append(RasterTile(&_projection, _transform,
					  _data.at(n), _styles.at(n), _zoom,
					  QRect(ttl, QSize(TILE_SIZE, TILE_SIZE)), _tileRatio,
					  _hillShading, _layer & Raster, _layer & Vector));
				}
			}
		}
	}

	if (!tiles.isEmpty()) {
		if (flags & Map::Block) {
			QFuture<void> future = QtConcurrent::map(tiles, &RasterTile::render);
			future.waitForFinished();

			for (int i = 0; i < tiles.size(); i++) {
				const RasterTile &mt = tiles.at(i);
				const QPixmap &pm = mt.pixmap();
				painter->drawPixmap(mt.xy(), pm);
				TileCache::insert(TileCache::Key(mt.data().first(), mt.zoom(),
				  mt.xy()), new QPixmap(pm));
			}
		} else
			runJob(new IMGJob(tiles));
	}
}

double IMGMap::elevation(const Coordinates &c)
{
	MapData *d = _data.first();

	if (d->hasDEM()) {
		QList<MapData::Elevation> tiles;

		d->elevations(0, RectC(c, Coordinates(c.lon() + DELTA, c.lat() - DELTA)),
		  d->zooms().max(), &tiles);
		DEMTree tree(tiles);

		return tree.elevation(c);
	} else
		return Map::elevation(c);
}

QStringList IMGMap::styles(int &defaultStyle) const
{
	QStringList list;
	MapData *d = _data.first();
	list.reserve(styles().size() + (d->typ() ? 1 : 0));

	for (int i = 0; i < styles().size(); i++)
		list.append(QFileInfo(styles().at(i)).baseName());

	if (d->typ())
		list.append(d->name());

	defaultStyle = d->typ() ? list.size() - 1 : 0;

	return list;
}

QStringList IMGMap::layers(const QString &lang, int &defaultLayer) const
{
	Q_UNUSED(lang);

	defaultLayer = 0;

	return QStringList() << tr("All") << tr("Vector only") << tr("Raster only");
}

Map* IMGMap::createIMG(const QString &path, const Projection &proj, bool *isDir)
{
	Q_UNUSED(proj);

	if (isDir)
		*isDir = false;

	return new IMGMap(path, false);
}

Map* IMGMap::createGMAP(const QString &path, const Projection &proj, bool *isDir)
{
	Q_UNUSED(proj);

	if (isDir)
		*isDir = true;

	return new IMGMap(path, true);
}

IMGMap::StyleList::StyleList()
{
	QString path(ProgramPaths::styleDir());
	if (path.isEmpty())
		return;

	QDir dir(path);
	QFileInfoList styles(dir.entryInfoList(QStringList("*.typ"), QDir::Files));

	for (int i = 0; i < styles.size(); i++)
		append(styles.at(i).absoluteFilePath());
}

IMGMap::StyleList &IMGMap::styles()
{
	static StyleList list;
	return list;
}

bool IMGMap::hillShading() const
{
	for (int i = 0; i < _data.size(); i++)
		if (_data.at(i)->hasDEM())
			return true;

	return false;
}
