#include <QSqlQuery>
#include <QSqlRecord>
#include <QSqlField>
#include <QSqlError>
#include <QPainter>
#include <QImageReader>
#include <QBuffer>
#include <QtConcurrentMap>
#include "common/util.h"
#include "osm.h"
#include "tile.h"
#include "tilecache.h"
#include "sqlitemap.h"

using namespace OSM;

SqliteMap::SqliteMap(const QString &fileName, QObject *parent)
  : Map(fileName, parent), _mapRatio(1.0), _valid(false)
{
	if (!Util::isSQLiteDB(fileName, _errorString))
		return;

	_db = QSqlDatabase::addDatabase("QSQLITE", fileName);
	_db.setDatabaseName(fileName);
	_db.setConnectOptions("QSQLITE_OPEN_READONLY");

	if (!_db.open()) {
		_errorString = _db.lastError().text();
		return;
	}

	QSqlRecord r = _db.record("tiles");
	if (r.isEmpty()
	  || r.field(0).name() != "x"
	  || METATYPE(r.field(0)) != QMetaType::Int
	  || r.field(1).name() != "y"
	  || METATYPE(r.field(1)) != QMetaType::Int
	  || r.field(2).name() != "z"
	  || METATYPE(r.field(2)) != QMetaType::Int
	  || r.field(4).name() != "image"
	  || METATYPE(r.field(4)) != QMetaType::QByteArray) {
		_errorString = "Invalid table format";
		return;
	}

	{
		QSqlQuery query("SELECT min(z), max(z) FROM tiles", _db);
		if (!query.first()) {
			_errorString = "Empty tile set";
			return;
		}
		_zooms = Range(17 - query.value(1).toInt(), 17 - query.value(0).toInt());
		if (_zooms.min() < 0 || !_zooms.isValid()) {
			_errorString = "Invalid zoom levels";
			return;
		}
	}
	_zoom = _zooms.max();

	{
		int z = _zooms.min();
		QString sql = QString("SELECT min(x), min(y), max(x), max(y) FROM tiles"
		  " WHERE z = %1").arg(17 - z);
		QSqlQuery query(sql, _db);
		query.first();

		int minX = qMin((1<<z) - 1, qMax(0, query.value(0).toInt()));
		int minY = qMin((1<<z) - 1, qMax(0, query.value(1).toInt()));
		int maxX = qMin((1<<z) - 1, qMax(0, query.value(2).toInt())) + 1;
		int maxY = qMin((1<<z) - 1, qMax(0, query.value(3).toInt())) + 1;
		Coordinates tl(tile2ll(QPoint(minX, minY), z));
		tl.rlat() = -tl.lat();
		Coordinates br(tile2ll(QPoint(maxX, maxY), z));
		br.rlat() = -br.lat();
		// Workaround of broken zoom levels 0 and 1 due to numerical instability
		tl.rlat() = qMin(tl.lat(), BOUNDS.top());
		br.rlat() = qMax(br.lat(), BOUNDS.bottom());
		_bounds = RectC(tl, br);
	}

	{
		QString sql = QString("SELECT image FROM tiles LIMIT 1");
		QSqlQuery query(sql, _db);
		query.first();

		QByteArray data = query.value(0).toByteArray();
		QBuffer buffer(&data);
		QImageReader reader(&buffer);
		QSize tileSize(reader.size());

		if (!tileSize.isValid() || tileSize.width() != tileSize.height()) {
			_errorString = "Unsupported/invalid tile images";
			return;
		}
		_tileSize = tileSize.width();
	}

	_db.close();

	_valid = true;
}

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

	_mapRatio = hidpi ? deviceRatio : 1.0;
	_factor = zoom2scale(_zoom, _tileSize) * _mapRatio;

	_db.open();
}

void SqliteMap::unload()
{
	_db.close();
}

QRectF SqliteMap::bounds()
{
	return QRectF(ll2xy(_bounds.topLeft()), ll2xy(_bounds.bottomRight()));
}

qreal SqliteMap::resolution(const QRectF &rect)
{
	return OSM::resolution(rect.center(), _zoom, tileSize());
}

int SqliteMap::limitZoom(int zoom) const
{
	if (zoom < _zooms.min())
		return _zooms.min();
	if (zoom > _zooms.max())
		return _zooms.max();

	return zoom;
}

