#ifndef SPEECHTESTMAINWINDOW_H
#define SPEECHTESTMAINWINDOW_H

#include <QMainWindow>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QWidget>
#include <QPushButton>
#include <QTextEdit>
#include <QLineEdit>
#include <QLabel>
#include <QSpinBox>
#include <QGroupBox>
#include <QSplitter>
#include <QStatusBar>
#include <QTimer>
#include <QAudioSource>
#include <QAudioDevice>
#include <QIODevice>
#include <QCheckBox>
#include <QComboBox>

#include "ASRManager.h"
#include "TTSManager.h"
#include "KWSManager.h"
#include "ModelSettingsDialog.h"
#include "sherpa-onnx/c-api/c-api.h"

class SpeechTestMainWindow : public QMainWindow {
    Q_OBJECT

public:
    explicit SpeechTestMainWindow(QWidget* parent = nullptr);
    ~SpeechTestMainWindow();

private slots:
    void browseFile();
    void startRecognition();
    void startSynthesis();
    void startMicRecognition();
    void stopMicRecognition();
    void processAudioData();
    void openModelSettings();
    void onModelsChanged();
    
    // 录音功能槽函数
    void startRecording();
    void stopRecording();
    void processRecordingData();
    
    // 语音唤醒功能槽函数
    void startKWS();
    void stopKWS();
    void processKWSData();

private:
    void setupUI();
    void setupMenuBar();
    void createOutputDirectories();
    void connectSignals();
    void reinitializeModels();
    bool saveWavFile(const QString& filePath, const QByteArray& audioData, const QAudioFormat& format);
    QByteArray convertAudioFormat(const QByteArray& inputData, const QAudioFormat& inputFormat, 
                                  const QAudioFormat& outputFormat);

    // UI组件
    QLineEdit* filePathEdit;
    QTextEdit* asrResultEdit;
    QTextEdit* ttsTextEdit;
    QTextEdit* ttsResultEdit;
    QSpinBox* speakerIdSpinBox;
    QPushButton* micRecordBtn;
    QPushButton* micStopBtn;
    
    // 录音功能UI组件
    QPushButton* recordBtn;
    QPushButton* recordStopBtn;
    QTextEdit* recordResultEdit;
    
    // 录音设置（设备参数）
    QComboBox* recordSampleRateComboBox;
    QComboBox* recordChannelComboBox;
    
    // 输出设置（保存格式）
    QComboBox* outputSampleRateComboBox;
    QComboBox* outputChannelComboBox;
    
    // 语音唤醒功能UI组件
    QPushButton* kwsStartBtn;
    QPushButton* kwsStopBtn;
    QTextEdit* kwsResultEdit;

    // 管理器
    ASRManager* asrManager;
    TTSManager* ttsManager;
    KWSManager* kwsManager;

    // 音频输入相关（语音识别）
    QAudioSource* audioSource = nullptr;
    QIODevice* audioDevice = nullptr;
    QTimer* audioTimer = nullptr;
    bool isRecording = false;
    const SherpaOnnxOnlineStream* onlineStream = nullptr;
    
    // 音频格式转换相关
    QAudioFormat currentAudioFormat;
    int originalSampleRate = 0;
    int originalChannelCount = 0;
    
    // 录音功能相关
    QAudioSource* recordAudioSource = nullptr;
    QIODevice* recordAudioDevice = nullptr;
    QTimer* recordTimer = nullptr;
    bool isRecordingWav = false;
    QAudioFormat recordAudioFormat;
    QByteArray recordedData;
    QString currentRecordingPath;
    
    // 语音唤醒功能相关
    QAudioSource* kwsAudioSource = nullptr;
    QIODevice* kwsAudioDevice = nullptr;
    QTimer* kwsTimer = nullptr;
    bool isKWSActive = false;
    QAudioFormat kwsAudioFormat;
    
    // KWS sherpa-onnx相关
    const SherpaOnnxKeywordSpotter* kwsSpotter = nullptr;
    const SherpaOnnxOnlineStream* kwsStream = nullptr;
};

#endif // SPEECHTESTMAINWINDOW_H