#include "IntelligenceUI.h"
#include "ui_IntelligenceUI.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QMessageBox>
#include <QFileDialog>
#include <QTimer>
#include <QDateTime>
#include <QDir>
#include <QFile>
#include <QProcess>
#include <QDebug>
#include <QFileInfo>
#include <QAudioRecorder>
#include <QAudioEncoderSettings>
#include <QMediaRecorder>
#include <QRandomGenerator>

IntelligenceUI::IntelligenceUI(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::IntelligenceUI)
    , sshProcess(nullptr)
    , fileTransferProcess(nullptr)
    , m_localAudioPath("")
    , audioRecorder(nullptr)
    , audioInput(nullptr)
    , volumeTimer(nullptr)
    , ttsProcess(nullptr)
    , ttsOutputPath("")
{
    ui->setupUi(this);
    
    // 初始化并加载SSH设置
    updateSshSettings();
    
    // 初始化录制功能
    setupAudioRecorder();
    
    // 初始化TTS功能
    setupTTS();

    // 连接信号槽
    connect(ui->playSelectedAudio, &QPushButton::clicked, this, &IntelligenceUI::on_playSelectedAudio_clicked);
    connect(ui->killWSAudio, &QPushButton::clicked, this, &IntelligenceUI::on_killWSAudio_clicked);
    connect(ui->refreshAudioList, &QPushButton::clicked, this, &IntelligenceUI::on_refreshAudioList_clicked);
    connect(ui->sshSettingsGroup->findChild<QPushButton*>("saveSshSettings"), &QPushButton::clicked, this, &IntelligenceUI::on_saveSshSettings_clicked);
    connect(ui->browseButton, &QPushButton::clicked, this, &IntelligenceUI::on_browseButton_clicked);
    connect(ui->uploadAndPlayButton, &QPushButton::clicked, this, &IntelligenceUI::on_uploadAndPlayButton_clicked);
    
    // 连接录制相关信号槽
    connect(ui->recordButton, &QPushButton::clicked, this, &IntelligenceUI::on_recordButton_clicked);
    connect(ui->stopRecordButton, &QPushButton::clicked, this, &IntelligenceUI::on_stopRecordButton_clicked);
    connect(ui->playRecordedButton, &QPushButton::clicked, this, &IntelligenceUI::on_playRecordedButton_clicked);

    // 连接TTS相关信号槽
    connect(ui->generateTtsButton, &QPushButton::clicked, this, &IntelligenceUI::on_generateTtsButton_clicked);
    connect(ui->playTtsButton, &QPushButton::clicked, this, &IntelligenceUI::on_playTtsButton_clicked);

    // 初始化状态
    updateStatus("情报传达系统已启动，准备就绪");
    ui->progressBar->setValue(0);
}

IntelligenceUI::~IntelligenceUI()
{
    if (sshProcess && sshProcess->state() != QProcess::NotRunning) {
        sshProcess->kill();
        sshProcess->waitForFinished(3000);
    }
    delete ui;
}

void IntelligenceUI::executeSSHCommand(const QString &command, const QString &description)
{
    if (sshProcess && sshProcess->state() != QProcess::NotRunning) {
        updateStatus("上一个命令仍在执行中，请稍候...", true);
        return;
    }

    if (!sshProcess) {
        sshProcess = new QProcess(this);
        connect(sshProcess, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
                this, &IntelligenceUI::onSshProcessFinished);
        connect(sshProcess, &QProcess::errorOccurred,
                this, &IntelligenceUI::onSshProcessError);
    }

    currentCommand = description;
    updateStatus(QString("正在执行: %1").arg(description));
    ui->progressBar->setValue(25);

    // 1. 从UI获取最新的目标板卡设置
    updateSshSettings();

    // 2. 定义跳板机和目标板卡的连接信息
    QString jumpHost = "pi@192.168.12.1";
    QString jumpPassword = "123"; 
    QString targetHost = QString("%1@%2").arg(m_sshUser).arg(m_sshHost);
    QString targetPassword = m_sshPassword;

    // 3. 简化的SSH命令 - 直接用单条命令链接
    QString escapedCommand = command;
    escapedCommand.replace("'", "'\"'\"'"); // 转义单引号
    
    QString fullCommand = QString(
        "sshpass -p '%1' ssh -T -n -o StrictHostKeyChecking=no -o ConnectTimeout=10 %2 "
        "\"sshpass -p '%3' ssh -T -n -o StrictHostKeyChecking=no -o ConnectTimeout=10 %4 '%5'\""
    ).arg(jumpPassword)
     .arg(jumpHost)
     .arg(targetPassword)
     .arg(targetHost)
     .arg(escapedCommand);

    qDebug() << "执行SSH命令:" << fullCommand;
    sshProcess->start("bash", QStringList() << "-c" << fullCommand);
}

