#pragma once

#include "Chunk.hpp"
#include "Area.hpp"
#include "../../core/world/EdittableChunk.hpp"

namespace world::server {

// Server and Client merged chunk
class SharedChunk final: public Chunk, public world::client::EdittableChunk {
public:
    SharedChunk(): world::Chunk(), Chunk(), world::client::EdittableChunk() { }
    SharedChunk(const chunk_pos &pos, const std::unique_ptr<generator::Abstract> &rnd): world::Chunk(), Chunk(pos, rnd), world::client::EdittableChunk() { }
    SharedChunk(std::istream &str, bool rle = RLE): world::Chunk(str, rle), Chunk(), world::client::EdittableChunk() { }

    /// Break voxel
    std::optional<Item> replace(chunk_voxel_idx idx, const Voxel &val, float delay = 0) override {
        const auto res = voxels[idx];
        set(idx, val);
        edits.erase(idx);
        if(delay > 0) {
            edits.emplace(idx, EditBody{res, delay});
        } else {
            invalidate(idx);
        }
        return {Item{res.density(), res.material()}};
    }
};
}