#pragma once

#include "common/dsp/complex.h"
#include "dsp/block.h"
#include <fftw3.h>
#include <functional>
#include <mutex>
#include <volk/volk_alloc.hh>

namespace satdump
{
    namespace ndsp
    {
        class FFTPanBlock : public Block
        {
        private:
            std::mutex fft_mutex;
            volk::vector<float> fft_taps;
            int fft_size;
            bool work();

            void destroy_fft();

            complex_t *fft_input_buffer;
            float *fft_output_buffer = nullptr;

            int in_reshape_buffer = 0;
            complex_t *fft_reshape_buffer;

            fftwf_complex *fftw_in;
            fftwf_complex *fftw_out;
            fftwf_plan fftw_plan;

            int rbuffer_rate = 0;
            int rbuffer_size = 0;
            int rbuffer_skip = 0;

            int reshape_buffer_size = 0;

        public:
            FFTPanBlock();
            ~FFTPanBlock();
            void set_fft_settings(int size, uint64_t samplerate, int rate = 60);

            std::function<void(float *, size_t)> on_fft = [](float *, size_t size) {};

            float *output_fft_buff;

            float avg_num = 10;

            nlohmann::json get_cfg(std::string key)
            {
                // if (key == "max_gain")
                //     return p_max_gain;
                // else
                throw satdump_exception(key);
            }

            cfg_res_t set_cfg(std::string key, nlohmann::json v)
            {
                // if (key == "max_gain")
                //     p_max_gain = v;
                // else
                throw satdump_exception(key);
                return RES_OK;
            }
        };
    } // namespace ndsp
} // namespace satdump