#pragma once

#include <QObject>
#include <QStringList>
#include <QVariantMap>
#include <QQmlEngine>

class LauncherBackend : public QObject
{
    Q_OBJECT
    QML_ELEMENT

    Q_PROPERTY(QStringList profileNames READ profileNames NOTIFY profilesChanged)
    Q_PROPERTY(QStringList versionNames READ versionNames NOTIFY versionsChanged)

public:
    explicit LauncherBackend(QObject *parent = nullptr);

    QStringList profileNames() const;
    QStringList versionNames() const;

    Q_INVOKABLE void addProfile(const QString &name, const QString &login, const QString &password);
    Q_INVOKABLE void addVersion(const QString &name, const QString &serverUrl);
    Q_INVOKABLE void updateProfile(int index, const QString &name, const QString &login, const QString &password);
    Q_INVOKABLE void updateVersion(int index, const QString &name, const QString &serverUrl);
    Q_INVOKABLE QVariantMap profileAt(int index) const;
    Q_INVOKABLE QVariantMap versionAt(int index) const;
    Q_INVOKABLE void removeProfile(int index);
    Q_INVOKABLE void removeVersion(int index);
    Q_INVOKABLE void launchGame(int profileIndex, int versionIndex);
    Q_INVOKABLE void openMinecraftFolder();

signals:
    void profilesChanged();
    void versionsChanged();
    void launched(const QString &profileName, const QString &versionName, const QString &serverUrl);
    void launchError(const QString &message);

private:
    struct Profile { int id; QString name, login, password; };
    struct Version { int id; QString name, serverUrl; };

    void loadData();
    void saveProfiles();
    void saveVersions();
    QString minecraftDir() const;

    QList<Profile> m_profiles;
    QList<Version> m_versions;
    QString m_dataDir;
    int m_nextProfileId = 1;
    int m_nextVersionId = 1;
};
