
#include "getConfig.hpp"

#include <phosphor-logging/lg2.hpp>
#include <sdbusplus/asio/connection.hpp>

#include <cstdlib>
#include <memory>
#include <string>
#include <utility>
#include <variant>
#include <vector>

namespace estoraged
{

namespace mapper
{
constexpr const char* busName = "xyz.openbmc_project.ObjectMapper";
constexpr const char* path = "/xyz/openbmc_project/object_mapper";
constexpr const char* interface = "xyz.openbmc_project.ObjectMapper";
constexpr const char* subtree = "GetSubTree";
} // namespace mapper

using GetSubTreeType = std::vector<
    std::pair<std::string,
              std::vector<std::pair<std::string, std::vector<std::string>>>>>;

void GetStorageConfiguration::getStorageInfo(const std::string& path,
                                             const std::string& owner)
{
    std::shared_ptr<GetStorageConfiguration> self = shared_from_this();
    self->dbusConnection->async_method_call(
        [self, path, owner](
            const boost::system::error_code ec,
            boost::container::flat_map<std::string, BasicVariantType>& data) {
            if (ec)
            {
                lg2::error("Error getting properties for {PATH}", "PATH", path,
                           "REDFISH_MESSAGE_ID",
                           std::string("OpenBMC.0.1.GetStorageInfoFail"));
                return;
            }

            self->respData[path] = std::move(data);
        },
        owner, path, "org.freedesktop.DBus.Properties", "GetAll",
        emmcConfigInterface);
}

void GetStorageConfiguration::getConfiguration()
{
    std::shared_ptr<GetStorageConfiguration> self = shared_from_this();
    dbusConnection->async_method_call(
        [self](const boost::system::error_code ec, const GetSubTreeType& ret) {
            if (ec)
            {
                lg2::error("Error calling mapper");
                return;
            }
            for (const auto& [objPath, objDict] : ret)
            {
                if (objDict.empty())
                {
                    return;
                }
                const std::string& objOwner = objDict.begin()->first;
                /* Look for the config interface exposed by this object. */
                for (const std::string& interface : objDict.begin()->second)
                {
                    if (interface.compare(emmcConfigInterface) == 0)
                    {
                        /* Get the properties exposed by this interface. */
                        self->getStorageInfo(objPath, objOwner);
                    }
                }
            }
        },
        mapper::busName, mapper::path, mapper::interface, mapper::subtree, "/",
        0, std::vector<const char*>(1, emmcConfigInterface));
}

GetStorageConfiguration::~GetStorageConfiguration()
{
    callback(respData);
}

} // namespace estoraged