void IntelligenceUI::on_playSelectedAudio_clicked()
{
    QString selectedAudio = ui->audioComboBox->currentText();
    playAudioFile(selectedAudio);
}

void IntelligenceUI::on_killWSAudio_clicked()
{
    killWSAudioProcess();
}

void IntelligenceUI::on_refreshAudioList_clicked()
{
    refreshAudioFileList();
}

void IntelligenceUI::on_saveSshSettings_clicked()
{
    updateSshSettings();
    updateStatus("SSH连接设置已更新并保存。");
}

void IntelligenceUI::updateSshSettings()
{
    m_sshHost = ui->lineEditIp->text();
    m_sshUser = ui->lineEditUsername->text();
    m_sshPassword = ui->lineEditPassword->text();

    ui->deviceLabel->setText(QString("当前目标: %1 (%2)").arg(m_sshHost).arg(m_sshUser));
}

void IntelligenceUI::killWSAudioProcess()
{
    QString command_template = "pids=$(ps -aux | grep wsaudio | grep -v grep | awk '{print $2}'); "
                     "if [ ! -z \"$pids\" ]; then "
                     "echo \"找到wsaudio进程: $pids\"; "
                     "echo '%1' | sudo -S kill -9 $pids; "
                     "echo \"已终止wsaudio进程\"; "
                     "else "
                     "echo \"未找到wsaudio进程\"; "
                     "fi";
    QString command = command_template.arg(m_sshPassword);
    
    executeSSHCommand(command, "解除wsaudio音频占用");
}

void IntelligenceUI::playAudioFile(const QString &audioFile)
{
    // 根据您的手动操作日志，文件路径为 audio_file/
    QString remote_audio_path = "audio_file/" + audioFile;

    // 这是最终在目标板卡上执行的脚本，使用换行符使其更清晰
    QString command_script_template = QString(
        "pids=$(ps -aux | grep wsaudio | grep -v grep | awk '{print $2}')\n"
        "if [ ! -z \"$pids\" ]; then\n"
        "  echo \"检测到wsaudio进程，正在终止...\"\n"
        "  echo '%1' | sudo -S kill -9 $pids\n"
        "  sleep 1\n"
        "fi\n"
        "echo \"开始播放音频: %2\"\n"
        "aplay -D plughw:2,0 %2"
    );
    QString command_script = command_script_template.arg(m_sshPassword).arg(remote_audio_path);
    
    executeSSHCommand(command_script, QString("播放音频文件: %1").arg(audioFile));
}

void IntelligenceUI::refreshAudioFileList()
{
    // 根据手动操作日志，文件位于 audio_file/ 目录
    QString command = "ls audio_file/*.wav 2>/dev/null || echo '未找到wav文件'";
    executeSSHCommand(command, "刷新音频文件列表");
}

void IntelligenceUI::onSshProcessFinished(int exitCode, QProcess::ExitStatus exitStatus)
{
    ui->progressBar->setValue(100);
    
    if (exitStatus == QProcess::NormalExit && exitCode == 0) {
        updateStatus(QString("%1 - 执行成功").arg(currentCommand));
    } else {
        updateStatus(QString("%1 - 执行失败 (退出码: %2)").arg(currentCommand).arg(exitCode), true);
    }
    
    // 读取命令输出
    if (sshProcess) {
        QByteArray output = sshProcess->readAllStandardOutput();
        QByteArray error = sshProcess->readAllStandardError();
        
        if (!output.isEmpty()) {
            updateStatus(QString("输出: %1").arg(QString::fromUtf8(output).trimmed()));
        }
        
        if (!error.isEmpty()) {
            updateStatus(QString("错误: %1").arg(QString::fromUtf8(error).trimmed()), true);
        }
    }
    
    // 强制清理进程，确保下次能正常执行
    if (sshProcess) {
        sshProcess->kill();  // 强制终止
        sshProcess->waitForFinished(1000);  // 等待最多1秒
        sshProcess->deleteLater();
        sshProcess = nullptr;
    }
    
    // 重置进度条
    QTimer::singleShot(2000, [this]() {
        ui->progressBar->setValue(0);
    });
}

