/****************************************************************************
 * 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/ipc_data.h>
#include <aqhome/ipc/msg_ipc_tag16.h>

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


/* ------------------------------------------------------------------------------------------------
 * defines
 * ------------------------------------------------------------------------------------------------
 */

#define AQH_IPCDATA_VALUE_TAGS_ID              0x01
#define AQH_IPCDATA_VALUE_TAGS_DRIVER          0x02
#define AQH_IPCDATA_VALUE_TAGS_NAMEFORDRIVER   0x03
#define AQH_IPCDATA_VALUE_TAGS_NAMEFORSYSTEM   0x04
#define AQH_IPCDATA_VALUE_TAGS_TYPE            0x05
#define AQH_IPCDATA_VALUE_TAGS_UNITS           0x06
#define AQH_IPCDATA_VALUE_TAGS_TIMEOFCREATION  0x07
#define AQH_IPCDATA_VALUE_TAGS_DEVFORDRIVER    0x08
#define AQH_IPCDATA_VALUE_TAGS_DEVFORSYSTEM    0x09

#define AQH_IPCDATA_DEVICE_TAGS_ID             0x01
#define AQH_IPCDATA_DEVICE_TAGS_DRIVER         0x02
#define AQH_IPCDATA_DEVICE_TAGS_ROOMNAME       0x03
#define AQH_IPCDATA_DEVICE_TAGS_NAMEFORDRIVER  0x04
#define AQH_IPCDATA_DEVICE_TAGS_NAMEFORSYSTEM  0x05
#define AQH_IPCDATA_DEVICE_TAGS_NAMEFORGUI     0x06
#define AQH_IPCDATA_DEVICE_TAGS_LOCATION       0x07
#define AQH_IPCDATA_DEVICE_TAGS_DESCRIPTION    0x08
#define AQH_IPCDATA_DEVICE_TAGS_DEVTYPE        0x09
#define AQH_IPCDATA_DEVICE_TAGS_MANUFACTURER   0x0a
#define AQH_IPCDATA_DEVICE_TAGS_TIMEOFCREATION 0x0b




/* ------------------------------------------------------------------------------------------------
 * forward declarations
 * ------------------------------------------------------------------------------------------------
 */

static void _writeValueFieldsAsTagsToBuffer(const AQH_VALUE *value, GWEN_BUFFER *buf);
static AQH_VALUE *_readValueFromTag(const uint8_t *ptr, uint32_t len);
static void _writeDeviceFieldsAsTagsToBuffer(const AQH_DEVICE *device, GWEN_BUFFER *buf);
static AQH_DEVICE *_readDeviceFromTag(const uint8_t *ptr, uint32_t len);



/* ------------------------------------------------------------------------------------------------
 * code
 * ------------------------------------------------------------------------------------------------
 */



int AQH_DataIpc_WriteValueListAsTagsToBuffer(unsigned int tagType, const AQH_VALUE_LIST *valueList, GWEN_BUFFER *buf)
{
  if (valueList) {
    const AQH_VALUE *value;

    value=AQH_Value_List_First(valueList);
    while(value) {
      int rv;

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

      value=AQH_Value_List_Next(value);
    }
  }
  return 0;
}



int AQH_DataIpc_WriteValueAsTagToBuffer(unsigned int tagType, const AQH_VALUE *value, GWEN_BUFFER *buf)
{
  int startPos;
  int rv;

  startPos=GWEN_Tag16_StartTagInBuffer(tagType, buf);
  if (startPos<0) {
    DBG_INFO(AQH_LOGDOMAIN, "here (%d)", startPos);
    return startPos;
  }

  _writeValueFieldsAsTagsToBuffer(value, buf);

  rv=GWEN_Tag16_EndTagInBuffer(startPos, buf);
  if (rv<0) {
    DBG_INFO(AQH_LOGDOMAIN, "here (%d)", rv);
    return rv;
  }

  return 0;
}



