/****************************************************************************
 * 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/mqtt/msg_mqtt_publish.h"

#include <gwenhywfar/debug.h>



static int _dumpPayload(uint8_t flags, const uint8_t *payloadPtr, uint32_t payloadLen, GWEN_BUFFER *dbuf);





GWEN_MSG *AQH_PublishMqttMsg_new(uint8_t flags, uint16_t packetId, const char *sTopic, const uint8_t *messagePtr, uint32_t messageLen)
{
  if (sTopic && *sTopic) {
    GWEN_MSG *msg;
    GWEN_BUFFER *buf;

    buf=GWEN_Buffer_new(0, 64, 0, 1);
    AQH_MqttMsg_AppendStringWithLen(buf, sTopic);
    if (flags & (AQH_MQTTMSG_FLAGS_QOS2 | AQH_MQTTMSG_FLAGS_QOS1)) {
      GWEN_Buffer_AppendByte(buf, (packetId>>8) & 0xff);
      GWEN_Buffer_AppendByte(buf, packetId & 0xff);
    }
    if (messagePtr && messageLen)
      GWEN_Buffer_AppendBytes(buf, (const char*) messagePtr, messageLen);

    msg=GWEN_MqttMsg_new(AQH_MQTTMSG_MSGTYPE_PUBLISH | flags, GWEN_Buffer_GetUsedBytes(buf), (const uint8_t*) GWEN_Buffer_GetStart(buf));
    GWEN_Buffer_free(buf);
    if (msg==NULL) {
      DBG_INFO(AQH_LOGDOMAIN, "here");
      return NULL;
    }
    return msg;
  }
  else {
    DBG_ERROR(AQH_LOGDOMAIN, "Missing topic");
    return NULL;
  }
}



void AQH_PublishMqttMsg_DumpToBuffer(const GWEN_MSG *msg, GWEN_BUFFER *dbuf, const char *sText)
{
  const uint8_t *msgPtr;
  uint32_t msgLen;

  msgPtr=GWEN_Msg_GetConstBuffer(msg);
  msgLen=GWEN_Msg_GetBytesInBuffer(msg);

  if (msgLen>1) {
    uint32_t payloadLen;
    const uint8_t *payloadPtr;
    int rv;

    payloadLen=GWEN_Msg_GetParsedPayloadSize(msg);
    payloadPtr=msgPtr+GWEN_Msg_GetParsedPayloadOffset(msg);

    GWEN_Buffer_AppendArgs(dbuf, "%s %s", AQH_MqttMsg_MsgTypeToString(msgPtr[0] & 0xf0), sText);

    GWEN_Buffer_AppendString(dbuf, "(");
    rv=_dumpPayload((msgPtr[0] & 0x0f), payloadPtr, payloadLen, dbuf);
    if (rv<0) {
      DBG_INFO(AQH_LOGDOMAIN, "here (%d)", rv);
    }
    else {
      GWEN_Buffer_AppendString(dbuf, ")");
    }
  }
}



char *AQH_PublishMqttMsg_ExtractTopic(const GWEN_MSG *msg)
{
  const uint8_t *msgPtr;
  uint32_t msgLen;

  msgPtr=GWEN_Msg_GetConstBuffer(msg);
  msgLen=GWEN_Msg_GetBytesInBuffer(msg);

  if (msgLen>1) {
    uint32_t payloadLen;
    const uint8_t *payloadPtr;
    char *result;

    payloadLen=GWEN_Msg_GetParsedPayloadSize(msg);
    payloadPtr=msgPtr+GWEN_Msg_GetParsedPayloadOffset(msg);

    result=AQH_MqttMsg_ExtractStringAt(payloadPtr, payloadLen);
    if (result==NULL) {
      DBG_INFO(AQH_LOGDOMAIN, "here");
      return NULL;
    }
    return result;
  }

  return NULL;
}



char *AQH_PublishMqttMsg_ExtractValue(const GWEN_MSG *msg)
{
  const uint8_t *msgPtr;
  uint32_t msgLen;

  msgPtr=GWEN_Msg_GetConstBuffer(msg);
  msgLen=GWEN_Msg_GetBytesInBuffer(msg);

  if (msgLen>1) {
    uint8_t flags;
    uint32_t payloadLen;
    const uint8_t *payloadPtr;
    int rv;

    flags=msgPtr[0] & 0x0f;
    payloadLen=GWEN_Msg_GetParsedPayloadSize(msg);
    payloadPtr=msgPtr+GWEN_Msg_GetParsedPayloadOffset(msg);

    rv=AQH_MqttMsg_SkipStringAt(payloadPtr, payloadLen);
    if (rv<0) {
      DBG_INFO(AQH_LOGDOMAIN, "here (%d)", rv);
      return NULL;
    }
    payloadLen-=rv;
    payloadPtr+=rv;

    if (flags & (AQH_MQTTMSG_FLAGS_QOS2 | AQH_MQTTMSG_FLAGS_QOS1)) {
      if (payloadLen<2) {
        DBG_ERROR(AQH_LOGDOMAIN, "Msg too small");
        return NULL;
      }
      else {
        payloadLen-=2;
        payloadPtr+=2;
      }
    }

    if (payloadLen) {
      char *result;

      result=(char*) malloc(payloadLen+1);
      memmove(result, payloadPtr, payloadLen);
      result[payloadLen]=0;
      return result;
    }
    else {
      DBG_INFO(AQH_LOGDOMAIN, "No message");
    }
  }

  return NULL;
}



int _dumpPayload(uint8_t flags, const uint8_t *payloadPtr, uint32_t payloadLen, GWEN_BUFFER *dbuf)
{
  int rv;

  GWEN_Buffer_AppendString(dbuf, "flags:");
  if (flags & AQH_MQTTMSG_FLAGS_DUP)
    GWEN_Buffer_AppendString(dbuf, " DUP");
  if (flags & AQH_MQTTMSG_FLAGS_RETAIN)
    GWEN_Buffer_AppendString(dbuf, " RETAIN");
  GWEN_Buffer_AppendArgs(dbuf, " QOS%d", (flags>>1) & 0x03);

  GWEN_Buffer_AppendString(dbuf, " topic: ");
  rv=AQH_MqttMsg_DumpString(payloadPtr, payloadLen, dbuf);
  if (rv<0) {
    DBG_INFO(AQH_LOGDOMAIN, "here (%d)", rv);
    return rv;
  }
  payloadLen-=rv;
  payloadPtr+=rv;

  if (flags & (AQH_MQTTMSG_FLAGS_QOS2 | AQH_MQTTMSG_FLAGS_QOS1)) {
    if (payloadLen<2) {
      DBG_ERROR(AQH_LOGDOMAIN, "Msg too small");
      return GWEN_ERROR_BAD_DATA;
    }
    else {
      GWEN_Buffer_AppendArgs(dbuf, " packet id: %d", (payloadPtr[0]<<8)+payloadPtr[1]);
      payloadLen-=2;
      payloadPtr+=2;
    }
  }

  if (payloadLen) {
    GWEN_Buffer_AppendString(dbuf, " message: ");
    GWEN_Buffer_AppendBytes(dbuf, (const char*) payloadPtr, payloadLen);
  }

  return 0;
}



