#include <QDir>
#include <QFileInfo>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QProcess>

#include "channelmessage.h"

struct MeetingItem{
    QString name;
    QString path;
    QString time;
//    QString duration;
};

ChannelMessage::ChannelMessage(QObject *parent) : QObject(parent)
{

}

ChannelMessage::~ChannelMessage()
{}

void ChannelMessage:: sendMessageFromJs(const QString &msgHead, const QString &message) {
    qDebug() << "msgHead:" << msgHead << "  Called from js with message:" << message;
    emit sigMessageFromJs(msgHead, message);
}

void ChannelMessage:: clearMessageFromJs() {
    emit sigClearFromJs();
}

void ChannelMessage:: updateReply(const QString &msgHead, const QString &message) {
    emit sigUpdateReply(msgHead, message);
}

void ChannelMessage::stopReply()
{
    emit sigStopReply();
}

void ChannelMessage::saveMessageList()
{
    emit sigSaveMessageList();
}

void ChannelMessage::getMessageList()
{
    emit sigGetMessageList();
}

void ChannelMessage::startVoiceInput()
{
    emit sigStartRecord();
}

void ChannelMessage::stopVoiceInput()
{
    emit sigStopRecord();
}

void ChannelMessage::startRuntime(QString lan, QString tartget)
{
//    qDebug() << "test channel " << lan << "---" << tartget;
    emit sigStartRuntime(lan, tartget);
}

void ChannelMessage::stopRuntime()
{
    emit sigStopRuntime();
}

void ChannelMessage::clearDictation()
{
    emit sigClearDictation();
}

void ChannelMessage::copyDictation()
{
    emit sigCopyDictation();
}

void ChannelMessage::startMeeting()
{
    emit sigStartMeeting();
}

void ChannelMessage::pauseMeeting()
{
    emit sigPauseMeeting();
}

void ChannelMessage::stopMeeting()
{
    emit sigStopMeeting();
}


void ChannelMessage::editName(QString originalName)
{
    emit sigRename(originalName);
}

void ChannelMessage::getMeetingList()
{
    //扫描本地文件生成List
    QString documentsDir = QDir::homePath() + "/文档";
    QString directoryPath = documentsDir + "/kylin-aiassistant/meeting";
    QDir dir(directoryPath);
    if (!dir.exists()) {
        qDebug() << "目录不存在：" << directoryPath;
        return;
    }

    QJsonArray jsonArray;
    QStringList filters;
    filters << "*.txt"; // 设置过滤器以只获取txt文件
    QStringList entries = dir.entryList(filters, QDir::Files); // 获取文件列表
    foreach (const QString &entry, entries) {
        QFileInfo fileInfo(dir.filePath(entry));
//        qDebug() << "找到JSON文件：" << fileInfo.fileName(); // 输出文件名
        QString fileName = fileInfo.fileName();
        int indexUnder = fileName.indexOf('_');
        int indexDot = fileName.indexOf('.');

        QJsonObject jsonObject;
        jsonObject["path"] = directoryPath + "/" + fileName;
        jsonObject["name"] = fileName.mid(0,indexDot);
        QString date = fileName.mid(indexUnder + 1,  4) + "年";
        date += fileName.mid(indexUnder + 5, 2) + "月";
        date += fileName.mid(indexUnder + 7, 2) + "日 ";
        date += fileName.mid(indexUnder + 10, 2) + ":";
        date += fileName.mid(indexUnder + 12, 2);

        jsonObject["time"] = date;
        qDebug() << date;

        jsonArray.append(jsonObject);

    }

    QString result = QJsonDocument(jsonArray).toJson(QJsonDocument::Indented);

    emit sigMeetingList(result);
}

void ChannelMessage::openMeetingFile(QString path)
{
    QProcess process;
    QString command = "pluma";
    QStringList arguments;
    arguments << path;
    process.start(command, arguments);
    process.waitForFinished(-1);
    // 检查进程是否正常退出
    if (process.exitStatus() == QProcess::NormalExit) {
        qDebug() << "Pluma opened the file successfully!";
    } else {
        qDebug() << "Failed to open the file with Pluma!";
    }

}

void ChannelMessage::deleteMeetingFile(QString path)
{
    QFile file(path);
    if (file.remove()) {
        qDebug() << "File removed successfully:" << path;
    } else {
        qDebug() << "Failed to remove file:" << path;
    }
    getMeetingList();
}
