/*
 * SPDX-FileCopyrightText: 2025 Copyright contributors to the cppTango project
 *
 * SPDX-License-Identifier: LGPL-3.0-or-later
 */
#include <tango/client/PipeEventData.h>

#include <tango/internal/client/event_helpers.h>

#include <tango/client/DevicePipe.h>
#include <tango/server/tango_clock.h>

#include <chrono>

namespace Tango
{

//+----------------------------------------------------------------------
//
//     PipeEventData constructor
//
//-----------------------------------------------------------------------

PipeEventData::PipeEventData(DeviceProxy *dev,
                             const std::string &nam,
                             const std::string &evt,
                             Tango::DevicePipe *pipe_value_in,
                             Tango::EventReason event_reason_in,
                             const DevErrorList &errors_in) :
    device(dev),
    pipe_name(nam),
    event(evt),
    pipe_value(pipe_value_in),
    event_reason(event_reason_in),
    errors(errors_in)
{
    TANGO_ASSERT(ensure_failure_reason_with_errors(event_reason, errors));

    err = errors.length() != 0;

    set_time();
}

//+----------------------------------------------------------------------
//
//     PipeEventData copy constructor
//
//-----------------------------------------------------------------------

PipeEventData::PipeEventData(const PipeEventData &sou)
{
    device = sou.device;
    pipe_name = sou.pipe_name;
    event = sou.event;
    if(sou.pipe_value != nullptr)
    {
        pipe_value = new DevicePipe(*(sou.pipe_value));
    }
    else
    {
        pipe_value = nullptr;
    }
    err = sou.err;
    errors = sou.errors;
    event_reason = sou.event_reason;
    reception_date = sou.reception_date;
}

//+----------------------------------------------------------------------
//
//     PipeEventData assignement operator
//
//-----------------------------------------------------------------------

PipeEventData &PipeEventData::operator=(const PipeEventData &ri)
{
    if(&ri == this)
    {
        return *this;
    }

    device = ri.device;
    pipe_name = ri.pipe_name;
    event = ri.event;
    if(ri.pipe_value != nullptr)
    {
        pipe_value = new DevicePipe(*(ri.pipe_value));
    }
    else
    {
        pipe_value = nullptr;
    }
    err = ri.err;
    errors = ri.errors;
    event_reason = ri.event_reason;
    reception_date = ri.reception_date;

    return *this;
}

//+----------------------------------------------------------------------
//
//     PipeEventData destructor
//
//-----------------------------------------------------------------------

PipeEventData::~PipeEventData()
{
    delete pipe_value;
}

//+-------------------------------------------------------------------------
//
// method :         PipeEventData::set_time
//
// description :     Set the event reception data
//
//--------------------------------------------------------------------------

void PipeEventData::set_time()
{
    reception_date = make_TimeVal(std::chrono::system_clock::now());
}

} // namespace Tango
