/*
 * SPDX-FileCopyrightText: 2023 Copyright contributors to the cppTango project
 *
 * SPDX-License-Identifier: LGPL-3.0-or-later
 */
// NOLINTBEGIN(*)

#include "old_common.h"

#include <tango/client/AttrConfEventDataList.h>
#include <tango/client/DataReadyEventDataList.h>
#include <tango/client/DevIntrChangeEventDataList.h>
#include <tango/client/EventDataList.h>
#include <tango/client/PipeEventDataList.h>
#include <tango/common/defines.h>

#include <stdlib.h>

#include <iostream>

auto unset_env(const std::string &var) -> int
{
#ifdef _TG_WINDOWS_
    return _putenv_s(var.c_str(), "");
#else
    return unsetenv(var.c_str());
#endif
}

auto set_env(const std::string &var, const std::string &value, bool force_update) -> int
{
#ifdef _TG_WINDOWS_
    return _putenv_s(var.c_str(), value.c_str());
#else
    return setenv(var.c_str(), value.c_str(), force_update);
#endif
}

std::string load_file(const std::string &file)
{
    std::ifstream ifs(file, std::ios::binary);

    return {std::istreambuf_iterator<char>{ifs}, {}};
}

namespace TangoTest
{

// return a instance of a class suitable for printing
template <typename T>
T construct_default()
{
    return T{};
}

template <>
Tango::DeviceAttribute construct_default()
{
    Tango::DeviceAttribute da;

    auto *errors = new Tango::DevErrorList;
    errors->length(1);
    (*errors)[0].severity = Tango::WARN;
    da.set_error_list(errors);

    return da;
}

template <>
Tango::CommandInfo construct_default()
{
    Tango::CommandInfo var;
    var.disp_level = Tango::DL_UNKNOWN;
    var.in_type = Tango::DEV_VOID;
    var.out_type = Tango::DEV_VOID;

    return var;
}

template <>
Tango::AttributeInfoEx construct_default()
{
    Tango::AttributeInfoEx var;
    var.memorized = Tango::NOT_KNOWN;
    var.disp_level = Tango::DL_UNKNOWN;
    var.writable = Tango::WT_UNKNOWN;
    var.data_format = Tango::FMT_UNKNOWN;
    var.data_type = Tango::DEV_VOID;

    return var;
}

template <>
Tango::DevIntrChangeEventData construct_default()
{
    Tango::DevIntrChangeEventData var{nullptr,
                                      "",
                                      "",
                                      (Tango::CommandInfoList *) nullptr,
                                      nullptr,
                                      false,
                                      Tango::EventReason::Update,
                                      Tango::DevErrorList{}};
    var.cmd_list.emplace_back(construct_default<Tango::CommandInfo>());
    var.att_list.emplace_back(construct_default<Tango::AttributeInfoEx>());

    return var;
}

template <>
Tango::EventData construct_default()
{
    return {nullptr, "", "", nullptr, Tango::EventReason::Update, Tango::DevErrorList{}};
}

template <>
Tango::AttrConfEventData construct_default()
{
    return {nullptr, "", "", nullptr, Tango::EventReason::Update, Tango::DevErrorList{}};
}

template <>
Tango::DataReadyEventData construct_default()
{
    return {nullptr, nullptr, "", Tango::EventReason::Update, Tango::DevErrorList{}};
}

template <>
Tango::PipeEventData construct_default()
{
    return {nullptr, "", "", nullptr, Tango::EventReason::Update, Tango::DevErrorList{}};
}

template <>
Tango::FwdEventData construct_default()
{
    return {nullptr, "", "", nullptr, Tango::EventReason::Update, Tango::DevErrorList{}};
}

template <>
Tango::FwdAttrConfEventData construct_default()
{
    return {nullptr, "", "", nullptr, Tango::EventReason::Update, Tango::DevErrorList{}};
}

template <>
Tango::TimeVal construct_default()
{
    Tango::TimeVal val;
    val.tv_sec = 1;
    val.tv_usec = 2;
    val.tv_nsec = 3;

    return val;
}

template Tango::ArchiveEventInfo construct_default();
template Tango::AttributeAlarmInfo construct_default();
template Tango::AttributeEventInfo construct_default();
template Tango::AttrConfEventDataList construct_default();
template Tango::ChangeEventInfo construct_default();
template Tango::DataReadyEventDataList construct_default();
template Tango::DevIntrChangeEventDataList construct_default();
template Tango::DeviceData construct_default();
template Tango::DeviceInfo construct_default();
template Tango::EventDataList construct_default();
template Tango::PeriodicEventInfo construct_default();
template Tango::PipeEventDataList construct_default();
template Tango::AttributeDim construct_default();
template Tango::AttributeValue_5 construct_default();
template Tango::AttributeConfig_5 construct_default();
template Tango::ArchiveEventProp construct_default();
template Tango::AttributeAlarm construct_default();
template Tango::ChangeEventProp construct_default();
template Tango::EventProperties construct_default();
template Tango::PeriodicEventProp construct_default();

} // namespace TangoTest

// NOLINTEND(*)
