/****************************************************************************
 * This file is part of the project AqHome.
 * AqHome (c) by 2024 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 "./link_p.h"

#include <gwenhywfar/debug.h>


GWEN_LIST_FUNCTIONS(AQHREACT_LINK, AQHREACT_Link)


AQHREACT_LINK *AQHREACT_Link_new()
{
  AQHREACT_LINK *lnk;

  GWEN_NEW_OBJECT(AQHREACT_LINK, lnk);
  GWEN_LIST_INIT(AQHREACT_LINK, lnk);

  return lnk;
}



void AQHREACT_Link_free(AQHREACT_LINK *lnk)
{
  if (lnk) {
    GWEN_LIST_FINI(AQHREACT_LINK, lnk);
    GWEN_FREE_OBJECT(lnk);
  }
}



AQHREACT_UNIT *AQHREACT_Link_GetTargetUnit(const AQHREACT_LINK *lnk)
{
  return lnk?lnk->targetUnit:NULL;
}



void AQHREACT_Link_SetTargetUnit(AQHREACT_LINK *lnk, AQHREACT_UNIT *unit)
{
  if (lnk)
    lnk->targetUnit=unit;
}



AQHREACT_PORT *AQHREACT_Link_GetTargetPort(const AQHREACT_LINK *lnk)
{
  return lnk?lnk->targetPort:NULL;
}



void AQHREACT_Link_SetTargetPort(AQHREACT_LINK *lnk, AQHREACT_PORT *port)
{
  if (lnk)
    lnk->targetPort=port;
}



void AQHREACT_Link_Dump(const AQHREACT_LINK *lnk, GWEN_BUFFER *buf, int indent)
{
  if (lnk && buf) {
    const char *unitName;
    const char *unitId;
    const char *portName;

    unitName=(lnk->targetUnit)?AQHREACT_Unit_GetTypeName(lnk->targetUnit):NULL;
    unitId=(lnk->targetUnit)?AQHREACT_Unit_GetId(lnk->targetUnit):NULL;
    portName=(lnk->targetPort)?AQHREACT_Port_GetName(lnk->targetPort):NULL;
    if (indent)
      GWEN_Buffer_FillWithBytes(buf, ' ', indent);
    GWEN_Buffer_AppendArgs(buf, "- target unit id: %s (%s), target slot name: %s [%d]\n",
                           unitId?unitId:"<no id>",
                           unitName?unitName:"<no name>",
                           portName?portName:"<no port>",
                           (lnk->targetPort)?AQHREACT_Port_GetIdForUnit(lnk->targetPort):0);
  }
}



void AQHREACT_Link_List_Dump(const AQHREACT_LINK_LIST *lnkList, GWEN_BUFFER *buf, int indent, const char *title)
{
  if (lnkList && buf) {
    const AQHREACT_LINK *lnk;

    if (indent)
      GWEN_Buffer_FillWithBytes(buf, ' ', indent);
    GWEN_Buffer_AppendArgs(buf, "%s\n", (title && *title)?title:"Link List:\n");

    lnk=AQHREACT_Link_List_First(lnkList);
    while(lnk) {
      AQHREACT_Link_Dump(lnk, buf, indent+2);
      lnk=AQHREACT_Link_List_Next(lnk);
    }
  }
}






