/****************************************************************************
 * 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 "./getlastdata.h"
#include "../utils.h"

#include "aqhome/dataclient/client.h"
#include "aqhome/msg/ipc/m_ipc.h"
#include "aqhome/msg/ipc/m_ipc_result.h"
#include "aqhome/msg/ipc/data/m_ipcd.h"
#include "aqhome/msg/ipc/data/m_ipcd_getdata.h"
#include "aqhome/msg/ipc/data/m_ipcd_multidata.h"
#include "aqhome/dataclient/client.h"

#include <gwenhywfar/args.h>
#include <gwenhywfar/i18n.h>
#include <gwenhywfar/debug.h>
#include <gwenhywfar/text.h>

#include <ctype.h>



/* ------------------------------------------------------------------------------------------------
 * defs
 * ------------------------------------------------------------------------------------------------
 */

#define I18S(msg) msg
#define I18N(msg) GWEN_I18N_Translate(PACKAGE, msg)

#define A_ARG GWEN_ARGS_FLAGS_HAS_ARGUMENT
#define A_END (GWEN_ARGS_FLAGS_HELP | GWEN_ARGS_FLAGS_LAST)
#define A_CHAR GWEN_ArgsType_Char
#define A_INT GWEN_ArgsType_Int



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

static int _runCommand(AQH_DATACLIENT *dc);




/* ------------------------------------------------------------------------------------------------
 * code
 * ------------------------------------------------------------------------------------------------
 */

int AQH_Tool_GetLastData(GWEN_DB_NODE *dbGlobalArgs, int argc, char **argv)
{
  AQH_EVENT_LOOP *eventLoop;
  AQH_DATACLIENT *dc;
  int rv;
  const GWEN_ARGS args[]= {
    /* flags type    name                   min max s     long           short_descr, long_descr */
    { A_ARG, A_CHAR, "brokerAddress",       0,  1,  "t",  "tcpaddress",  I18S("TCP address to connect to [127.0.0.1]"), NULL},
    { A_ARG, A_INT,  "brokerPort",          0,  1,  "P",  "tcpport",     I18S("Specify the TCP port to listen on"), NULL},
    { A_ARG, A_INT,  "timeout",             0,  1,  "T",  NULL,          I18S("Specify timeout in seconds for response"), NULL},
    { A_ARG, A_CHAR, "brokerClientId",      0,  1,  "c",  "clientid",    I18S("Specify CLIENTID"), NULL},
    { A_ARG, A_CHAR, "userId",              0,  1,  "u",  "userid",      I18S("Specify user id"), NULL},
    { A_ARG, A_CHAR, "password",            0,  1,  "p",  "password",    I18S("Specify service password"), NULL},
    { A_ARG, A_CHAR, "valueName",           1,  1,  "N",  "valuename",   I18S("Value name (e.g. server/temp/system)"), NULL},
    { A_ARG, A_INT,  "numOfDatapoints",     0,  1,  "n",  NULL,          I18S("Get up to n datapoints"), NULL},
    { 0,     A_INT,  "printMean",           0,  1,  "M",  "mean",        I18S("Print mean value of data received"), NULL},
    { 0,     A_INT,  "printDiff",           0,  1,  "D",  "diff",        I18S("Print diff last-first value"), NULL},
    { A_END, A_INT,  "help",                0,  0,  "h",  "help",        I18S("Show this help screen"), NULL}
  };

  eventLoop=AQH_EventLoop_new();
  dc=AQH_DataClient_new(eventLoop, AQH_IPC_PROTOCOL_DATA_ID, AQH_IPC_PROTOCOL_DATA_VERSION);

  rv=AQH_DataClient_ReadLocalArgs(dc, dbGlobalArgs, args, argc, argv);
  if (rv<0) {
    DBG_ERROR(NULL, "here (%d)", rv);
    AQH_DataClient_free(dc);
    AQH_EventLoop_free(eventLoop);
    return 2;
  }

  rv=AQH_DataClient_ConnectWithArgs(dc, 0);
  if (rv<0) {
    DBG_ERROR(NULL, "Error connecting (%d)", rv);
    AQH_DataClient_free(dc);
    AQH_EventLoop_free(eventLoop);
    return 2;
  }

  rv=_runCommand(dc);
  if (rv<0) {
    DBG_ERROR(NULL, "Error running (%d)", rv);
    AQH_DataClient_free(dc);
    AQH_EventLoop_free(eventLoop);
    return 2;
  }

  AQH_DataClient_free(dc);
  AQH_EventLoop_free(eventLoop);
  return 0;
}



int _runCommand(AQH_DATACLIENT *dc)
{
  GWEN_DB_NODE *dbLocalArgs;
  const char *valueName;
  uint64_t num;
  int printMean;
  int printDiff;

  dbLocalArgs=AQH_DataClient_GetDbLocalArgs(dc);
  valueName=GWEN_DB_GetCharValue(dbLocalArgs, "valueName", 0, NULL);
  num=GWEN_DB_GetIntValue(dbLocalArgs, "numOfDatapoints", 0, 1);
  printMean=GWEN_DB_GetIntValue(dbLocalArgs, "printMean", 0, 0);
  printDiff=GWEN_DB_GetIntValue(dbLocalArgs, "printDiff", 0, 0);

  if (num>0) {
    uint64_t *dataPoints;
    uint64_t recvdNum;

    dataPoints=malloc(num*sizeof(uint64_t)*2);

    recvdNum=AQH_DataClient_GetLastData(dc, valueName, dataPoints, num);
    if (recvdNum>0) {
      if (printMean)
        Utils_PrintMeanData(dataPoints, recvdNum, NULL);
      else if (printDiff)
        Utils_PrintDiffData(dataPoints, recvdNum, NULL);
      else
        Utils_PrintDataPoints(dataPoints, recvdNum, NULL);
    }
    free(dataPoints);
  }

  return 0;
}



