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

#include "aqhome/msg/msg_value.h"
#include "aqhome/msg/msg_value2.h"
#include "aqhome/msg/msg_value3.h"
#include "aqhome/msg/msg_sendstats.h"
#include "aqhome/msg/msg_recvstats.h"
#include "aqhome/msg/msg_memstats.h"
#include "aqhome/msg/msg_sysstats.h"
#include "aqhome/msg/msg_ping.h"
#include "aqhome/msg/msg_pong.h"
#include "aqhome/msg/m_heartbeat.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 "aqhome/msg/msg_flashready.h"
#include "aqhome/msg/msg_flashstart.h"
#include "aqhome/msg/msg_flashresponse.h"
#include "aqhome/msg/msg_flashend.h"
#include "aqhome/msg/msg_flashdata.h"
#include "aqhome/msg/msg_reboot.h"

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

#include <stdio.h>



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

static void _writeToLogFile(const char *filename, const char *txt);



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

void AqHomed_LogTtyMsg(AQHOMED *aqh, const GWEN_MSG *msg)
{
  if (aqh && aqh->logFile) {
    uint8_t msgType;
    int msgIsValid;
    GWEN_BUFFER *dbuf;
    GWEN_TIME *ti;
  
    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_HEARTBEAT:      AQH_HeartbeatMessage_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;
        case AQH_MSG_TYPE_MEMSTATS:       AQH_MemStatsMsg_DumpToBuffer(msg, dbuf, "received"); break;
        case AQH_MSG_TYPE_SYSSTATS:       AQH_SysStatsMsg_DumpToBuffer(msg, dbuf, "received"); break;
        case AQH_MSG_TYPE_FLASH_READY:    AQH_FlashReadyMsg_DumpToBuffer(msg, dbuf, "received"); break;
        case AQH_MSG_TYPE_FLASH_START:    AQH_FlashStartMsg_DumpToBuffer(msg, dbuf, "received"); break;
        case AQH_MSG_TYPE_FLASH_RSP:      AQH_FlashResponseMsg_DumpToBuffer(msg, dbuf, "received"); break;
        case AQH_MSG_TYPE_FLASH_END:      AQH_FlashEndMsg_DumpToBuffer(msg, dbuf, "received"); break;
        case AQH_MSG_TYPE_FLASH_DATA:     AQH_FlashDataMsg_DumpToBuffer(msg, dbuf, "received"); break;
        case AQH_MSG_TYPE_REBOOT_REQ:     AQH_RebootRequestMsg_DumpToBuffer(msg, dbuf, "received"); break;
        case AQH_MSG_TYPE_REBOOT_RSP:     AQH_RebootResponseMsg_DumpToBuffer(msg, dbuf, "received"); break;
        case AQH_MSG_TYPE_VALUE_REPORT:
        case AQH_MSG_TYPE_VALUE_SET:
        case AQH_MSG_TYPE_VALUE_SET_ACK:
        case AQH_MSG_TYPE_VALUE_SET_NACK: AQH_Value3Msg_DumpToBuffer(msg, dbuf, "received"); break;
        default:                          AQH_NodeMsg_DumpToBuffer(msg, dbuf, "received"); break;
      }
    }
    else {
      AQH_NodeMsg_DumpToBuffer(msg, dbuf, "(invalid) received");
    }
    _writeToLogFile(aqh->logFile, 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);
    }
  }
}



