/*
 * Copyright (C) 2020, KylinSoft Co., Ltd.
 *
 * This program 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 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program.  If not, see <https://www.gnu.org/licenses/>.
 */
#ifndef SYSTEMCHAT_H
#define SYSTEMCHAT_H

#include "global/kaglobal.h"
#include "commandtemplatelist.h"

#include <QDebug>
#include <QFile>
#include <QFileInfo>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QObject>
//#include <kylin-ai/private/osassistant/osassistant.h>
#include <string>
#include <aiconfig/modelconfig.h>
#include <QTimer>
#include <string>
#include <aiconfig/modelconfig.h>
#include <QTimer>
#include <chatdatabase.h>

using namespace kyai::assistant;

class SystemChat : public QObject
{
    Q_OBJECT
public:
    explicit SystemChat(QObject *parent = nullptr);
    ~SystemChat();

    static SystemChat *getPtr();
    void initSystemChat();
	void initCommandList();
    void initWithHistory(std::vector<Message> messages);

    void setActor(int actorId);
    std::vector<Prompt> getPrompts();
    int createCommand(QString name, QString content);
    bool updateCommand(int actorId, QString name, QString content);
    bool deleteCommand(int actorId);
    bool setCommandOrder(QList<CommandTemplateItem> commandTemplateList);

    void stopChat();
    void clearContext();
    void sendMessage(QString text, QString filePath);
    void sendToolMessage(int toolId, FileType fileType, QString para);
    void setSession(int sessionID);
    int getSession();
    bool getFeatureStatus(Features::Feature feature);   //根据AI SDK返回是否支持某种能力

signals:
    void reply(const QString &reasoningContentMsg, const QString &message, bool is_reasoning_end, bool is_end, int index, int errorCode, QString model, QString feedbackURL);
    void sigGenerateSummary(const QString &message, bool is_end, int index, int errorCode, QString model);
    void sigPictureUpdate(const QString &data, const QString &errorMessage);
    void pictureOperateUpdate(const QString &data, const QString &errorMessage);
    void sigInfoSourceUpdate(QMap<int, QString> imageToolMap);
    void toolReply(const QString &message, QMap<int, QString> imageToolMap, QString imageUrl, int errorCode,  QString model, QString feedbackURL);
    void sigAddScheduleDate(int operatorId, QString schedule_date, QString msg, QString model, QString feedbackURL);
    void sigSoundMsg(const QString &message, bool is_end, int index);
    void sigGeneratingPic(QString model, QString feedbackURL);

private:
    FileType judgeFileType(QString filePath);

public:
    TartgetType target = RECVMSG;
    bool is_end;
    bool is_reasoning_end;
    QString result_msg;
    int result_index;
    QString text;
    bool soundFlag = false;
    QString reasoningContentMsg;

private:
    OsAssistant *m_osassistant = nullptr;
    int m_sessionID;            //助手维护，用于区分会话上下文
};

#endif // SYSTEMCHAT_H
