/****************************************************************************
 * 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 "./loop_tty_ipc.h"
#include "./aqhomed_p.h"
#include "./tty_log.h"
#include "./tty_write.h"
#include "./db.h"

#include "aqhome/msg/endpoint_tty.h"
#include "aqhome/msg/msg_node.h"
#include "aqhome/msg/msg_value2.h"
#include "aqhome/ipc/endpoint_ipc.h"
#include "aqhome/ipc/nodes/msg_ipc_forward.h"
#include "aqhome/ipc/nodes/msg_ipc_value.h"
#include "aqhome/mqtt/endpoint_mqttc.h"

#include <gwenhywfar/gwenhywfar.h>
#include <gwenhywfar/args.h>
#include <gwenhywfar/debug.h>
#include <gwenhywfar/endpoint_tcpd.h>



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

#define I18N(msg) msg
#define I18S(msg) msg



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

static void _forwardValue2MsgToIpc(GWEN_MSG_ENDPOINT *ep, const GWEN_MSG *nodeMsg);
static void _forwardAnyMsgToIpc(GWEN_MSG_ENDPOINT *ep, const GWEN_MSG *nodeMsg);



/* ------------------------------------------------------------------------------------------------
 * implementations
 * ------------------------------------------------------------------------------------------------
 */

void AqHomed_ForwardTtyMsgToIpcClients(AQHOMED *aqh, const GWEN_MSG *msg)
{
  uint32_t msgGroup;

  msgGroup=AQH_NodeMsg_GetMsgGroup(AQH_NodeMsg_GetMsgType(msg));
  if (msgGroup) {
    GWEN_MSG_ENDPOINT *ep;

    ep=GWEN_MsgEndpoint_Tree2_GetFirstChild(aqh->ipcdEndpoint);
    while(ep) {
      if (msgGroup & AQH_IpcEndpoint_GetAcceptedMsgGroups(ep)) {
        DBG_INFO(NULL, "Endpoint accepts msg group %d", msgGroup);
        switch(AQH_NodeMsg_GetMsgType(msg)) {
        case AQH_MSG_TYPE_VALUE2:
          _forwardValue2MsgToIpc(ep, msg);
          break;
        default:
          _forwardAnyMsgToIpc(ep, msg);
          break;
        }

      }
      ep=GWEN_MsgEndpoint_Tree2_GetNext(ep);
    }
  }
  else {
    DBG_ERROR(NULL, "Message type %d not in any message group, ignoring message", AQH_NodeMsg_GetMsgType(msg));
  }
}



void _forwardValue2MsgToIpc(GWEN_MSG_ENDPOINT *ep, const GWEN_MSG *nodeMsg)
{
  GWEN_MSG *ipcMsg;

  ipcMsg=AQH_ValueIpcMsg_new(AQH_MSGTYPE_IPC_NODES_VALUE,
                             AQH_Value2Msg_GetUid(nodeMsg),
                             AQH_Value2Msg_GetValueId(nodeMsg),
                             AQH_Value2Msg_GetValueType(nodeMsg),
                             AQH_Value2Msg_GetValueNom(nodeMsg),
                             AQH_Value2Msg_GetValueDenom(nodeMsg));
  GWEN_MsgEndpoint_AddSendMessage(ep, ipcMsg);
}



void _forwardAnyMsgToIpc(GWEN_MSG_ENDPOINT *ep, const GWEN_MSG *nodeMsg)
{
  GWEN_MSG *ipcMsg;

  ipcMsg=AQH_ForwardIpcMsg_new(AQH_MSGTYPE_IPC_NODES_FORWARD, GWEN_Msg_GetConstBuffer(nodeMsg), GWEN_Msg_GetBytesInBuffer(nodeMsg));
  GWEN_MsgEndpoint_AddSendMessage(ep, ipcMsg);
}