void IntelligenceUI::onSshProcessError(QProcess::ProcessError error)
{
    ui->progressBar->setValue(0);
    
    QString errorString;
    switch (error) {
        case QProcess::FailedToStart:
            errorString = "命令启动失败";
            break;
        case QProcess::Crashed:
            errorString = "命令执行崩溃";
            break;
        case QProcess::Timedout:
            errorString = "命令执行超时";
            break;
        default:
            errorString = "未知错误";
            break;
    }
    
    updateStatus(QString("%1 - %2").arg(currentCommand).arg(errorString), true);
}

void IntelligenceUI::updateStatus(const QString &message, bool isError)
{
    QString timestamp = QDateTime::currentDateTime().toString("hh:mm:ss");
    QString logMessage = QString("[%1] %2").arg(timestamp).arg(message);
    
    if (isError) {
        ui->logTextEdit->setTextColor(QColor(255, 100, 100));
    } else {
        ui->logTextEdit->setTextColor(QColor(100, 255, 100));
    }
    
    ui->logTextEdit->append(logMessage);
    ui->logTextEdit->setTextColor(QColor(220, 230, 240)); // 重置颜色
    
    // 自动滚动到底部
    ui->logTextEdit->moveCursor(QTextCursor::End);
}

void IntelligenceUI::on_browseButton_clicked()
{
    QString filePath = QFileDialog::getOpenFileName(this, "选择音频文件", QDir::homePath(), "音频文件 (*.wav)");
    if (!filePath.isEmpty()) {
        m_localAudioPath = filePath;
        ui->filePathLineEdit->setText(filePath);
        updateStatus(QString("已选择文件: %1").arg(QFileInfo(filePath).fileName()));
    }
}

void IntelligenceUI::on_uploadAndPlayButton_clicked()
{
    if (m_localAudioPath.isEmpty()) {
        updateStatus("错误: 请先选择一个要上传的音频文件。", true);
        return;
    }

    if (fileTransferProcess && fileTransferProcess->state() != QProcess::NotRunning) {
        updateStatus("上一个文件传输仍在进行中，请稍候...", true);
        return;
    }

    if (!fileTransferProcess) {
        fileTransferProcess = new QProcess(this);
        connect(fileTransferProcess, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
                this, &IntelligenceUI::onFileUploadFinished);
    }
    
    QFileInfo fileInfo(m_localAudioPath);
    QString fileName = fileInfo.fileName();
    QString remotePath = "audio_file/" + fileName;

    currentCommand = QString("上传并播放: %1").arg(fileName);
    updateStatus(QString("正在上传文件: %1...").arg(fileName));
    ui->progressBar->setValue(10);

    // --- 使用cat和管道进行文件传输 ---
    updateSshSettings();
    QString jumpHost = "pi@192.168.12.1";
    QString jumpPassword = "123"; 
    QString targetHost = QString("%1@%2").arg(m_sshUser).arg(m_sshHost);
    QString targetPassword = m_sshPassword;

    const QString commandTemplate = 
        "cat %1 | sshpass -p '%2' ssh -o StrictHostKeyChecking=no -o ConnectTimeout=10 %3 "
        "\"sshpass -p '%4' ssh -o StrictHostKeyChecking=no -o ConnectTimeout=10 %5 'cat > %6'\"";

    QString fullCommand = QString(commandTemplate)
        .arg(m_localAudioPath)    // %1: 本地文件路径
        .arg(jumpPassword)        // %2: 跳板机密码
        .arg(jumpHost)            // %3: 跳板机地址
        .arg(targetPassword)      // %4: 目标板卡密码
        .arg(targetHost)          // %5: 目标板卡地址
        .arg(remotePath);         // %6: 远程文件路径

    qDebug() << "执行文件上传命令:" << fullCommand;
    fileTransferProcess->start("bash", QStringList() << "-c" << fullCommand);
}

void IntelligenceUI::onFileUploadFinished(int exitCode, QProcess::ExitStatus exitStatus)
{
    QFileInfo fileInfo(m_localAudioPath);
    QString fileName = fileInfo.fileName();

    if (exitStatus == QProcess::NormalExit && exitCode == 0) {
        updateStatus(QString("文件 '%1' 上传成功。").arg(fileName));
        ui->progressBar->setValue(50);
        // 上传成功后，立即播放该文件
        playAudioFile(fileName);
    } else {
        updateStatus(QString("文件 '%1' 上传失败 (退出码: %2)").arg(fileName).arg(exitCode), true);
        ui->progressBar->setValue(0);
    }
}

