/*
  ==============================================================================

    This file contains the basic framework code for a JUCE plugin processor.

  ==============================================================================
*/

#pragma once

#include <juce_audio_processors/juce_audio_processors.h>
#include <juce_audio_utils/juce_audio_utils.h>
#include <juce_dsp/juce_dsp.h>
//==============================================================================
/**
*/

class AudioFIFO {
public:
    AudioFIFO();
    ~AudioFIFO();

    juce::Array<float> sampleStack;
    void loadSamplesToFIFO(const juce::AudioBuffer<float> &samples);
    juce::Array<float> sendSamplesToEditor();
    juce::SpinLock     lock;

private:


};

class CrystalizerEQAudioProcessor  : public juce::AudioProcessor , public juce::AudioProcessorValueTreeState::Listener
{
public:
    //==============================================================================
    CrystalizerEQAudioProcessor();
    ~CrystalizerEQAudioProcessor() override;
    void parameterChanged (const juce::String& id, float newValue) override;


    //==============================================================================
    void prepareToPlay (double sampleRate, int samplesPerBlock) override;
    void releaseResources() override;

   #ifndef JucePlugin_PreferredChannelConfigurations
    bool isBusesLayoutSupported (const BusesLayout& layouts) const override;
   #endif

    void processBlock (juce::AudioBuffer<float>&, juce::MidiBuffer&) override;

    //==============================================================================
    juce::AudioProcessorEditor* createEditor() override;
    bool hasEditor() const override;

    //==============================================================================
    const juce::String getName() const override;

    bool acceptsMidi() const override;
    bool producesMidi() const override;
    bool isMidiEffect() const override;
    double getTailLengthSeconds() const override;

    //==============================================================================
    int getNumPrograms() override;
    int getCurrentProgram() override;
    void setCurrentProgram (int index) override;
    const juce::String getProgramName (int index) override;
    void changeProgramName (int index, const juce::String& newName) override;


    //==============================================================================
    void getStateInformation (juce::MemoryBlock& destData) override;
    void setStateInformation (const void* data, int sizeInBytes) override;

    // --------- Parameter (APVTS) ----------
    static juce::AudioProcessorValueTreeState::ParameterLayout createParameterLayout();
    juce::AudioProcessorValueTreeState apvts { *this, nullptr, "PARAMS", createParameterLayout() };

    void setPresetName (const juce::String& s);
    juce::String getPresetName() const noexcept { return presetName; }

    juce::StringArray getPresetNamesArray() const;

    juce::String savePresetToFile() const;

    void loadPreset(const juce::String &preset);

    void deletePreset(const juce::String &preset) const;

    void resetAllParameters() const;

    AudioFIFO audioFIFO;


private:
    // --------- EQ-Kette ----------
    using Gain = juce::dsp::Gain<float>;
    using Filter = juce::dsp::IIR::Filter<float>;
    using Chain  = juce::dsp::ProcessorChain<Gain, Filter, Filter, Filter, Filter, Filter, Filter, Filter, Filter, Filter, Filter, Filter, Filter, Filter, Gain>; // 0: LowCut (HP), 1: Peak, 2: HighCut (LP)
    juce::Random noiseRandL, noiseRandR;
    Chain leftChain, rightChain;


    void updateFilters(); // Koeffizienten aus Parametern berechnen





    juce::AudioBuffer<float> processMultiBand(juce::AudioBuffer<float>& lowBuf, juce::AudioBuffer<float>& highBuf);


    using Coeff = juce::dsp::IIR::Coefficients<float>;

    juce::dsp::LinkwitzRileyFilter<float> mbLowpass;
    juce::dsp::LinkwitzRileyFilter<float> mbHighpass;
    juce::dsp::IIR::Filter<float> mbHighPeak;
    juce::dsp::WaveShaper<float> saturator;


    juce::String presetName { "Init" };

    //==============================================================================
    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CrystalizerEQAudioProcessor)
};


