#include <PipelineStage/DrawStage/ParticlesStage/ParticlesStage.hpp>
#include <FrameDrawer/FrameDrawer.hpp>
#include <Display/WidgetDisplay/WorldDisplay/WorldDisplay.hpp>
#include <Settings/Settings.hpp>
#include <Vk/ShaderConst.hpp>
#include <IndexedUnitCube/IndexedUnitCube.hpp>

bool ParticlesStage::Particle::isAlive() const
{
    return SDL_GetTicks() < endTime;
}

ParticlesStage::ParticlesStage(FrameDrawer &frameDrawer, std::shared_ptr<Vk::DescriptorSet> dataDescSet) :
    DrawStage(
        "Particles",
        frameDrawer,
        *frameDrawer.worldDisplay.particlesRenderPass,
        *frameDrawer.worldDisplay.particlesFramebuffer,
        *frameDrawer.worldDisplay.particlesPipeline,
        {}
    ),
    dataDescSet(dataDescSet)
{
    clearValues.push_back({{{0.0f, 0.0f, 0.0f, 0.0f}}});
}

void ParticlesStage::onFrameBegins()
{
    cmdbuf->bindDescriptorSet(
        VK_PIPELINE_BIND_POINT_GRAPHICS,
        pipeline,
        GLSL_PARTICLES_FRAME_DESCRIPTOR_SET,
        *dataDescSet
    );
}

void ParticlesStage::draw(Vk::DescriptorSet &descSet, uint32_t count)
{
    if (!settings.particleEffects)
    {
        return;
    }

    cmdbuf->bindDescriptorSet(
        VK_PIPELINE_BIND_POINT_GRAPHICS,
        pipeline,
        GLSL_PARTICLES_EMITTER_DESCRIPTOR_SET,
        descSet
    );

    IndexedUnitCube::draw(*cmdbuf, count);
}