void IntelligenceUI::setupAudioRecorder()
{
    // 创建音频录制器
    audioRecorder = new QAudioRecorder(this);
    
    // 设置音频格式 - 匹配机器狗需要的格式
    QAudioEncoderSettings audioSettings;
    audioSettings.setCodec("audio/pcm");
    audioSettings.setSampleRate(22050);  // 22kHz采样率，匹配warning.wav
    audioSettings.setBitRate(176400);    // 8位单声道的比特率
    audioSettings.setChannelCount(1);    // 单声道
    audioSettings.setQuality(QMultimedia::NormalQuality);
    
    audioRecorder->setAudioSettings(audioSettings);
    audioRecorder->setContainerFormat("wav");
    
    // 连接录制相关信号
    connect(audioRecorder, &QAudioRecorder::durationChanged, this, [this](qint64 duration) {
        updateRecordingStatus(QString("录制中... %1秒").arg(duration / 1000));
    });
    
    connect(audioRecorder, &QAudioRecorder::statusChanged, this, [this](QMediaRecorder::Status status) {
        if (status == QMediaRecorder::UnavailableStatus || status == QMediaRecorder::UnloadedStatus) {
            onRecordingFinished();
        }
    });
    
    connect(audioRecorder, QOverload<QMediaRecorder::Error>::of(&QAudioRecorder::error),
            this, &IntelligenceUI::onRecordingError);
    
    // 创建音量监测定时器
    volumeTimer = new QTimer(this);
    connect(volumeTimer, &QTimer::timeout, this, &IntelligenceUI::updateAudioLevel);
    
    updateRecordingStatus("录制系统就绪");
}

void IntelligenceUI::on_recordButton_clicked()
{
    if (!audioRecorder) {
        updateStatus("错误: 录制器未初始化", true);
        return;
    }
    
    // 创建录制文件路径
    QString recordingsDir = QDir::currentPath() + "/recordings";
    QDir().mkpath(recordingsDir);
    
    QString timestamp = QDateTime::currentDateTime().toString("yyyyMMdd_hhmmss");
    recordedFilePath = recordingsDir + QString("/recorded_%1.wav").arg(timestamp);
    
    updateStatus(QString("准备录制到: %1").arg(recordedFilePath));
    
    audioRecorder->setOutputLocation(QUrl::fromLocalFile(recordedFilePath));
    
    // 开始录制
    audioRecorder->record();
    
    enableRecordingControls(true);
    updateRecordingStatus("正在录制...");
    updateStatus("开始录制语音");
    
    // 启动音量监测
    volumeTimer->start(100);  // 每100ms更新一次音量显示
}

void IntelligenceUI::on_stopRecordButton_clicked()
{
    updateStatus("尝试停止录制...");
    
    if (!audioRecorder) {
        updateStatus("错误: 录制器为空", true);
        return;
    }
    
    QMediaRecorder::State currentState = audioRecorder->state();
    updateStatus(QString("当前录制状态: %1").arg(currentState));
    
    // 无论当前状态如何，都尝试停止
    audioRecorder->stop();
    volumeTimer->stop();
    ui->volumeMeter->setValue(0);
    
    enableRecordingControls(false);
    updateRecordingStatus("正在停止录制...");
    
    // 延迟检查文件是否生成
    QTimer::singleShot(1000, this, [this]() {
        if (QFile::exists(recordedFilePath)) {
            QFileInfo fileInfo(recordedFilePath);
            updateRecordingStatus(QString("录制完成 - %1 (%2 KB)").arg(fileInfo.fileName()).arg(fileInfo.size() / 1024));
            updateStatus(QString("录制完成，文件保存至: %1").arg(recordedFilePath));
            
            // 启用试听按钮
            ui->playRecordedButton->setEnabled(true);
            
            // 自动设置为要上传的文件
            m_localAudioPath = recordedFilePath;
            ui->filePathLineEdit->setText(recordedFilePath);
        } else {
            updateRecordingStatus("录制可能失败，未找到文件");
            updateStatus(QString("录制文件未找到: %1").arg(recordedFilePath), true);
        }
    });
}

