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

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

#include <tango/server/tango_clock.h>

#include <chrono>

namespace Tango
{

//+----------------------------------------------------------------------
//
//  DataReadyEventData constructor
//
//-----------------------------------------------------------------------

DataReadyEventData::DataReadyEventData(DeviceProxy *dev,
                                       AttDataReady *dr,
                                       const std::string &evt,
                                       Tango::EventReason event_reason_in,
                                       const DevErrorList &errors_in) :
    event(evt),
    event_reason(event_reason_in),
    errors(errors_in)
{
    TANGO_ASSERT(ensure_failure_reason_with_errors(event_reason, errors));

    device = dev;
    if(dr != nullptr)
    {
        attr_name = dr->name.in();
        attr_data_type = dr->data_type;
        ctr = dr->ctr;
    }
    else
    {
        attr_name = "Unknown";
        attr_data_type = -1;
        ctr = -1;
    }

    err = errors.length() != 0;

    set_time();
}

//+----------------------------------------------------------------------
//
//  DataReadyEventData copy constructor
//
//-----------------------------------------------------------------------

DataReadyEventData::DataReadyEventData(const DataReadyEventData &sou)
{
    device = sou.device;
    ctr = sou.ctr;
    attr_name = sou.attr_name;
    event = sou.event;
    attr_data_type = sou.attr_data_type;
    reception_date = sou.reception_date;
    err = sou.err;
    errors = sou.errors;
    event_reason = sou.event_reason;
}

//+----------------------------------------------------------------------
//
//     DataReadyEventData assignement operator
//
//-----------------------------------------------------------------------

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

    device = ri.device;
    ctr = ri.ctr;
    attr_data_type = ri.attr_data_type;
    attr_name = ri.attr_name;
    event = ri.event;

    reception_date = ri.reception_date;

    err = ri.err;
    errors = ri.errors;
    event_reason = ri.event_reason;

    return *this;
}

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

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

} // namespace Tango
