/*
 *  TnLandmarkDB.h
 *  GLMap
 *
 *  Created by jianhuang on 8/31/10.
 *  Copyright 2010 TeleNav. All rights reserved.
 *
 */

#ifndef TN_LANDMARK_DB_H
#define TN_LANDMARK_DB_H

class TnLandmarkDB
{
	struct BinaryNode
	{
		BinaryNode() : offset(0), size(0) {};
		BinaryNode(int32_t& _offset, int32_t& _size):offset(_offset), size(_size){};
		int32_t offset;
		int32_t size;
	};

public:
	TnLandmarkDB();

	virtual ~TnLandmarkDB();

	void	LoadIndexBin(Tn::Foundation::shared_array<uint8_t>& spBuf, int32_t size);

	void	QueryTileInfo(int32_t xIndex, int32_t yIndex, int32_t zoom, std::vector<int64_t>& ids);

	int32_t QueryLandmarkDataInfo(int64_t id, int32_t& offset, int32_t& size);

	int32_t AnalyzeLandmarkData(uint8_t* buf, int32_t size, int64_t& id, double& lat, double& lon, std::vector<float>& vecPoints, Tn::Foundation::shared_array<uint8_t>& spImage, int32_t& sizeImage);

private:

	int8_t		ReadInt8(uint8_t*& p);

	int16_t		ReadInt16(uint8_t*& p);

	int32_t		ReadInt32(uint8_t*& p);

	int64_t		ReadInt64(uint8_t*& p);

    float       ReadFloat(uint8_t*& p);

	int64_t		ComposeTopNodeId(int32_t xIndex, int32_t yIndex, int32_t zoom);

	int64_t		ComposeNodeId(int32_t xIndex, int32_t yIndex, int32_t zoom, int32_t landmarkIndex = 0);

	int32_t		DecomposeNodeId(int64_t idNode, int32_t& xIndex, int32_t& yIndex, int32_t& zoom);

	uint8_t*	GetIndexNodeBuf(int32_t xIndex, int32_t yIndex, int32_t zoom, int32_t& sizeNode);

	void		GetBaseNodeIds(uint8_t* buf, int32_t sizeNode, int32_t xIndex, int32_t yIndex, int32_t zoom, std::vector<int64_t>& ids);

private:
    
	int32_t		m_nVersion;
	int32_t		m_nLevelTop;
	int32_t		m_nTopNodes;

	std::map<int64_t, BinaryNode> m_mapTopTiles;

	Tn::Foundation::shared_array<uint8_t> m_spIndexData;
};

#endif
