/*
 * Copyright (C) 2020 CESNET, https://photonics.cesnet.cz/
 *
 * Written by Tomáš Pecka <tomas.pecka@fit.cvut.cz>
 *
*/
#include "DbusSemaphoreInput.h"
#include "utils/log.h"

namespace {

velia::health::State stateFromString(const std::string& str)
{
    if (str == "WARNING")
        return velia::health::State::WARNING;
    if (str == "ERROR")
        return velia::health::State::ERROR;
    if (str == "OK")
        return velia::health::State::OK;

    throw std::invalid_argument("DbusSemaphoreInput received invalid state");
}
}

namespace velia::health {

DbusSemaphoreInput::DbusSemaphoreInput(std::shared_ptr<AbstractManager> manager, sdbus::IConnection& connection, const std::string& bus, const std::string& objectPath, const std::string& propertyName, const std::string& propertyInterface)
    : AbstractInput(std::move(manager))
    , m_dbusObjectProxy(sdbus::createProxy(connection, bus, objectPath))
    , m_propertyName(propertyName)
    , m_propertyInterface(propertyInterface)
    , m_log(spdlog::get("health"))
{
    m_dbusObjectProxy->uponSignal("PropertiesChanged").onInterface("org.freedesktop.DBus.Properties").call([&](const std::string& iface, const std::map<std::string, sdbus::Variant>& changed, [[maybe_unused]] const std::vector<std::string>& invalidated) {
        if (iface != m_propertyInterface) {
            return;
        }

        if (auto it = changed.find(m_propertyName); it != changed.end()) {
            std::string newState = it->second.get<std::string>();
            m_log->debug("Changed {} {}: {}", objectPath, propertyName, newState);
            updateState(stateFromString(newState));
        }
    });
    m_dbusObjectProxy->finishRegistration();
    m_log->debug("Watching for property changes of {}, object {}, property {}.{}", bus, objectPath, propertyInterface, propertyName);

    // we might update the state twice here (once from the callback, once from here).
    // But better than querying the current state before the registration; we might miss a state change that could happen between querying and callback registration
    std::string currentState = m_dbusObjectProxy->getProperty(m_propertyName).onInterface(propertyInterface).get<std::string>();
    m_log->debug("Property initialized to {}", currentState);
    updateState(stateFromString(currentState));
}

DbusSemaphoreInput::~DbusSemaphoreInput() = default;

}