int AQH_DataIpc_WriteDeviceListAsTagsToBuffer(unsigned int tagType, const AQH_DEVICE_LIST *deviceList, GWEN_BUFFER *buf)
{
  if (deviceList) {
    const AQH_DEVICE *device;

    device=AQH_Device_List_First(deviceList);
    while(device) {
      int rv;

      rv=AQH_DataIpc_WriteDeviceAsTagToBuffer(tagType, device, buf);
      if (rv<0) {
        DBG_INFO(AQH_LOGDOMAIN, "here (%d)", rv);
        GWEN_Buffer_free(buf);
        return rv;
      }

      device=AQH_Device_List_Next(device);
    }
  }
  return 0;
}



int AQH_DataIpc_WriteDeviceAsTagToBuffer(unsigned int tagType, const AQH_DEVICE *device, GWEN_BUFFER *buf)
{
  int startPos;
  int rv;

  startPos=GWEN_Tag16_StartTagInBuffer(tagType, buf);
  if (startPos<0) {
    DBG_INFO(AQH_LOGDOMAIN, "here (%d)", startPos);
    return startPos;
  }

  _writeDeviceFieldsAsTagsToBuffer(device, buf);

  rv=GWEN_Tag16_EndTagInBuffer(startPos, buf);
  if (rv<0) {
    DBG_INFO(AQH_LOGDOMAIN, "here (%d)", rv);
    return rv;
  }

  return 0;
}



void _writeValueFieldsAsTagsToBuffer(const AQH_VALUE *value, GWEN_BUFFER *buf)
{
  const char *s;

  GWEN_Tag16_WriteUint64TagToBuffer(AQH_IPCDATA_VALUE_TAGS_ID, AQH_Value_GetId(value), buf);

  s=AQH_Value_GetDriver(value);
  if (s && *s)
    GWEN_Tag16_WriteStringTagToBuffer(AQH_IPCDATA_VALUE_TAGS_DRIVER, s, buf);

  s=AQH_Value_GetName(value);
  if (s && *s)
    GWEN_Tag16_WriteStringTagToBuffer(AQH_IPCDATA_VALUE_TAGS_NAMEFORDRIVER, s, buf);

  s=AQH_Value_GetNameForSystem(value);
  if (s && *s)
    GWEN_Tag16_WriteStringTagToBuffer(AQH_IPCDATA_VALUE_TAGS_NAMEFORSYSTEM, s, buf);

  GWEN_Tag16_WriteUint32TagToBuffer(AQH_IPCDATA_VALUE_TAGS_TYPE, AQH_Value_GetValueType(value), buf);

  s=AQH_Value_GetValueUnits(value);
  if (s && *s)
    GWEN_Tag16_WriteStringTagToBuffer(AQH_IPCDATA_VALUE_TAGS_UNITS, s, buf);

  GWEN_Tag16_WriteUint64TagToBuffer(AQH_IPCDATA_VALUE_TAGS_TIMEOFCREATION, AQH_Value_GetTimestampCreation(value), buf);

  s=AQH_Value_GetDeviceName(value);
  if (s && *s)
    GWEN_Tag16_WriteStringTagToBuffer(AQH_IPCDATA_VALUE_TAGS_DEVFORDRIVER, s, buf);

  s=AQH_Value_GetDeviceNameForSystem(value);
  if (s && *s)
    GWEN_Tag16_WriteStringTagToBuffer(AQH_IPCDATA_VALUE_TAGS_DEVFORSYSTEM, s, buf);
}






