// Copyright (C) 2025 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#ifndef QOHOSABILITYCONTEXT_H
#define QOHOSABILITYCONTEXT_H

#include <QtCore/qbytearray.h>
#include <QtCore/qlist.h>
#include <QtCore/qsharedpointer.h>
#include <QtCore/qstring.h>
#include <QtCore/qjsonobject.h>
#include <QtOhosExtras/qohosapppermissions.h>
#include <QtOhosExtras/qohosoperationstatus.h>
#include <QtOhosExtras/qohoswant.h>
#include <QtOhosExtras/qohosstartoptions.h>
#include <QtOhosExtras/qtohosextrasglobal.h>
#include <QtOhosExtras/qohossharekit.h>
#include <QtGui/QWindow>
#include <QtWidgets/QWidget>

QT_BEGIN_NAMESPACE

namespace QtOhosExtras
{

class Q_OHOS_EXTRAS_EXPORT QOhosOpenLinkOptions
{
public:
    virtual ~QOhosOpenLinkOptions();

    virtual void setAppLinkingOnly(bool appLinkingOnly) = 0;

protected:
    QOhosOpenLinkOptions();

private:
    Q_DISABLE_COPY(QOhosOpenLinkOptions)
};

Q_OHOS_EXTRAS_EXPORT QSharedPointer<QOhosOpenLinkOptions> createOpenLinkOptions();

class Q_OHOS_EXTRAS_EXPORT QOhosOnContinueContext
{
public:
    virtual ~QOhosOnContinueContext();

    virtual void setAgreeResponse(const QByteArray &responseData) = 0;
    virtual void setRejectResponse() = 0;
    virtual void setMismatchResponse() = 0;

    virtual void setExitAppOnSourceDeviceAfterMigration(bool exitAfterMigration) = 0;

    virtual int sourceApplicationVersionCode() const = 0;

protected:
    QOhosOnContinueContext();

private:
    Q_DISABLE_COPY(QOhosOnContinueContext)
};

class Q_OHOS_EXTRAS_EXPORT QOhosAbilityContext : public QObject
{
    Q_OBJECT

public:
    static QOhosAbilityContext *instance();

    static QSharedPointer<QOhosAbilityContext> getDefaultInstance();
    static QSharedPointer<QOhosAbilityContext> getInstanceForMainWindow(QWindow *instanceMainWindow);

    virtual void setDestroyFromSystemEnabled(bool destroyEnabled) = 0;

    virtual void requestPermissionFromUserIfNeeded(AppPermissions::Permission permission) = 0;
    virtual void requestPermissionFromUserIfNeeded(const QString &permissionName) = 0;
    virtual void requestPermissionOnSettingIfNeeded(AppPermissions::Permission permission) = 0;
    virtual void requestPermissionOnSettingIfNeeded(const QString &permissionName) = 0;

    virtual QByteArray startAbilityForResult(const QOhosWant &want) = 0;
    virtual QByteArray startAbilityForResult(const QOhosWant &want, const QOhosStartOptions &options) = 0;

    virtual QByteArray shareDataWithShareKit(
        const QList<QSharedPointer<ShareKit::QOhosSharedRecord>> &records,
        QSharedPointer<ShareKit::QOhosShareControllerOptions> controllerOptions = nullptr) = 0;

    virtual bool tryOpenLink(const QString &link) = 0;
    virtual bool tryOpenLink(const QString &link, const QOhosOpenLinkOptions &options) = 0;

    virtual void setContinuationActive(bool continuationActive) = 0;

Q_SIGNALS:
    void newWantReceived(QOhosWant want);
    void newWantInfoReceived(QSharedPointer<QOhosWantInfo> wantInfo);
    void continueRequestReceived(QSharedPointer<QOhosOnContinueContext> onContinueContext);

    void permissionRequestResponseReceived(
        AppPermissions::Permission permission, AppPermissions::PermissionResult result);
    void namedPermissionRequestResponseReceived(
        QString permissionName, AppPermissions::PermissionResult result);
    void permissionRequestOnSettingResponseReceived(
        AppPermissions::Permission permission, bool permissionGranted);
    void namedPermissionRequestOnSettingResponseReceived(
        QString permissionName, bool permissionGranted);

    void startAbilityForResultResponseReceived(QByteArray requestId, int resultCode, QSharedPointer<QOhosWant> optWant);
    void startAbilityForResultErrorResponseReceived(QByteArray requestId);

    void shareKitPanelClosed(QByteArray requestId);
    void shareKitCompleted(
        QByteArray requestId, QSharedPointer<ShareKit::QOhosShareOperationResult> shareOperationResult);

protected:
    QOhosAbilityContext();

    Q_DISABLE_COPY(QOhosAbilityContext)
};

Q_OHOS_EXTRAS_EXPORT QSharedPointer<QOhosOperationStatus> startAbility(const QOhosWant &want);
Q_OHOS_EXTRAS_EXPORT QSharedPointer<QOhosOperationStatus> startAbility(const QOhosWant &want, const QOhosStartOptions &options);

Q_OHOS_EXTRAS_EXPORT QSharedPointer<QOhosOperationStatus> startAbilityByType(const QString &appType, const QJsonObject &wantParameters);

Q_OHOS_EXTRAS_EXPORT void startNewAbilityInstance(QWidget *instanceWidget);

Q_OHOS_EXTRAS_EXPORT void startAppProcess(const QString &processId, const QOhosWant &requestWant);
Q_OHOS_EXTRAS_EXPORT void startAppProcess(const QString &processId, const QOhosWant &requestWant, const QOhosStartOptions &options);

Q_OHOS_EXTRAS_EXPORT void setAbilityInstanceDestroyEnabled(QWindow *instanceWindow, bool destroyEnabled);

Q_OHOS_EXTRAS_EXPORT QSharedPointer<QByteArray> tryGetOnContinueData(const QOhosWant &want);

}

QT_END_NAMESPACE

#endif // QOHOSABILITYCONTEXT_H
