#ifndef PLANTSUNCAT_DSP2_H
#define PLANTSUNCAT_DSP2_H
#include "../server/WebSocket_Manager.h"
#include "global.h"
class Global;
#define USE_PROCESS 0
#ifdef USE_PROCESS
#include "Psc_Cpp_Core/Shared_Memory/Shared_Memory.h"
#endif
// #ifdef WIN32
// #define USE_PROCESS 1
// #endif
// #define USE_PROCESS 1
extern BaseLogger* dsp_logger;
class DSP_Config_Data {
public:
    std::uint64_t frame_rate;
    bool debug;
    PSC_USE_JSON
};
class DSP_Config : public Base_DSP, public DSP_Config_Data {
public:
    ~DSP_Config();
    void close_dsp_device() const;
    void init_update(const Psc::JSON* that_json) {
        Get_J(center_freq);
        Get_J(band_width);
        Get_J(sample_rate);
        Get_J(fft_point_number);
    }
    void from_json(const Psc::JSON* that_json) {
        Base_DSP::from_base_json(that_json);
        DSP_Config_Data::from_base_json(that_json);
    }
    Psc::JSON to_base_json() const {
        return Base_DSP::to_base_json() += DSP_Config_Data::to_base_json();
    }
    void init_env();
    void set_fft_point_number(uint64_t fft_point_number, bool force = false);
    std::atomic<bool> record_iq_stream_ing = false;
    struct Memory_Buffer {
        std::vector<std::string> get_all() {
            std::vector<std::string> empty;
            std::lock_guard lock(m);
            std::swap(empty, data);
            return empty;
        }
        void push(void* pdata, uint64_t length) {
            std::lock_guard lock(m);
            data.emplace_back((char*)pdata, length);
        }
    protected:
        std::vector<std::string> data;
        Psc::Spin_Lock m;
    };
#if USE_PROCESS
    void adsb_set_less_30Mhz_ddc_param(double center_freq, double sample_rate, double band_width, int sw);
    void adsb_set_less_30Mhz_fft_param(UHD_FFTWin fft_win_type, size_t fft_point_number, size_t fft_period_ms, int sw);
    Psc::SM_Block_RingBuffer json_func_call = Psc::SM_Block_RingBuffer("json_func_call2", 1000);
    Psc::SM_Block_RingBuffer rb = Psc::SM_Block_RingBuffer("rb", 2112 * 1000);
    Psc::Pure_Share_Memory psm = Psc::Pure_Share_Memory("fft_data", 65536);
    void restart_device();
#else
    void server(Global* g);
    std::shared_ptr<WebSocket_Manager> ws_fft;
    std::shared_ptr<WebSocket_Manager> ws_iq;
    Memory_Buffer iq_memory_buffer;
    std::vector<std::uint8_t> fft_data;
    std::mutex fft_mtx;
    void* lib{};
#endif
};
#endif
