/* -*- c++ -*- */
/*
 * Gqrx SDR: Software defined radio receiver powered by GNU Radio and Qt
 *           http://gqrx.dk/
 *
 * Copyright 2013 Christian Lindner DL2VCL, Stefano Leucci.
 *
 * Gqrx is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 *
 * Gqrx is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Gqrx; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street,
 * Boston, MA 02110-1301, USA.
 */
#ifndef SPECWIDGET_H
#define SPECWIDGET_H

#include <QWidget>
#include <QVariant>
#include <fftw3.h>
#include <vector>
#include <QStandardItemModel>

#ifdef Q_OS_WINDOWS
#include "uhd_device_win32.h"
#endif
#ifdef Q_OS_LINUX
#include "uhd_device.h"
#endif
#include "stdout_watcher.h"
namespace Ui {
	class specWidget;
}

class specWidget : public QWidget, public uhd_device
{
	Q_OBJECT
public:
	explicit specWidget(QWidget *parent = nullptr);
	~specWidget();
	void setSampleRate(double rate);
protected:
	void timerEvent(QTimerEvent * e) override;
public slots:
	void slot_newDemodFreq(qint64 freq, qint64 delta); /* delta is the offset from the center */
	void slot_msg(QByteArray , bool);
public:
	void appendWavComplex(const double (* pWav)[2], const int count, double voltage_ref = 16384);
public:
	void setCenterFreq(quint64 f);	
private slots:
	void on_pushButton_reset_clicked();
	void on_comboBox_fftSize_activated(int index);
	void on_doubleSpinBox_center_valueChanged(double arg1);
	void on_spinBox_gain_valueChanged(int arg1);
	void on_doubleSpinBox_range_max_valueChanged(double arg1);
	void on_doubleSpinBox_range_min_valueChanged(double arg1);
	void on_comboBox_atn_currentIndexChanged(const QString &arg1);
	void on_spinBox_chan_valueChanged(int arg1);
	void on_toolButton_br_clicked();
	void on_checkBox_save_stateChanged(int arg1);
	void on_spinBox_bw_valueChanged(int arg1);
	void on_toolButton_br_play_clicked();
	void on_checkBox_play_stateChanged(int arg1);
	void on_doubleSpinBox_center_tx_valueChanged(double arg1);
	void on_spinBox_chan_tx_valueChanged(int arg1);
	void on_spinBox_gain_tx_valueChanged(int arg1);
	void on_spinBox_bw_tx_valueChanged(int arg1);

private:
	void loadSettings();
	void saveSettings();
private:
	void resetProgram();
	void stopProgram();

	void initFFTW();
	void freeFFTW();
	void initRx();
	void initTx();
	void freeRx();
	void freeTx();
private:
	Ui::specWidget *ui = nullptr;
	QStandardItemModel * m_attenMod = nullptr;
private:
	int	m_nFFTSize = 16384;
	std::vector<double> m_dHammingWnd;
	std::vector<double> m_dFFTAmp;
	fftw_plan m_pFFTPlan = nullptr;
	fftw_complex * m_pFFTIn = nullptr;
	fftw_complex * m_pFFTOut = nullptr;
	int m_nTimerID = -1;
	int m_nCurrentCenter = 0;
	double m_dManuAdd = 0;
	//Thread
	uhd_io_thread * m_rxth = nullptr;
	uhd_io_thread * m_saveth = nullptr;
	uhd_io_thread * m_txth = nullptr;
	uhd_io_thread * m_readth = nullptr;
	unsigned long long save_count;
	unsigned long long read_count;
	std::atomic<bool> m_bSaveToFile;
	std::atomic<bool> m_bPlayFromFile;
	QString m_strFolder;
	QString m_strFolderPlay;
	//stdout reader
	stdout_watcher * watcher = nullptr;
};

#endif // SPECWIDGET_H
