/*
 * SPDX-FileCopyrightText: 2024 Copyright contributors to the cppTango project
 *
 * SPDX-License-Identifier: LGPL-3.0-or-later
 */
#include "utils/auto_device_class.h"

namespace TangoTest
{
std::vector<detail::ClassRegistrarBase *> *detail::ClassRegistrarBase::registrars = nullptr;

std::optional<std::vector<std::string>> detail::ClassRegistrarBase::enabled_classes;

EnableClasses::EnableClasses(std::vector<std::string> classes)
{
    m_prev = detail::ClassRegistrarBase::enabled_classes;
    detail::ClassRegistrarBase::enabled_classes = classes;
}

EnableClasses::~EnableClasses()
{
    detail::ClassRegistrarBase::enabled_classes = m_prev;
}

} // namespace TangoTest

namespace
{
std::vector<std::string> get_enabled_classes()
{
    std::vector<std::string> result;

    if(TangoTest::detail::ClassRegistrarBase::enabled_classes.has_value())
    {
        return *TangoTest::detail::ClassRegistrarBase::enabled_classes;
    }

    const char *maybe_env = std::getenv(TangoTest::detail::k_enabled_classes_env_var);

    if(maybe_env == nullptr)
    {
        return result;
    }

    std::string env{maybe_env};

    size_t pos = 0;
    while(true)
    {
        size_t next = env.find(';', pos);
        if(next == std::string::npos)
        {
            result.push_back(env.substr(pos));
            break;
        }

        result.push_back(env.substr(pos, next - 1));
        pos = next + 1;
    }

    return result;
}

bool contains(const std::vector<std::string> &haystack, const std::string &needle)
{
    return std::find(haystack.begin(), haystack.end(), needle) != haystack.end();
}

} // namespace

namespace TangoTest::detail
{
void DServerImpl::class_factory()
{
    std::vector<std::string> enabled = get_enabled_classes();

    for(auto *registrar : *TangoTest::detail::ClassRegistrarBase::registrars)
    {
        if(!enabled.empty() && !contains(enabled, registrar->name))
        {
            continue;
        }

        add_class(registrar->init_class());
    }
}
} // namespace TangoTest::detail
