/*
    Copyright 2016-2019 Gleb Popov <6yearold@gmail.com>
    Copyright (c) 2023 Serenity Cyber Security, LLC <license@futurecrew.ru>
                Author: Alexey Yushkin <x636808@gmail.com>

    Redistribution and use in source and binary forms, with or without modification,
    are permitted provided that the following conditions are met:
    1. Redistributions of source code must retain the above copyright notice,
    this list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice,
    this list of conditions and the following disclaimer in the documentation
    and/or other materials provided with the distribution.
    3. Neither the name of the copyright holder nor the names of its contributors may
    be used to endorse or promote products derived from this software without specific
    prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
    ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
    IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
    INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
    OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
    ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#pragma once

#include <QHash>

#include "AuthQueue.h"
#include "manageradaptor.h"
#include "geomprober.h"

#define DBUS_THROW_CONTEXT_IMPL(id, descr, context) { \
    context->setDelayedReply(true); \
    context->connection().send(context->message().createErrorReply(id, descr)); \
    return {}; \
    }

#define DBUS_THROW_CONTEXT_VOID(id, descr) { \
    context->setDelayedReply(true); \
    context->connection().send(context->message().createErrorReply(id, descr)); \
    return; \
    }

#define DBUS_RETHROW_CONTEXT(reply) { \
    context->setDelayedReply(true); \
    context->connection().send(context->message().createErrorReply(reply.error())); \
    return {}; \
    }

#define DBUS_RETHROW_CONTEXT_VOID(reply) { \
    context->setDelayedReply(true); \
    context->connection().send(context->message().createErrorReply(reply.error())); \
    return; \
    }

class Block;
class Drive;
class ZFSInfo;

class ObjectManager : public QObject
{
    Q_OBJECT

public:
    ObjectManager();
    static ObjectManager& Instance();

    void CheckAuth(const QString& actionId, QObject* subject, AuthQueue::Continuation continuation);
    bool BlockObjectValid(Block* block) const;
    bool DriveObjectValid(Drive* drive) const;

    void filesystemAdded(Block* b, QString fs, const ZFSInfo* zfsInfo = nullptr);

    void addBlock(QString dev);
    void updateBlock(QString dev);
    void removeBlock(QString dev);

    void addDrive(QString dev);
    void removeDrive(QString dev);

    void addZFSDataset(const ZFSInfo& zfsInfo);

    void mountFS(QString mountDevice, QString mountPoint);
    void unmountFS(QString mountPoint);

    void initialProbe();
    void startFilesystemProbe(Block* b);

public slots:
    DBUSManagerStruct GetManagedObjects();

signals:
    void InterfacesAdded(const QDBusObjectPath &object_path, const QVariantMapMap &interfaces_and_properties);
    void InterfacesRemoved(const QDBusObjectPath &object_path, const QStringList &interfaces);

private:
    void startGeomProbe(Block* b);

    void registerDrive(Drive* d);
    bool registerBlock(Block* d, bool tryPostponed = true);
    void postponeRegistration(QString blockName);
    void tryRegisterPostponed();

    void addPartition(Block* b, const QString& tableBlockName, const Part& partInfo);

    void addInterfaces(QDBusObjectPath path, QList<std::pair<QString, QDBusAbstractAdaptor*>> newInterfaces);
    void removeInterfaces(QDBusObjectPath path, QStringList ifaces);

    bool initialProbeDone;
    QHash<QString, Block*> m_blockObjects;
    QHash<QString, Drive*> m_driveObjects;
    QSet<QString> m_postponedRegistrations;
    AuthQueue m_authQueue;
};
