// SPDX-FileCopyrightText: 2024 Open Mobile Platform LLC <community@omp.ru>
// SPDX-License-Identifier: BSD-3-Clause

#ifndef CALLMANAGER_H
#define CALLMANAGER_H

#include <QObject>
#include <QTimer>
#include <QtDBus/QDBusObjectPath>
#include <QtDBus/QDBusContext>

#include "dbustypes.h"
#include "callservice1dbusinterface.h"
#include "call1dbusobject.h"
#include "earpieceplayer.h"

class CallManagerDBusAdaptor;

/*!
 * \brief CallManager class is call manager that implements the org.freedesktop.DBus.ObjectManager
 * interface: https://developer.auroraos.ru/doc/5.0.0/software_development/reference/call_api.
 */
class CallManager : public QObject, public QDBusContext
{
    Q_OBJECT

public:
    explicit CallManager(QObject *parent = nullptr);
    ~CallManager();

    void registerCallManager();
    void registerCall1DBusObject(const bool incoming);

    void startIncomingCall();
    void startOutgoingCall();

public slots:
    // org.freedesktop.DBus.ObjectManager method
    DBusManagerStruct GetManagedObjects();

private:
    void registerDBusObject();
    void unregisterDBusObject();

private slots:
    void sendCall1DialingStatus();
    void sendCall1RingingStatus();
    void playCallAudioFile();
    void answerOutgoingCall();
    void handleCallStatusChanged(const uint32_t status);
    void handleAudioResourceGrantedChanged();

private:
    QTimer m_answerTimer;
    CallManagerDBusAdaptor *m_dbusAdaptor;
    Call1DBusObject m_call1DBusObject;
    EarpiecePlayer m_earpiecePlayer;
    DBusManagerStruct m_dbusManagedObjects;
    Call1DBusObject::CallStatus m_currentCallStatus;

signals:
    void callManagerRegistered();
    void dbusErrorReceived(const QString &errorMessage);

signals:
    // org.freedesktop.DBus.ObjectManager signals
    void InterfacesAdded(const QDBusObjectPath &object, const VariantMapMap &interfaces);
    void InterfacesRemoved(const QDBusObjectPath &object, const QStringList &interfaces);
};

#endif // CALLMANAGER_H
