/****************************************************************************
 * This file is part of the project AqHome.
 * AqHome (c) by 2023 Martin Preuss, all rights reserved.
 *
 * The license for this file can be found in the file COPYING which you
 * should have received along with this file.
 ****************************************************************************/

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <aqhome/ipc/data/msg_data_multidata.h>
#include <aqhome/ipc/data/ipc_data.h>
#include <aqhome/ipc/msg_ipc_tag16.h>

#include <gwenhywfar/msg.h>
#include <gwenhywfar/buffer.h>

#include <gwenhywfar/debug.h>
#include <gwenhywfar/msg_ipc.h>



#define AQH_MSGDATA_MULTIDATA_MINSIZE GWEN_MSGIPC_OFFS_PAYLOAD




GWEN_MSG *AQH_MultiDataDataIpcMsg_new(uint16_t code, const AQH_VALUE *value, const uint64_t *i64Ptr, int numOfDataPoints)
{
  GWEN_MSG *msg;
  GWEN_BUFFER *buf;
  int rv;

  buf=GWEN_Buffer_new(0, 256, 0, 1);

  rv=AQH_DataIpc_WriteValueAsTagToBuffer(AQH_MSGDATA_MULTIDATA_TAGS_VALUE, value, buf);
  if (rv<0) {
    DBG_INFO(AQH_LOGDOMAIN, "here (%d)", rv);
    GWEN_Buffer_free(buf);
    return NULL;
  }

  if (i64Ptr && numOfDataPoints)
    GWEN_Tag16_WriteTagToBuffer(AQH_MSGDATA_MULTIDATA_TAGS_DATA, (const uint8_t*)i64Ptr, numOfDataPoints*2*sizeof(uint64_t), buf);

  msg=AQH_Tag16IpcMsg_new(AQH_IPC_PROTOCOL_DATA_ID, AQH_IPC_PROTOCOL_DATA_VERSION, code,
			  GWEN_Buffer_GetUsedBytes(buf), (const uint8_t*) GWEN_Buffer_GetStart(buf));
  GWEN_Buffer_free(buf);
  return msg;
}



GWEN_MSG *AQH_MultiDataDataIpcMsg_newForOne(uint16_t code, const AQH_VALUE *value, uint64_t timeStamp, double dataPoint)
{
  GWEN_MSG *msg;
  GWEN_BUFFER *buf;
  int rv;
  union {double f; uint64_t i;} u;
  uint64_t arrayToSend[2];

  buf=GWEN_Buffer_new(0, 256, 0, 1);

  rv=AQH_DataIpc_WriteValueAsTagToBuffer(AQH_MSGDATA_MULTIDATA_TAGS_VALUE, value, buf);
  if (rv<0) {
    DBG_INFO(AQH_LOGDOMAIN, "here (%d)", rv);
    GWEN_Buffer_free(buf);
    return NULL;
  }

  arrayToSend[0]=timeStamp;
  u.f=dataPoint;
  arrayToSend[1]=u.i;
  GWEN_Tag16_WriteTagToBuffer(AQH_MSGDATA_MULTIDATA_TAGS_DATA, (const uint8_t*) arrayToSend, 2*sizeof(uint64_t), buf);

  msg=AQH_Tag16IpcMsg_new(AQH_IPC_PROTOCOL_DATA_ID, AQH_IPC_PROTOCOL_DATA_VERSION, code,
			  GWEN_Buffer_GetUsedBytes(buf), (const uint8_t*) GWEN_Buffer_GetStart(buf));
  GWEN_Buffer_free(buf);
  return msg;
}



void AQH_MultiDataDataIpcMsg_Parse(GWEN_MSG *msg, int doCopy)
{
  AQH_Tag16IpcMsg_ExtendAndParse(msg, doCopy);
}



AQH_VALUE *AQH_MultiDataDataIpcMsg_ReadValue(const GWEN_MSG *msg)
{
  return AQH_DataIpc_ReadValueFromTagList(AQH_Tag16IpcMsg_GetTags(msg), AQH_MSGDATA_MULTIDATA_TAGS_VALUE);
}



void AQH_ValuesDataIpcMsg_GetDatapoints(const GWEN_MSG *msg, const uint64_t **pDataPtr, uint64_t *pNumberOfPoints)
{
  if (msg) {
    const GWEN_TAG16 *tag;
    unsigned int numberOfPoints;
    const uint64_t *dataPoints;

    tag=AQH_Tag16IpcMsg_FindFirstTagByType(msg, AQH_MSGDATA_MULTIDATA_TAGS_DATA);
    numberOfPoints=(tag?GWEN_Tag16_GetTagLength(tag):0)/(2*sizeof(uint64_t));
    dataPoints=tag?((const uint64_t*) GWEN_Tag16_GetTagData(tag)):NULL;
    if (numberOfPoints>0 && dataPoints) {
      *pDataPtr=dataPoints;
      *pNumberOfPoints=numberOfPoints;
    }
  }
}



void AQH_MultiDataDataIpcMsg_DumpToBuffer(const GWEN_MSG *msg, GWEN_BUFFER *dbuf, const char *sText)
{
  if (GWEN_Msg_GetBytesInBuffer(msg)>=AQH_MSGDATA_MULTIDATA_MINSIZE) {
    const GWEN_TAG16 *tag;
    AQH_VALUE *value;
    const char *valueName;
    const char *valueUnits;
    int valueType;
    unsigned int numberOfPoints=0;

    value=AQH_MultiDataDataIpcMsg_ReadValue(msg);
    valueName=value?AQH_Value_GetNameForSystem(value):NULL;
    valueUnits=value?AQH_Value_GetValueUnits(value):NULL;
    valueType=value?AQH_Value_GetValueType(value):0;

    tag=AQH_Tag16IpcMsg_FindFirstTagByType(msg, AQH_MSGDATA_MULTIDATA_TAGS_DATA);
    numberOfPoints=(tag?GWEN_Tag16_GetTagLength(tag):0)/(2*sizeof(uint64_t));

    GWEN_Buffer_AppendArgs(dbuf,
                           "MULTIDATA (code=%d, proto=%d, proto version=%d, name=%s, units=%s, type=%d, datapoints=%u)\n",
                           GWEN_IpcMsg_GetCode(msg),
                           GWEN_IpcMsg_GetProtoId(msg),
                           GWEN_IpcMsg_GetProtoVersion(msg),
			   valueName?valueName:"<empty>",
			   valueUnits?valueUnits:"<empty>",
                           valueType,
                           numberOfPoints);
    AQH_Value_free(value);
  }
}