AQH_VALUE_LIST *AQH_DataIpc_ReadValuesFromTagList(const GWEN_TAG16_LIST *tagList, unsigned int wantedTagType)
{
  AQH_VALUE_LIST *valueList;
  const GWEN_TAG16 *tag;

  valueList=AQH_Value_List_new();
  tag=GWEN_Tag16_List_First(tagList);
  while(tag) {
    unsigned int tagType;
    AQH_VALUE *value;

    tagType=GWEN_Tag16_GetTagType(tag);
    if (tagType==wantedTagType) {
      value=_readValueFromTag((const uint8_t*) GWEN_Tag16_GetTagData(tag), (uint32_t) GWEN_Tag16_GetTagLength(tag));
      if (value)
        AQH_Value_List_Add(value, valueList);
    }

    tag=GWEN_Tag16_List_Next(tag);
  }

  if (AQH_Value_List_GetCount(valueList)<1) {
    AQH_Value_List_free(valueList);
    return NULL;
  }

  return valueList;
}



AQH_VALUE *AQH_DataIpc_ReadValueFromTagList(const GWEN_TAG16_LIST *tagList, unsigned int wantedTagType)
{
  if (tagList) {
    const GWEN_TAG16 *tag;

    tag=GWEN_Tag16_List_FindFirstByTagType(tagList, wantedTagType);
    return tag?_readValueFromTag((const uint8_t*) GWEN_Tag16_GetTagData(tag), (uint32_t) GWEN_Tag16_GetTagLength(tag)):NULL;
  }
  return NULL;
}



AQH_DEVICE_LIST *AQH_DataIpc_ReadDevicesFromTagList(const GWEN_TAG16_LIST *tagList, unsigned int wantedTagType)
{
  AQH_DEVICE_LIST *deviceList;
  const GWEN_TAG16 *tag;

  deviceList=AQH_Device_List_new();
  tag=GWEN_Tag16_List_First(tagList);
  while(tag) {
    unsigned int tagType;
    AQH_DEVICE *device;

    tagType=GWEN_Tag16_GetTagType(tag);
    if (tagType==wantedTagType) {
      device=_readDeviceFromTag((const uint8_t*) GWEN_Tag16_GetTagData(tag), (uint32_t) GWEN_Tag16_GetTagLength(tag));
      if (device)
        AQH_Device_List_Add(device, deviceList);
    }

    tag=GWEN_Tag16_List_Next(tag);
  }

  if (AQH_Device_List_GetCount(deviceList)<1) {
    AQH_Device_List_free(deviceList);
    return NULL;
  }

  return deviceList;
}



AQH_DEVICE *AQH_DataIpc_ReadDeviceFromTagList(const GWEN_TAG16_LIST *tagList, unsigned int wantedTagType)
{
  if (tagList) {
    const GWEN_TAG16 *tag;

    tag=GWEN_Tag16_List_FindFirstByTagType(tagList, wantedTagType);
    return tag?_readDeviceFromTag((const uint8_t*) GWEN_Tag16_GetTagData(tag), (uint32_t) GWEN_Tag16_GetTagLength(tag)):NULL;
  }
  return NULL;
}



