/*
 * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
 * SPDX-FileCopyrightText: 2014 ownCloud GmbH
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#ifndef APPLICATION_H
#define APPLICATION_H

#include "owncloudgui.h"
#include "progressdispatcher.h"
#include "clientproxy.h"
#include "folderman.h"

#ifdef Q_OS_MACOS
#include "macOS/singleinstancemanager_mac.h"
#else
#include <KDSingleApplication>
#endif

#if defined KF6DBusAddons_FOUND && KF6DBusAddons_FOUND
#include <KDBusService>
#endif

#include <QApplication>
#include <QPointer>
#include <QQueue>
#include <QSet>
#include <QTimer>
#include <QElapsedTimer>
#include <QNetworkInformation>

class QMessageBox;
class QSystemTrayIcon;
class QSocket;

namespace OCC {

Q_DECLARE_LOGGING_CATEGORY(lcApplication)

class Theme;
class Folder;
class ShellExtensionsServer;
class SslErrorDialog;

#if defined(Q_OS_MACOS)
namespace Mac {
class FinderSyncXPC;
class FinderSyncService;
}
#endif

/**
 * @brief The Application class
 * @ingroup gui
 */
class Application : public QApplication
{
    Q_OBJECT
public:
    explicit Application(int &argc, char **argv);
    ~Application() override;

    bool giveHelp();
    void showHelp();
    void showHint(std::string errorHint);
    bool debugMode();
    [[nodiscard]] bool backgroundMode() const;
    bool versionOnly(); // only display the version?
    void showVersion();

    [[nodiscard]] bool isRunning() const;

    bool sendMessage(const QString &message);

    void showMainDialog();
    bool handleUriSchemeRequest(const QUrl &url);

    [[nodiscard]] ownCloudGui *gui() const;

#if defined(Q_OS_MACOS)
    [[nodiscard]] Mac::FinderSyncXPC *finderSyncXPC() const;
#endif

    bool event(QEvent *event) override;

public slots:
    // TODO: this should not be public
    void slotownCloudWizardDone(int);
    void slotCrash();
    /**
     * Will download a virtual file, and open the result.
     * The argument is the filename of the virtual file (including the extension)
     */
    void openVirtualFile(const QString &filename);

    /// Attempt to show() the tray icon again. Used if no systray was available initially.
    void tryTrayAgain();

protected:
    void parseOptions(const QStringList &);
    void setupTranslations();
    void setupLogging();

signals:
    void folderRemoved();
    void folderStateChanged(OCC::Folder *);
    void isShowingSettingsDialog();
    void systemPaletteChanged();

protected slots:
    void slotParseMessage(const QByteArray &msg);
    void slotActivateRequestedMessage(const QStringList &arguments, const QString &workingDirectory);
    void slotCheckConnection();
    void slotCleanup();
    void slotAccountStateAdded(OCC::AccountState *accountState);
    void slotAccountStateRemoved(OCC::AccountState *accountState);
    void slotSystemOnlineConfigurationChanged();
    void slotGuiIsShowingSettings();

private:
    void setHelp();

    void handleUriFromOptions();

    AccountManager::AccountsRestoreResult restoreLegacyAccount();
    void setupConfigFile();
    void setupAccountsAndFolders();

    void showMainDialogRemoteCommand();
    void parseOptionsRemoteCommand(const QStringList &options);

    /**
     * Maybe a newer version of the client was used with this config file:
     * if so, backup, confirm with user and remove the config that can't be read.
     */
    bool configVersionMigration();

    QPointer<ownCloudGui> _gui;

#ifdef Q_OS_MACOS
    OCC::SingleInstanceManager _singleApp;
#elif defined KF6DBusAddons_FOUND && KF6DBusAddons_FOUND
    KDBusService _dbusService;
#else
    KDSingleApplication _singleApp;
#endif

    Theme *_theme;

    bool _helpOnly = false;
    bool _versionOnly = false;

    QElapsedTimer _startedAt;

    // options from command line:
    bool _showLogWindow = false;
    bool _quitInstance = false;
    QString _logFile;
    QString _logDir;
    int _logExpire = 0;
    bool _logFlush = false;
    bool _logDebug = false;
    bool _userTriggeredConnect = false;
    bool _debugMode = false;
    bool _backgroundMode = false;
    bool _suppressNextEmptyAccountCheck = false;
    QUrl _uriSchemeUrl;

    ClientProxy _proxy;

    QTimer _checkConnectionTimer;

    QString _overrideServerUrl;
    QString _overrideLocalDir;
    QString _setLanguage;

    QScopedPointer<FolderMan> _folderManager;
#if defined(Q_OS_WIN)
    QScopedPointer<ShellExtensionsServer> _shellExtensionsServer;
#endif
#if defined(Q_OS_MACOS)
    std::unique_ptr<Mac::FinderSyncXPC> _finderSyncXPC;
    std::unique_ptr<Mac::FinderSyncService> _finderSyncService;
    QSet<QString> _registeredFinderSyncPaths;
#endif
};

} // namespace OCC

#endif // APPLICATION_H
