#include <Block/SpriteBlock/BasicBlock/Zype/Zype.hpp>
#include <ChunkWindow/ChunkWindow.hpp>
#include <random>

class ZypeEffect : public BlockEffect
{
private:
    glm::vec3 offset;
    uint32_t lastParticleSpawn = 0;

public:
    ZypeEffect(glm::vec3 offset) : offset(offset) {}

    virtual void update(ChunkWindow &window) override
    {
        if (SDL_GetTicks() - lastParticleSpawn >= 10)
        {
            std::random_device rd;
            std::mt19937 gen(rd());
            std::uniform_real_distribution<float> posDis(0, 1);
            std::uniform_real_distribution<float> unitDis(-1, 1);

            lastParticleSpawn = SDL_GetTicks();

            for (int i = 0; i < 3; i++)
            {
                ParticlesStage::Particle particle{};
                particle.startPos = offset + glm::vec3(posDis(gen), posDis(gen), 1.0f);
                particle.startVel = glm::vec3(0.1f * unitDis(gen), 0.1f * unitDis(gen), 0.7f + 0.3f * posDis(gen));
                particle.startSize = glm::vec3(1.0f/8.0f);
                particle.endSize = glm::vec3(0.0f);
                particle.acceleration = glm::vec3(0.0f, 0.0f, -0.04f);
                particle.startColor = glm::vec4(0.0f, 0.0f, 1.0f, 1.0f);
                particle.endColor = glm::vec4(1.0f, 0.0f, 0.0f, 1.0f);
                particle.startTime = lastParticleSpawn;
                particle.endTime = particle.startTime + 4000;

                window.emitParticle(particle);
            }
        }
    }
};

Zype::Zype(std::string name) : BasicBlock(
    name,
    "Zype",
    60.0f,
    {"zype", "zype", "zypeFront", "zype", "zype", "zype"}
)
{
    withGlassSounds();
}

bool Zype::canManuallyDig()
{
    return true;
}

glm::vec3 Zype::getLightColor(uint8_t meta)
{
    return glm::vec3(1.0f, 0.0f, 0.0f);
}

void Zype::getEffects(uint8_t meta, glm::vec3 offset, std::vector<std::shared_ptr<BlockEffect>> &effects)
{
    effects.push_back(std::make_shared<ZypeEffect>(offset));
}