int SqliteMap::zoomFit(const QSize &size, const RectC &rect)
{
	if (!rect.isValid())
		_zoom = _zooms.max();
	else {
		QRectF tbr(ll2m(rect.topLeft()), ll2m(rect.bottomRight()));
		QPointF sc(tbr.width() / size.width(), tbr.height() / size.height());
		_zoom = limitZoom(scale2zoom(qMax(sc.x(), -sc.y()) / _mapRatio,
		  _tileSize));
	}

	_factor = zoom2scale(_zoom, _tileSize) * _mapRatio;

	return _zoom;
}

void SqliteMap::setZoom(int zoom)
{
	_zoom = zoom;
	_factor = zoom2scale(_zoom, _tileSize) * _mapRatio;
}

int SqliteMap::zoomIn()
{
	_zoom = qMin(_zoom + 1, _zooms.max());
	_factor = zoom2scale(_zoom, _tileSize) * _mapRatio;

	return _zoom;
}

int SqliteMap::zoomOut()
{
	_zoom = qMax(_zoom - 1, _zooms.min());
	_factor = zoom2scale(_zoom, _tileSize) * _mapRatio;

	return _zoom;
}

qreal SqliteMap::tileSize() const
{
	return (_tileSize / _mapRatio);
}

QByteArray SqliteMap::tileData(int zoom, const QPoint &tile) const
{
	QSqlQuery query(_db);
	query.prepare("SELECT image FROM tiles WHERE z=:zoom AND x=:x AND y=:y");
	query.bindValue(":zoom", 17 - zoom);
	query.bindValue(":x", tile.x());
	query.bindValue(":y", tile.y());
	query.exec();

	if (query.first())
		return query.value(0).toByteArray();

	return QByteArray();
}

void SqliteMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
{
	Q_UNUSED(flags);
	QPoint tile = mercator2tile(QPointF(rect.topLeft().x(),
	  -rect.topLeft().y()) * _factor, _zoom);
	QPointF tl(tile2mercator(tile, _zoom) / _factor);
	QSizeF s(rect.right() - tl.x(), rect.bottom() - tl.y());
	int width = ceil(s.width() / tileSize());
	int height = ceil(s.height() / tileSize());

	QList<DataTile> tiles;

	for (int i = 0; i < width; i++) {
		for (int j = 0; j < height; j++) {
			QPoint t(tile.x() + i, tile.y() + j);
			QPixmap *pm = TileCache::object(TileCache::Key(this, _zoom, t));
			if (pm) {
				QPointF tp(tl.x() + (t.x() - tile.x()) * tileSize(),
				  tl.y() + (t.y() - tile.y()) * tileSize());
				drawTile(painter, pm, tp);
			} else
				tiles.append(DataTile(t, _zoom, tileData(_zoom, t)));
		}
	}

	QFuture<void> future = QtConcurrent::map(tiles, &DataTile::load);
	future.waitForFinished();

	for (int i = 0; i < tiles.size(); i++) {
		const DataTile &mt = tiles.at(i);
		if (mt.pixmap().isNull())
			continue;

		TileCache::insert(TileCache::Key(this, mt.zoom(), mt.xy()),
		  new QPixmap(mt.pixmap()));

		QPointF tp(tl.x() + (mt.xy().x() - tile.x()) * tileSize(),
		  tl.y() + (mt.xy().y() - tile.y()) * tileSize());
		drawTile(painter, &mt.pixmap(), tp);
	}
}

void SqliteMap::drawTile(QPainter *painter, const QPixmap *pixmap,
  const QPointF &tp) const
{
	if (_mapRatio != pixmap->devicePixelRatio()) {
		QPixmap pm(*pixmap);
		pm.setDevicePixelRatio(_mapRatio);
		painter->drawPixmap(tp, pm);
	} else
		painter->drawPixmap(tp, *pixmap);
}

QPointF SqliteMap::ll2xy(const Coordinates &c)
{
	QPointF m = ll2m(c);
	return QPointF(m.x(), -m.y()) / _factor;
}

Coordinates SqliteMap::xy2ll(const QPointF &p)
{
	return m2ll(QPointF(p.x(), -p.y()) * _factor);
}

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

	if (isDir)
		*isDir = false;

	return new SqliteMap(path);
}