void IntelligenceUI::on_playRecordedButton_clicked()
{
    if (recordedFilePath.isEmpty() || !QFile::exists(recordedFilePath)) {
        updateStatus("错误: 没有找到录制的音频文件", true);
        return;
    }
    
    // 使用系统默认播放器试听录制的音频
    QProcess *playProcess = new QProcess(this);
    connect(playProcess, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
            playProcess, &QProcess::deleteLater);
    
    // 尝试使用不同的音频播放命令
    #ifdef Q_OS_LINUX
    playProcess->start("aplay", QStringList() << recordedFilePath);
    #elif defined(Q_OS_WIN)
    playProcess->start("powershell", QStringList() << "-c" << QString("(New-Object Media.SoundPlayer '%1').PlaySync()").arg(recordedFilePath));
    #elif defined(Q_OS_MAC)
    playProcess->start("afplay", QStringList() << recordedFilePath);
    #endif
    
    updateStatus("正在本地试听录制的音频...");
}

void IntelligenceUI::onRecordingFinished()
{
    enableRecordingControls(false);
    volumeTimer->stop();
    ui->volumeMeter->setValue(0);
    
    if (QFile::exists(recordedFilePath)) {
        QFileInfo fileInfo(recordedFilePath);
        updateRecordingStatus(QString("录制完成 - %1 (%2 KB)").arg(fileInfo.fileName()).arg(fileInfo.size() / 1024));
        updateStatus(QString("录制完成: %1").arg(fileInfo.fileName()));
    } else {
        updateRecordingStatus("录制失败");
        updateStatus("录制失败: 文件未生成", true);
    }
}

void IntelligenceUI::onRecordingError(QMediaRecorder::Error error)
{
    QString errorString;
    switch (error) {
        case QMediaRecorder::NoError:
            return;
        case QMediaRecorder::ResourceError:
            errorString = "资源错误";
            break;
        case QMediaRecorder::FormatError:
            errorString = "格式错误";
            break;
        case QMediaRecorder::OutOfSpaceError:
            errorString = "磁盘空间不足";
            break;
        default:
            errorString = "未知错误";
            break;
    }
    
    updateRecordingStatus("录制错误: " + errorString);
    updateStatus("录制错误: " + errorString, true);
    
    enableRecordingControls(false);
    volumeTimer->stop();
    ui->volumeMeter->setValue(0);
}

void IntelligenceUI::updateAudioLevel()
{
    if (!audioRecorder || audioRecorder->state() != QMediaRecorder::RecordingState) {
        return;
    }
    
    // 简化音量显示 - 模拟录制时的音量指示
    static int volumeCounter = 0;
    volumeCounter = (volumeCounter + 1) % 100;
    
    // 模拟音量波动（在实际项目中可以通过其他方式获取真实音量）
    int volumeLevel = 50 + (QRandomGenerator::global()->bounded(30));  // 50-80之间的随机值
    
    ui->volumeMeter->setValue(volumeLevel);
}

void IntelligenceUI::updateRecordingStatus(const QString &status)
{
    ui->recordStatusLabel->setText("录制状态: " + status);
}

void IntelligenceUI::enableRecordingControls(bool recording)
{
    ui->recordButton->setEnabled(!recording);
    ui->stopRecordButton->setEnabled(recording);
    
    if (recording) {
        ui->recordButton->setText("🎤 录制中...");
        ui->recordButton->setStyleSheet(
            "QPushButton { background-color: rgb(165, 85, 45); }"
        );
    } else {
        ui->recordButton->setText("🎤 开始录制");
        ui->recordButton->setStyleSheet(
            "QPushButton { background-color: rgb(45, 125, 65); }"
            "QPushButton:hover { background-color: rgb(65, 145, 85); }"
            "QPushButton:pressed { background-color: rgb(55, 135, 75); }"
        );
    }
}

// ========== TTS相关功能实现 ==========

void IntelligenceUI::setupTTS()
{
    ttsProcess = nullptr;
    updateTtsStatus("TTS系统就绪");
}

