#include "surrounding.hpp"

#include "../../core/world/Area.hpp"
#include "../world/Chunk.hpp"
#include "../../core/data/math.hpp"

using namespace geometry;
namespace contouring::surrounding {
    bool load(corners &out, const chunk_pos &chunkPos, const world::ChunkContainer &chunks) {
        {
            const auto chunk = chunks.findInRange(chunkPos);
            if(!chunk.has_value())
                return false;

            out[0] = std::dynamic_pointer_cast<world::client::EdittableChunk>(chunk.value());
        }
        for (size_t i = 1; i < 8; i++) {
            const auto pos = chunkPos + g_corner_offsets[i];
            if (chunks.inRange(pos)) {
                if(const auto chunk = chunks.findInRange(pos)) {
                    out[i] = std::dynamic_pointer_cast<world::client::EdittableChunk>(chunk.value());
                } else
                    return false;
            } else {
                out[i] = world::client::EMPTY_CHUNK;
            }
        }
        return true;
    }
    void load(corners &out, const glm::ucvec3 &areaSize, const glm::ucvec3 &chunkPos, const std::vector<std::shared_ptr<world::Chunk>> &area) {
        for (size_t i = 0; i < 8; i++) {
            const auto pos = chunkPos + glm::ucvec3(g_corner_offsets[i]);
            if (pos.x < areaSize.x && pos.y < areaSize.y && pos.z < areaSize.z) {
                out[i] = std::dynamic_pointer_cast<world::client::EdittableChunk>(area.at(glm::toIdx(pos, areaSize)));
            } else {
                out[i] = world::client::EMPTY_CHUNK;
            }
        }
    }
}
