#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 "../../../../../core_library/Core/Core/Shared_Memory/Shared_Memory.h"
#endif
// #ifdef WIN32
// #define USE_PROCESS 1
// #endif
// #define USE_PROCESS 1

enum Demod_Mode {
    LSB,
    USB,
    AM,
    FM
};


extern BaseLogger* dsp_logger;
struct DSP_Config : Base_DSP {
    std::string   url;
    std::uint16_t port{};
    std::int64_t  power_min;
    std::int64_t  power_max;
    std::int64_t  power_offset;
    std::uint64_t frame_rate;
    bool          debug;
    Demod_Mode demod_mode;
    std::int64_t audio_gain;
    std::int64_t color_gain;
    ~DSP_Config();

    void close_dsp_device() const;

    void  init_update(const Psc::JSON *that_json) {
        Get_J(port);
        Get_J(center_freq);
        Get_J(band_width);
        Get_J(sample_rate);
        Get_J(fft_point_number);
        Get_J(power_min);
        Get_J(power_max);
        Get_J(power_offset);
        Get_J(demod_mode);
        Get_J(audio_gain);
        Get_J(color_gain);
    }
    void init(const Psc::JSON *that_json) {
        init_update(that_json);
        Get_J(url);
        Get_J(fft_period_ms);
        Get_J(fft_win_type);
        Get_J(debug);
        Get_J(frame_rate);
        Get_J(library_path);
    }
    Psc::JSON to_json() const {
        Psc::JSON ret = Base_DSP::to_json();
        Ret_J(url);
        Ret_J(port);
        Ret_J(power_min);
        Ret_J(power_max);
        Ret_J(power_offset);
        Ret_J(frame_rate);
        Ret_J(debug);
        Ret_J(demod_mode);
        Ret_J(audio_gain);
        Ret_J(color_gain);
        return ret;
    }


    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