void IntelligenceUI::on_generateTtsButton_clicked()
{
    QString text = ui->ttsTextEdit->toPlainText().trimmed();
    if (text.isEmpty()) {
        updateStatus("错误: 请输入要转换的文字内容", true);
        return;
    }
    
    if (ttsProcess && ttsProcess->state() != QProcess::NotRunning) {
        updateStatus("TTS转换正在进行中，请稍候...", true);
        return;
    }
    
    // 创建TTS输出目录
    QString ttsDir = QDir::currentPath() + "/tts_output";
    QDir().mkpath(ttsDir);
    
    // 生成输出文件名
    QString timestamp = QDateTime::currentDateTime().toString("yyyyMMdd_hhmmss");
    ttsOutputPath = ttsDir + QString("/tts_%1.wav").arg(timestamp);
    
    // 获取选择的语音类型
    QString voiceType = ui->voiceComboBox->currentText();
    QString espeakVoice;
    
    if (voiceType == "标准女声") {
        espeakVoice = "cmn"; // 中文普通话
    } else if (voiceType == "标准男声") {
        espeakVoice = "cmn"; // 中文普通话
    } else if (voiceType == "儿童声") {
        espeakVoice = "cmn"; // 中文普通话
    } else {
        espeakVoice = "cmn"; // 默认中文普通话
    }
    
    updateTtsStatus("正在生成语音...");
    updateStatus(QString("正在将文字转换为语音: %1").arg(text.left(50) + (text.length() > 50 ? "..." : "")));
    
    // 创建TTS进程
    if (!ttsProcess) {
        ttsProcess = new QProcess(this);
        connect(ttsProcess, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
                this, &IntelligenceUI::onTtsGenerationFinished);
    }
    
    // 构建espeak命令
    // -v: 语音类型, -s: 语速, -a: 音量, -w: 输出到WAV文件
    QStringList arguments;
    arguments << "-v" << espeakVoice
              << "-s" << "150"  // 语速 150 wpm
              << "-a" << "100"  // 音量 100
              << "-w" << ttsOutputPath  // 输出文件
              << text;  // 要转换的文字
    
    qDebug() << "TTS命令:" << "espeak-ng" << arguments.join(" ");
    ttsProcess->start("espeak-ng", arguments);
}

void IntelligenceUI::on_playTtsButton_clicked()
{
    if (ttsOutputPath.isEmpty() || !QFile::exists(ttsOutputPath)) {
        updateStatus("错误: 没有找到TTS生成的音频文件", true);
        return;
    }
    
    // 使用系统默认播放器试听TTS音频
    QProcess *playProcess = new QProcess(this);
    connect(playProcess, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
            playProcess, &QProcess::deleteLater);
    
    // 尝试使用不同的音频播放命令
    #ifdef Q_OS_LINUX
    playProcess->start("aplay", QStringList() << ttsOutputPath);
    #elif defined(Q_OS_WIN)
    playProcess->start("powershell", QStringList() << "-c" << QString("(New-Object Media.SoundPlayer '%1').PlaySync()").arg(ttsOutputPath));
    #elif defined(Q_OS_MAC)
    playProcess->start("afplay", QStringList() << ttsOutputPath);
    #endif
    
    updateStatus("正在本地试听TTS生成的音频...");
}

void IntelligenceUI::onTtsGenerationFinished(int exitCode, QProcess::ExitStatus exitStatus)
{
    if (exitStatus == QProcess::NormalExit && exitCode == 0) {
        if (QFile::exists(ttsOutputPath)) {
            QFileInfo fileInfo(ttsOutputPath);
            updateTtsStatus(QString("TTS完成 - %1 (%2 KB)").arg(fileInfo.fileName()).arg(fileInfo.size() / 1024));
            updateStatus(QString("TTS生成成功: %1").arg(fileInfo.fileName()));
            
            // 启用试听按钮
            ui->playTtsButton->setEnabled(true);
            
            // 自动设置为要上传的文件
            m_localAudioPath = ttsOutputPath;
            ui->filePathLineEdit->setText(ttsOutputPath);
            
            updateStatus("TTS音频已自动设置为上传文件，可直接点击'上传并播放'");
        } else {
            updateTtsStatus("TTS失败: 文件未生成");
            updateStatus("TTS生成失败: 文件未生成", true);
        }
    } else {
        updateTtsStatus("TTS生成失败");
        updateStatus(QString("TTS生成失败 (退出码: %1)").arg(exitCode), true);
        
        // 读取错误信息
        if (ttsProcess) {
            QByteArray error = ttsProcess->readAllStandardError();
            if (!error.isEmpty()) {
                updateStatus(QString("TTS错误: %1").arg(QString::fromUtf8(error).trimmed()), true);
            }
        }
    }
    
    // 清理进程
    if (ttsProcess) {
        ttsProcess->deleteLater();
        ttsProcess = nullptr;
    }
}

void IntelligenceUI::updateTtsStatus(const QString &status)
{
    ui->ttsStatusLabel->setText("TTS状态: " + status);
} 