#ifndef TONEADDITEMDIALOG_H
#define TONEADDITEMDIALOG_H

#include <QDialog>
#include <QLabel>
#include <QList>
#include <QStack>
#include "global/kaglobal.h"
#include "documentlistview.h"
#include "documentitemdelegate.h"
#include "../writing/documentreader.h"
#include "docparsethread.h"
#include "paragraphmanager.h"

namespace Ui {
class ToneAddItemDialog;
}

class ToneAddItemDialog : public QDialog
{
    Q_OBJECT
public:
    explicit ToneAddItemDialog(QWidget *parent = nullptr);
    ~ToneAddItemDialog();

signals:
    void sigDocItemAdd(DocTemplate info);
    void sigToneItemAdd(ToneStyle info);

private slots:
    void on_toolButton_add_clicked();
    void onRecParseMsg(const QString message);
    void onRecParseError(const QString message);

    void on_pushButton_cancel_clicked();
    void on_pushButton_parse_clicked();
    void on_pushButton_prev_clicked();
    void on_pushButton_confirm_clicked();
    void on_toolButton_exampleText_clicked();
    void on_lineEdit_tonestyle_name_textChanged(const QString &arg1);
    void on_closeButton_clicked();
    void onParseTimeout();

private:
    void initUI();
    void initConnect();
    void checkEmptyState();
    void onAddDocument(const QString &path);
    void onShowDeleteConfirm(const QString &filePath);
    void onSelectDocument();
    void checkDuplicateName(const QStringList &filePaths);
    bool onDeleteDocument(const QString &filePath);
    bool isTargetFile(const QString &documentPath);
    bool canEnableNextButton();
    void parsingTone();
    bool parseToneResponse(const QJsonObject &root, QString &toneName, QString &tonePrompt, QString &targetText);
    void updateStep1NextButtonState();
    QJsonObject extractJsonFromString(const QString& input);

private:
    Ui::ToneAddItemDialog *ui;
    DocumentItemDelegate *mDelegate = nullptr;
    DocumentListModel *mDocumentModel = nullptr;
    DocumentReader *mDocumentReader = nullptr;
    DocParseThread *mDocParseThread = nullptr;
    // 段落管理器
    ParagraphManager *m_paragraphManager;
    QString mRecvMsg;
    QTimer* mParseTimer = nullptr;
};

#endif // TONEADDITEMDIALOG_H
