/****************************************************************************
 * This file is part of the project AqHome.
 * AqHome (c) by 2025 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/msg/ipc/m_ipc_connect.h"
#include "aqhome/msg/ipc/m_ipc_tag16.h"
#include "aqhome/msg/ipc/m_ipc.h"
#include "aqhome/msg/ipc/data/m_ipcd.h"

#include <gwenhywfar/text.h>
#include <gwenhywfar/tag16.h>
#include <gwenhywfar/buffer.h>



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



/* ------------------------------------------------------------------------------------------------
 * implementation
 * ------------------------------------------------------------------------------------------------
 */

AQH_MESSAGE *AQH_IpcMessageConnect_new(uint8_t protoId, uint8_t protoVer, uint16_t code,
                                       uint32_t msgId, uint32_t refMsgId,
                                       const char *clientId, const char *userId, const char *password, uint32_t flags)
{
  AQH_MESSAGE *msg;
  GWEN_BUFFER *buf;

  buf=GWEN_Buffer_new(0, 256, 0, 1);
  if (clientId && *clientId)
    GWEN_Tag16_WriteStringTagToBuffer(AQH_MSG_CONNECT_TAGS_CLIENTID, clientId, buf);
  if (userId && *userId)
    GWEN_Tag16_WriteStringTagToBuffer(AQH_MSG_CONNECT_TAGS_USERID, userId, buf);
  if (password && *password)
    GWEN_Tag16_WriteStringTagToBuffer(AQH_MSG_CONNECT_TAGS_PASSWORD, password, buf);
  GWEN_Tag16_WriteUint32TagToBuffer(AQH_MSG_CONNECT_TAGS_FLAGS, flags, buf);

  msg=AQH_IpcMessage_new(protoId, protoVer, code, msgId, refMsgId,
                         GWEN_Buffer_GetUsedBytes(buf), (const uint8_t*) GWEN_Buffer_GetStart(buf));

  GWEN_Buffer_free(buf);
  return msg;
}



void AQH_IpcMessageConnect_DumpToBuffer(const AQH_MESSAGE *msg, const GWEN_TAG16_LIST *tagList, GWEN_BUFFER *dbuf, const char *sText)
{
  char *clientId=NULL;
  char *userId=NULL;
  uint32_t flags=0;

  if (tagList) {
    clientId=AQH_Tag16_GetTagDataAsNewString(tagList, AQH_MSG_CONNECT_TAGS_CLIENTID, NULL);
    userId=AQH_Tag16_GetTagDataAsNewString(tagList, AQH_MSG_CONNECT_TAGS_USERID, NULL);
    flags=AQH_Tag16_GetTagDataAsUint32(tagList, AQH_MSG_CONNECT_TAGS_FLAGS, 0);
  }

  GWEN_Buffer_AppendArgs(dbuf,
                         "CONNECT(%s) %s (code=%d, proto=%d, proto version=%d, clientId=%s, userId=%s, flags=%08x)\n",
                         AQH_IpcdMessage_MsgTypeToChar(AQH_IpcMessage_GetCode(msg)),
                         sText?sText:"",
                         AQH_IpcMessage_GetCode(msg),
                         AQH_IpcMessage_GetProtoId(msg),
                         AQH_IpcMessage_GetProtoVersion(msg),
                         clientId?clientId:"<empty>",
                         userId?userId:"<empty>",
                         flags);
  free(userId);
  free(clientId);
}





