/**
 * Copyright © 2018 IBM Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <phosphor-logging/log.hpp>
#include "dbus.hpp"

namespace ibm
{
namespace logging
{

constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper";
constexpr auto MAPPER_IFACE = "xyz.openbmc_project.ObjectMapper";
constexpr auto PROPERTY_IFACE = "org.freedesktop.DBus.Properties";

using namespace phosphor::logging;

ObjectValueTree getManagedObjects(sdbusplus::bus::bus& bus,
                                  const std::string& service,
                                  const std::string& objPath)
{
    ObjectValueTree interfaces;

    auto method = bus.new_method_call(service.c_str(), objPath.c_str(),
                                      "org.freedesktop.DBus.ObjectManager",
                                      "GetManagedObjects");

    auto reply = bus.call(method);

    if (reply.is_method_error())
    {
        log<level::ERR>("Failed to get managed objects",
                        entry("SERVICE=%s", service.c_str()),
                        entry("PATH=%s", objPath.c_str()));
    }
    else
    {
        reply.read(interfaces);
    }

    return interfaces;
}

DbusPropertyMap getAllProperties(sdbusplus::bus::bus& bus,
                                 const std::string& service,
                                 const std::string& objPath,
                                 const std::string& interface)
{
    DbusPropertyMap properties;

    auto method = bus.new_method_call(service.c_str(), objPath.c_str(),
                                      PROPERTY_IFACE, "GetAll");
    method.append(interface);
    auto reply = bus.call(method);

    if (reply.is_method_error())
    {
        log<level::ERR>("Failed to get all properties",
                        entry("SERVICE=%s", service.c_str()),
                        entry("PATH=%s", objPath.c_str()),
                        entry("INTERFACE=%s", interface.c_str()));
    }
    else
    {
        reply.read(properties);
    }

    return properties;
}

DbusSubtree getSubtree(sdbusplus::bus::bus& bus, const std::string& root,
                       size_t depth, const std::string& interface)
{
    DbusSubtree tree;

    auto method = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH, MAPPER_IFACE,
                                      "GetSubTree");
    method.append(root);
    method.append(depth);
    method.append(std::vector<std::string>({interface}));
    auto reply = bus.call(method);

    if (reply.is_method_error())
    {
        log<level::ERR>("Failed to get subtree", entry("ROOT=%s", root.c_str()),
                        entry("INTERFACE=%s", interface.c_str()));
    }
    else
    {
        reply.read(tree);
    }

    return tree;
}

DbusService getService(const std::string& objPath, const std::string& interface,
                       const DbusSubtree& tree)
{
    DbusService service;

    auto services = tree.find(objPath);
    if (services != tree.end())
    {
        auto s = std::find_if(services->second.begin(), services->second.end(),
                              [&interface](const auto& entry) {
                                  auto i =
                                      std::find(entry.second.begin(),
                                                entry.second.end(), interface);
                                  return i != entry.second.end();
                              });
        if (s != services->second.end())
        {
            service = s->first;
        }
    }

    return service;
}
}
}