AQH_VALUE *_readValueFromTag(const uint8_t *ptr, uint32_t len)
{
  GWEN_TAG16_LIST *tagList;

  tagList=GWEN_Tag16_List_fromBuffer(ptr, len, 0);
  if (tagList) {
    GWEN_TAG16 *tag;
    AQH_VALUE *value;

    value=AQH_Value_new();
    tag=GWEN_Tag16_List_First(tagList);
    while(tag) {
      unsigned int tagType;
      char *s;

      tagType=GWEN_Tag16_GetTagType(tag);
      switch(tagType) {
      case AQH_IPCDATA_VALUE_TAGS_ID:
        AQH_Value_SetId(value, GWEN_Tag16_GetTagDataAsUint64(tag, 0));
        break;
      case AQH_IPCDATA_VALUE_TAGS_DRIVER:
        s=GWEN_Tag16_GetTagDataAsNewString(tag, NULL);
        AQH_Value_SetDriver(value, s);
        free(s);
        break;
      case AQH_IPCDATA_VALUE_TAGS_NAMEFORDRIVER:
        s=GWEN_Tag16_GetTagDataAsNewString(tag, NULL);
        AQH_Value_SetName(value, s);
        free(s);
        break;
      case AQH_IPCDATA_VALUE_TAGS_NAMEFORSYSTEM:
        s=GWEN_Tag16_GetTagDataAsNewString(tag, NULL);
        AQH_Value_SetNameForSystem(value, s);
        free(s);
        break;
      case AQH_IPCDATA_VALUE_TAGS_TYPE:
        AQH_Value_SetValueType(value, GWEN_Tag16_GetTagDataAsUint32(tag, 0));
        break;
      case AQH_IPCDATA_VALUE_TAGS_UNITS:
        s=GWEN_Tag16_GetTagDataAsNewString(tag, NULL);
        AQH_Value_SetValueUnits(value, s);
        free(s);
        break;
      case AQH_IPCDATA_VALUE_TAGS_TIMEOFCREATION:
        AQH_Value_SetTimestampCreation(value, GWEN_Tag16_GetTagDataAsUint64(tag, 0));
        break;
      case AQH_IPCDATA_VALUE_TAGS_DEVFORDRIVER:
        s=GWEN_Tag16_GetTagDataAsNewString(tag, NULL);
        AQH_Value_SetDeviceName(value, s);
        free(s);
        break;
      case AQH_IPCDATA_VALUE_TAGS_DEVFORSYSTEM:
        s=GWEN_Tag16_GetTagDataAsNewString(tag, NULL);
        AQH_Value_SetDeviceNameForSystem(value, s);
        free(s);
        break;
      default:
        DBG_INFO(AQH_LOGDOMAIN, "Unhandled tag typ %d (%02x)", tagType, tagType);
        break;
      }
      tag=GWEN_Tag16_List_Next(tag);
    }
    GWEN_Tag16_List_free(tagList);
    return value;
  }

  return NULL;
}



void _writeDeviceFieldsAsTagsToBuffer(const AQH_DEVICE *device, GWEN_BUFFER *buf)
{
  const char *s;

  GWEN_Tag16_WriteUint64TagToBuffer(AQH_IPCDATA_DEVICE_TAGS_ID, AQH_Device_GetId(device), buf);

  s=AQH_Device_GetDriver(device);
  if (s && *s)
    GWEN_Tag16_WriteStringTagToBuffer(AQH_IPCDATA_DEVICE_TAGS_DRIVER, s, buf);

  s=AQH_Device_GetRoomName(device);
  if (s && *s)
    GWEN_Tag16_WriteStringTagToBuffer(AQH_IPCDATA_DEVICE_TAGS_ROOMNAME, s, buf);

  s=AQH_Device_GetName(device);
  if (s && *s)
    GWEN_Tag16_WriteStringTagToBuffer(AQH_IPCDATA_DEVICE_TAGS_NAMEFORDRIVER, s, buf);

  s=AQH_Device_GetNameForSystem(device);
  if (s && *s)
    GWEN_Tag16_WriteStringTagToBuffer(AQH_IPCDATA_DEVICE_TAGS_NAMEFORSYSTEM, s, buf);

  s=AQH_Device_GetNameForGui(device);
  if (s && *s)
    GWEN_Tag16_WriteStringTagToBuffer(AQH_IPCDATA_DEVICE_TAGS_NAMEFORGUI, s, buf);

  s=AQH_Device_GetLocation(device);
  if (s && *s)
    GWEN_Tag16_WriteStringTagToBuffer(AQH_IPCDATA_DEVICE_TAGS_LOCATION, s, buf);

  s=AQH_Device_GetDescription(device);
  if (s && *s)
    GWEN_Tag16_WriteStringTagToBuffer(AQH_IPCDATA_DEVICE_TAGS_DESCRIPTION, s, buf);

  s=AQH_Device_GetDeviceType(device);
  if (s && *s)
    GWEN_Tag16_WriteStringTagToBuffer(AQH_IPCDATA_DEVICE_TAGS_DEVTYPE, s, buf);

  s=AQH_Device_GetManufacturer(device);
  if (s && *s)
    GWEN_Tag16_WriteStringTagToBuffer(AQH_IPCDATA_DEVICE_TAGS_MANUFACTURER, s, buf);

  GWEN_Tag16_WriteUint64TagToBuffer(AQH_IPCDATA_DEVICE_TAGS_TIMEOFCREATION, AQH_Device_GetTimestampCreation(device), buf);
}



