/****************************************************************************
 * 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/msg/endpoint_log_p.h"
#include "aqhome/msg/endpoint_node.h"

#include "aqhome/msg/msg_value.h"
#include "aqhome/msg/msg_value2.h"
#include "aqhome/msg/msg_sendstats.h"
#include "aqhome/msg/msg_recvstats.h"
#include "aqhome/msg/msg_ping.h"
#include "aqhome/msg/msg_pong.h"
#include "aqhome/msg/msg_needaddr.h"
#include "aqhome/msg/msg_claimaddr.h"
#include "aqhome/msg/msg_haveaddr.h"
#include "aqhome/msg/msg_denyaddr.h"
#include "aqhome/msg/msg_device.h"

#include <gwenhywfar/list.h>
#include <gwenhywfar/inherit.h>
#include <gwenhywfar/debug.h>
#include <gwenhywfar/gwentime.h>


#define AQH_MSG_ENDPOINT_LOG_NAME            "log"



GWEN_INHERIT(GWEN_MSG_ENDPOINT, AQH_MSG_ENDPOINT_LOG)


static void GWENHYWFAR_CB _freeData(void *bp, void *p);
static void _run(GWEN_MSG_ENDPOINT *ep);
static void _logMessage(GWEN_MSG_ENDPOINT *ep, const GWEN_MSG *msg);
static void _writeToLogFile(const char *filename, const char *txt);





GWEN_MSG_ENDPOINT *AQH_LogEndpoint_new(const char *filename, int groupId)
{
  GWEN_MSG_ENDPOINT *ep;
  AQH_MSG_ENDPOINT_LOG *xep;

  ep=AQH_NodeEndpoint_new(AQH_MSG_ENDPOINT_LOG_NAME, groupId);
  GWEN_NEW_OBJECT(AQH_MSG_ENDPOINT_LOG, xep);
  xep->filename=strdup(filename);
  GWEN_INHERIT_SETDATA(GWEN_MSG_ENDPOINT, AQH_MSG_ENDPOINT_LOG, ep, xep, _freeData);
  AQH_NodeEndpoint_SetAcceptedMsgGroups(ep, AQH_MSG_TYPEGROUP_ALL);
  GWEN_MsgEndpoint_AddFlags(ep, GWEN_MSG_ENDPOINT_FLAGS_NOIO);
  GWEN_MsgEndpoint_SetRunFn(ep, _run);

  return ep;
}



void _freeData(void *bp, void *p)
{
  AQH_MSG_ENDPOINT_LOG *xep;

  xep=(AQH_MSG_ENDPOINT_LOG*) p;
  free(xep->filename);
  GWEN_FREE_OBJECT(xep);
}



void _run(GWEN_MSG_ENDPOINT *ep)
{
  GWEN_MSG_LIST *msgList;

  msgList=GWEN_MsgEndpoint_GetSendMessageList(ep);
  if (msgList && GWEN_Msg_List_GetCount(msgList)) {
    GWEN_MSG *msg;

    msg=GWEN_Msg_List_First(msgList);
    while(msg) {
      GWEN_MSG *next;

      next=GWEN_Msg_List_Next(msg);
      if (GWEN_Msg_GetGroupId(msg)==GWEN_MsgEndpoint_GetGroupId(ep))
        _logMessage(ep, msg);
      GWEN_Msg_free(msg);
      msg=next;
    }
  }
}



void _logMessage(GWEN_MSG_ENDPOINT *ep, const GWEN_MSG *msg)
{
  AQH_MSG_ENDPOINT_LOG *xep;
  uint8_t msgType;
  int msgIsValid;
  GWEN_BUFFER *dbuf;
  GWEN_TIME *ti;

  xep=GWEN_INHERIT_GETDATA(GWEN_MSG_ENDPOINT, AQH_MSG_ENDPOINT_LOG, ep);
  assert(xep);
  dbuf=GWEN_Buffer_new(0, 256, 0, 1);
  ti=GWEN_CurrentTime();
  GWEN_Time_toString(ti, "YYYY-MM-DD hh:mm:ss  ", dbuf);
  GWEN_Time_free(ti);
  ti=NULL;

  msgIsValid=(AQH_NodeMsg_IsChecksumValid(msg) && AQH_NodeMsg_IsMsgComplete(msg));
  msgType=AQH_NodeMsg_GetMsgType(msg);

  if (msgIsValid) {
    switch(msgType) {
      case AQH_MSG_TYPE_PING:          AQH_PingMsg_DumpToBuffer(msg, dbuf, "received"); break;
      case AQH_MSG_TYPE_PONG:          AQH_PongMsg_DumpToBuffer(msg, dbuf, "received"); break;
      case AQH_MSG_TYPE_COMSENDSTATS:  AQH_SendStatsMsg_DumpToBuffer(msg, dbuf, "received"); break;
      case AQH_MSG_TYPE_COMRECVSTATS:  AQH_RecvStatsMsg_DumpToBuffer(msg, dbuf, "received"); break;
      case AQH_MSG_TYPE_TWIBUSMEMBER:  AQH_NodeMsg_DumpToBuffer(msg, dbuf, "received"); break;
      case AQH_MSG_TYPE_DEBUG:         AQH_NodeMsg_DumpToBuffer(msg, dbuf, "received"); break;
      case AQH_MSG_TYPE_VALUE:         AQH_ValueMsg_DumpToBuffer(msg, dbuf, "received"); break;
      case AQH_MSG_TYPE_VALUE2:        AQH_Value2Msg_DumpToBuffer(msg, dbuf, "received"); break;
      case AQH_MSG_TYPE_NEED_ADDRESS:  AQH_NeedAddrMsg_DumpToBuffer(msg, dbuf, "received"); break;
      case AQH_MSG_TYPE_CLAIM_ADDRESS: AQH_ClaimAddrMsg_DumpToBuffer(msg, dbuf, "received"); break;
      case AQH_MSG_TYPE_HAVE_ADDRESS:  AQH_HaveAddrMsg_DumpToBuffer(msg, dbuf, "received"); break;
      case AQH_MSG_TYPE_DENY_ADDRESS:  AQH_DenyAddrMsg_DumpToBuffer(msg, dbuf, "received"); break;
      case AQH_MSG_TYPE_DEVICE:        AQH_DeviceMsg_DumpToBuffer(msg, dbuf, "received"); break;
      default:                         AQH_NodeMsg_DumpToBuffer(msg, dbuf, "received"); break;
    }
  }
  else {
    AQH_NodeMsg_DumpToBuffer(msg, dbuf, "(invalid) received");
  }
  _writeToLogFile(xep->filename, GWEN_Buffer_GetStart(dbuf));
  GWEN_Buffer_free(dbuf);
}



void _writeToLogFile(const char *filename, const char *txt)
{
  if (txt && *txt) {
    FILE *f;

    f=fopen(filename, "a+");
    if (f) {
      if (1!=fwrite(txt, strlen(txt), 1, f)) {
        DBG_ERROR(AQH_LOGDOMAIN, "Error logging.");
      }
      fclose(f);
    }
  }
}





