#pragma once

#include "../Parameters/DeepPluckParameters.h"

#include <juce_audio_basics/juce_audio_basics.h>
#include <juce_dsp/juce_dsp.h>

#include <array>
#include <vector>

namespace deeppluck
{
class DeepPluckReverb final
{
public:
    void prepare (double sampleRate, int maximumBlockSize);
    void reset() noexcept;
    void setParameters (const DeepPluckParameterSnapshot& snapshot) noexcept;
    void process (juce::AudioBuffer<float>& buffer, int startSample, int numSamples) noexcept;

    [[nodiscard]] bool isIdle() const noexcept;

private:
    static constexpr double maximumPreDelaySeconds = 0.120;
    static constexpr float tailSilenceThreshold = 1.0e-5f;
    static constexpr int requiredQuietBlocks = 8;
    static constexpr float wetCalibration = 0.45f;

    [[nodiscard]] float readPreDelay (int channel, double delayInSamples) const noexcept;
    void clearSignalState() noexcept;

    juce::dsp::Reverb reverb;
    juce::AudioBuffer<float> wetBuffer;
    std::array<std::vector<float>, 2> preDelayBuffer;
    juce::SmoothedValue<double, juce::ValueSmoothingTypes::Linear> preDelaySamples;
    juce::SmoothedValue<float, juce::ValueSmoothingTypes::Linear> mix;
    juce::SmoothedValue<float, juce::ValueSmoothingTypes::Linear> feedGain;
    juce::SmoothedValue<float, juce::ValueSmoothingTypes::Linear> bypassDry;
    double currentSampleRate = 44100.0;
    int writePosition = 0;
    int quietBlockCount = 0;
    bool active = false;
    bool bypassed = true;
};
}
