#pragma once

#include <QObject>
#include <QJsonObject>
#include <QSettings>

class QWidget;

class DesktopBridge : public QObject {
  Q_OBJECT

public:
  explicit DesktopBridge(QWidget *window, QObject *parent = nullptr);

  void setFrontendRoot(const QString &frontendRoot);

  Q_INVOKABLE QString windowMinimize();
  Q_INVOKABLE QString windowMaximize();
  Q_INVOKABLE QString windowClose();

  Q_INVOKABLE QString openProject(const QString &path = QString());
  Q_INVOKABLE QString saveProject(const QString &payloadJson);
  Q_INVOKABLE QString savePrintPdf(const QString &payloadJson);
  Q_INVOKABLE QString pickSpreadsheet();
  Q_INVOKABLE QString readSpreadsheet(const QString &path);
  Q_INVOKABLE QString listLocalFonts() const;
  Q_INVOKABLE QString listPrinters() const;
  Q_INVOKABLE QString submitPrintJob(const QString &payloadJson);

private:
  QString readFileAsBase64(const QString &path) const;
  QString writeBase64File(const QString &path, const QString &dataBase64) const;
  QString filePayloadForPath(const QString &path) const;
  QString errorResult(const QString &message) const;
  QString successResult(const QJsonObject &payload = QJsonObject()) const;
  QString ensureFileExtension(QString path, const QString &suffix) const;

  QWidget *window_;
  QString frontendRoot_;
  mutable QSettings settings_;
};
