#pragma once

#include "lib/settings.hpp"
#include "lib/logmessage.hpp"

#include <QFileInfo>
#include <QInputDialog>
#include <QList>
#include <QProcess>

namespace SpotifyClient
{
	/**
	 * Manages any running spotify client instance
	 */
	class Runner: public QObject
	{
	Q_OBJECT

	public:
		Runner(const lib::settings &settings, const lib::paths &paths, QWidget *parent);
		~Runner() override;

		void start();

		static auto getLog() -> const QList<LogMessage> &;
		auto isRunning() const -> bool;

	signals:
		/**
		 * Client status changed, usually after attempted start
		 * @param status Error message, or empty string on success
		 */
		void statusChanged(const QString &status);

	private:
		QProcess *process;
		QWidget *parentWidget = nullptr;
		QString path;
		static QList<LogMessage> log;
		const lib::settings &settings;
		const lib::paths &paths;

		void logOutput(const QByteArray &output, QtMsgType logType);
		static auto joinArgs(const QStringList &args) -> QString;

		auto getCachePath() const -> std::filesystem::path;
		auto isLoggedIn() const -> bool;
		auto resetCredentials() const -> bool;

		void onReadyReadOutput();
		void onReadyReadError();
		void onStarted();
		void onErrorOccurred(QProcess::ProcessError error);
	};
}