AQH_DEVICE *_readDeviceFromTag(const uint8_t *ptr, uint32_t len)
{
  GWEN_TAG16_LIST *tagList;

  tagList=GWEN_Tag16_List_fromBuffer(ptr, len, 0);
  if (tagList) {
    GWEN_TAG16 *tag;
    AQH_DEVICE *device;

    device=AQH_Device_new();
    tag=GWEN_Tag16_List_First(tagList);
    while(tag) {
      unsigned int tagType;
      char *s;

      tagType=GWEN_Tag16_GetTagType(tag);
      switch(tagType) {
      case AQH_IPCDATA_DEVICE_TAGS_ID:
        AQH_Device_SetId(device, GWEN_Tag16_GetTagDataAsUint64(tag, 0));
        break;
      case AQH_IPCDATA_DEVICE_TAGS_DRIVER:
        s=GWEN_Tag16_GetTagDataAsNewString(tag, NULL);
        AQH_Device_SetDriver(device, s);
        free(s);
        break;
      case AQH_IPCDATA_DEVICE_TAGS_ROOMNAME:
        s=GWEN_Tag16_GetTagDataAsNewString(tag, NULL);
        AQH_Device_SetRoomName(device, s);
        free(s);
        break;
      case AQH_IPCDATA_DEVICE_TAGS_NAMEFORDRIVER:
        s=GWEN_Tag16_GetTagDataAsNewString(tag, NULL);
        AQH_Device_SetName(device, s);
        free(s);
        break;
      case AQH_IPCDATA_DEVICE_TAGS_NAMEFORSYSTEM:
        s=GWEN_Tag16_GetTagDataAsNewString(tag, NULL);
        AQH_Device_SetNameForSystem(device, s);
        free(s);
        break;
      case AQH_IPCDATA_DEVICE_TAGS_NAMEFORGUI:
        s=GWEN_Tag16_GetTagDataAsNewString(tag, NULL);
        AQH_Device_SetNameForGui(device, s);
        free(s);
        break;
      case AQH_IPCDATA_DEVICE_TAGS_LOCATION:
        s=GWEN_Tag16_GetTagDataAsNewString(tag, NULL);
        AQH_Device_SetLocation(device, s);
        free(s);
        break;
      case AQH_IPCDATA_DEVICE_TAGS_DESCRIPTION:
        s=GWEN_Tag16_GetTagDataAsNewString(tag, NULL);
        AQH_Device_SetDescription(device, s);
        free(s);
        break;
      case AQH_IPCDATA_DEVICE_TAGS_DEVTYPE:
        s=GWEN_Tag16_GetTagDataAsNewString(tag, NULL);
        AQH_Device_SetDeviceType(device, s);
        free(s);
        break;
      case AQH_IPCDATA_DEVICE_TAGS_MANUFACTURER:
        s=GWEN_Tag16_GetTagDataAsNewString(tag, NULL);
        AQH_Device_SetManufacturer(device, s);
        free(s);
        break;
      case AQH_IPCDATA_DEVICE_TAGS_TIMEOFCREATION:
        AQH_Device_SetTimestampCreation(device, GWEN_Tag16_GetTagDataAsUint64(tag, 0));
        break;
      default:
        DBG_INFO(AQH_LOGDOMAIN, "Unhandled tag typ %d (%02x)", tagType, tagType);
        break;
      }
      tag=GWEN_Tag16_List_Next(tag);
    }
    GWEN_Tag16_List_free(tagList);
    return device;
  }

  return NULL;
}




