#ifndef QTCPROCESS_H
#define QTCPROCESS_H


#include "environment.h"

#include <QProcess>

namespace Utils {
class AbstractMacroExpander;

class QtcProcess : public QProcess
{
    Q_OBJECT

public:
    QtcProcess(QObject *parent = nullptr);

    void setEnvironment(const Environment &env) { m_environment = env; m_haveEnv = true; }
    void setCommand(const CommandLine &cmdLine) { m_commandLine  = cmdLine; }
    void setUseCtrlCStub(bool enabled);
    void start();
    void terminate();
    void interrupt();
    void setLowPriority() { m_lowPriority = true; }

    class Arguments
    {
    public:
        static Arguments createWindowsArgs(const QString &args);
        static Arguments createUnixArgs(const QStringList &args);

        QString toWindowsArgs() const;
        QStringList toUnixArgs() const;
        QString toString() const;

    private:
        QString m_windowsArgs;
        QStringList m_unixArgs;
        bool m_isWindows;
    };

    enum SplitError {
        SplitOk = 0, //! All went just fine
        BadQuoting, //! Command contains quoting errors
        FoundMeta //! Command contains complex shell constructs
    };

    //! Quote a single argument for usage in a unix shell command
    static QString quoteArgUnix(const QString &arg);
    //! Quote a single argument for usage in a shell command
    static QString quoteArg(const QString &arg, OsType osType = HostOsInfo::hostOs());
    //! Quote a single argument and append it to a shell command
    static void addArg(QString *args, const QString &arg, OsType osType = HostOsInfo::hostOs());
    //! Join an argument list into a shell command
    static QString joinArgs(const QStringList &args, OsType osType = HostOsInfo::hostOs());
    //! Prepare argument of a shell command for feeding into QProcess
    static Arguments prepareArgs(const QString &cmd, SplitError *err,
                                 OsType osType = HostOsInfo::hostOs(),
                                 const Environment *env = nullptr, const QString *pwd = nullptr,
                                 bool abortOnMeta = true);
    //! Prepare a shell command for feeding into QProcess
    static bool prepareCommand(const QString &command, const QString &arguments,
                               QString *outCmd, Arguments *outArgs, OsType osType = HostOsInfo::hostOs(),
                               const Environment *env = nullptr, const QString *pwd = nullptr);
    //! Quote and append each argument to a shell command
    static void addArgs(QString *args, const QStringList &inArgs);
    //! Append already quoted arguments to a shell command
    static void addArgs(QString *args, const QString &inArgs);
    //! Split a shell command into separate arguments. ArgIterator is usually a better choice.
    static QStringList splitArgs(const QString &cmd, OsType osType = HostOsInfo::hostOs(),
                                 bool abortOnMeta = false, SplitError *err = nullptr,
                                 const Environment *env = nullptr, const QString *pwd = nullptr);
    //! Safely replace the expandos in a shell command
    static bool expandMacros(QString *cmd, AbstractMacroExpander *mx,
                             OsType osType = HostOsInfo::hostOs());
    static QString expandMacros(const QString &str, AbstractMacroExpander *mx,
                                OsType osType = HostOsInfo::hostOs());

    /*! Iterate over arguments from a command line.
     *  Assumes that the name of the actual command is *not* part of the line.
     *  Terminates after the first command if the command line is complex.
     */
    class ArgIterator {
    public:
        ArgIterator(QString *str, OsType osType = HostOsInfo::hostOs())
            : m_str(str), m_osType(osType)
        {}
        //! Get the next argument. Returns false on encountering end of first command.
        bool next();
        //! True iff the argument is a plain string, possibly after unquoting.
        bool isSimple() const { return m_simple; }
        //! Return the string value of the current argument if it is simple, otherwise empty.
        QString value() const { return m_value; }
        //! Delete the last argument fetched via next() from the command line.
        void deleteArg();
        //! Insert argument into the command line after the last one fetched via next().
        //! This may be used before the first call to next() to insert at the front.
        void appendArg(const QString &str);
    private:
        QString *m_str, m_value;
        int m_pos = 0;
        int m_prev = -1;
        bool m_simple;
        OsType m_osType;
    };

    class ConstArgIterator {
    public:
        ConstArgIterator(const QString &str, OsType osType = HostOsInfo::hostOs())
            : m_str(str), m_ait(&m_str, osType)
        {}
        bool next() { return m_ait.next(); }
        bool isSimple() const { return m_ait.isSimple(); }
        QString value() const { return m_ait.value(); }
    private:
        QString m_str;
        ArgIterator m_ait;
    };

private:
    void setupChildProcess() override;

    CommandLine m_commandLine;
    Environment m_environment;
    bool m_haveEnv = false;
    bool m_useCtrlCStub = false;
    bool m_lowPriority = false;
};

} // namespace Utils

Q_DECLARE_METATYPE(QProcess::ExitStatus);
Q_DECLARE_METATYPE(QProcess::ProcessError);


#endif // QTCPROCESS_H
