#pragma once

#include <Trie.h>
#include <Drawing.h>

#include "ChunkMap.h"
#include "MapOptions.h"

struct MapPlayer
{
    Framework::Vec3<float> position;
    Framework::Text name;
};

class DimensionMap : public Framework::DrawableBackground
{
private:
    MapOptions* zOptions;
    Framework::RCTrie<ChunkMap>* chunks;
    Framework::Array<ChunkMap*> chunkList;
    Framework::Point originChunkCenter;
    Framework::Point scrollOffset;
    int chunkCount;
    int pixelsPerBlock;
    int requestCount;
    bool drag;
    Framework::Point lastMouse;
    Framework::Point lastSize;
    Framework::Array<MapPlayer> players;
    double nextPlayersRequest;
    Framework::Image* playerIcon;

    void getAddrOf(Framework::Point cPos, char* addr) const;
    void getAddrOfWorld(Framework::Point wPos, char* addr) const;
    Framework::Point getMinVisibleChunkCenter(
        Framework::Point& screenPos) const;
    Framework::Point getMaxVisibleChunkCenter(
        Framework::Point& screenPos) const;
    void removeUnused() const;

public:
    DimensionMap(MapOptions* zOptions);
    ~DimensionMap();

    void updatePlayers(char* data);
    void requestNextChunk();
    void addChunk(ChunkMap* chunk);
    bool tick(double time) override;
    void render(Framework::Image& rObj) override;
    void doMouseEvent(Framework::MouseEvent& me, bool userRet) override;
};