

#include "fl/fx/detail/fx_layer.h"

#include "crgb.h"
#include "fl/fx/frame.h"
#include "fl/fx/fx.h"
#include "fl/stl/cstring.h"

namespace fl {

void FxLayer::setFx(fl::shared_ptr<Fx> newFx) {
    if (newFx != fx) {
        release();
        fx = newFx;
    }
}

void FxLayer::draw(fl::u32 now, float speed, const AudioBatch *audio) {
    // assert(fx);
    if (!frame) {
        frame = fl::make_shared<Frame>(fx->getNumLeds());
    }

    if (!running) {
        // Clear the frame
        fl::memset((u8*)frame->rgb().data(), 0, frame->size() * sizeof(CRGB));
        fx->resume(now);
        mLastNow = now;
        running = true;
    }
    u16 frame_time = static_cast<u16>(now - mLastNow);
    mLastNow = now;
    Fx::DrawContext context(now, frame->rgb(), frame_time, speed, audio);
    fx->draw(context);
}

void FxLayer::pause(fl::u32 now) {
    if (fx && running) {
        fx->pause(now);
        running = false;
    }
}

void FxLayer::release() {
    pause(0);
    fx.reset();
}

fl::shared_ptr<Fx> FxLayer::getFx() { 
    return fx; 
}

fl::span<CRGB> FxLayer::getSurface() {
    return frame->rgb();
}

}
