#ifndef BATCHPROCESSING_H
#define BATCHPROCESSING_H

#include <QDialog>
#include <QFileSystemWatcher>
#include <QStringListModel>
#include <QCompleter>
#include <QLineEdit>
#include "scriptengine.h"

namespace Ui { class BatchProcessing; }

class Database;

class BatchProcessing : public QDialog
{
    Q_OBJECT
    Ui::BatchProcessing *_ui;
    QString _scriptBasePath;
    QFileSystemWatcher _fileWatcher;
    Script::ScriptEngineThread *_engineThread = nullptr;
    Script::ScriptEngine *_engine = nullptr;
    QColor _textColor;
    Database *_database;
    QStringListModel *_completerModel = nullptr;
    QCompleter *_completer = nullptr;
    QList<QPair<QString, QString>> _paths;
private slots:
    void scanScriptDir();
public:
    explicit BatchProcessing(Database *database, QWidget *parent = nullptr);
    ~BatchProcessing();
    void setOutputDir(const QString &output);
    void setPaths(const QStringList &paths);
protected:
    void closeEvent(QCloseEvent *event);
    void refreshPaths();
public slots:
    void addFiles();
    void addDir();
    void addMarked();
    void removePath();
    void removeAllPaths();
    void browse();
    void openScriptDir();
    void runScript();
    void runScript(const QString &script, const QString &arg, bool exit);
    void stopScript();
    void scriptFinished();
    void newMessage(const QString &message, bool error);
    void newMessageCli(const QString &message, bool error);

    QJSValue getString(const QString &label, const QString &text);
    QJSValue getInt(const QString &label, int value);
    QJSValue getFloat(const QString &label, double value, int decimals);
    QJSValue getItem(const QStringList &items, const QString &label, int current);
    QJSValue question(const QString &question, const QStringList &buttons, const QString &title = "");

    void plot(const QVariant &graph);
};

class ConsoleLine : public QLineEdit
{
    Q_OBJECT
public:
    explicit ConsoleLine(QWidget *parent = nullptr);
    void addLine();
    void keyReleaseEvent(QKeyEvent *event) override;
private:
    int _currentLine = 0;
    QStringList _history;
};

void openDir(const QString &path);

#endif // BATCHPROCESSING_H
