#pragma once

#include <Punkt.h>
#include <Reader.h>
#include <Writer.h>
#include <ReferenceCounter.h>
#include <Critical.h>

#pragma pack(push, 1)

// stores the color aof the map at a specific height
struct MapBlock
{
    unsigned char height;
    int color;
};

// stores the colors at all heights of a specific x and y position on the map.
// there can by only 256 colors stored per position
struct MapPixel
{
    unsigned char len;
    MapBlock* blocks;
};

#pragma pack(pop)

class Chunk;

// stores the map colors of a complete chunk
class ChunkMap : public Framework::ReferenceCounter
{
private:
    Framework::Punkt chunkCenter;
    MapPixel* pixels;
    Framework::Critical cs;

public:
    ChunkMap(Framework::Punkt chunkCenter);
    ChunkMap(Chunk* zChunk);
    ChunkMap(Framework::StreamReader* zReader);
    ~ChunkMap();

    // updates the color in the chunk map at a specific position.
    // the color stored will be the color that is less transparent from color1
    // and color2. if both colors are completle transparent then the color at
    // this height will be removed.
    // \param x the x coordinate in chunk space \param y the y
    // coordinate in chunk space \param heigth the height to update (at each
    // height are two blocks stored) \param color1 the color of the first block
    // at this height \param color2 the color of the second block at this height
    bool update(char x, char y, unsigned char height, int color1, int color2);

    void writeTo(Framework::StreamWriter* zWriter) const;
    Framework::Punkt getChunkCenter() const;
};