/****************************************************************************
 * 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 "./u_valueset.h"

#include "aqhome/ipc/data/msg_data_set.h"
#include "aqhome/ipc/data/ipc_data.h"

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


#define DEBUG_DRY_RUN 1 /* don't actually set value if "1" */


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

#define AQHOMEREACT_UNIT_VALUESET_INSLOT_VALUE   0



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

static void _cbInputData(AQHREACT_UNIT *unit, AQHREACT_PORT *port, const AQHREACT_DATAOBJECT *dataObject);
static GWEN_MSG *_mkSetDataMsgString(const char *sValueName, const AQHREACT_DATAOBJECT *dataObject);
static GWEN_MSG *_mkSetDataMsgDouble(const char *sValueName, const AQHREACT_DATAOBJECT *dataObject);



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

AQHREACT_UNIT *AqHomeReact_UnitValueSet_new(AQHOME_REACT *aqh)
{
  AQHREACT_UNIT *unit;
  AQHREACT_PORT *port;
  AQHREACT_PARAM *param;

  unit=AQHREACT_Unit_new(aqh);
  AQHREACT_Unit_SetTypeName(unit, "valueset");
  AQHREACT_Unit_SetDescription(unit, "Set value by value path");
  AQHREACT_Unit_SetInputDataFn(unit, _cbInputData);

  port=AQHREACT_Port_new();
  AQHREACT_Port_SetName(port, "input");
  AQHREACT_Port_SetIdForUnit(port, AQHOMEREACT_UNIT_VALUESET_INSLOT_VALUE);
  AQHREACT_Port_SetDataType(port, AQHREACT_DATAOBJECTTYPE_STRING);
  AQHREACT_Unit_AddInputPort(unit, port);

  param=AQHREACT_Param_new();
  AQHREACT_Param_SetName(param, AQHOMEREACT_UNIT_VALUESET_PARAM_VALUENAME);
  AQHREACT_Param_SetDataType(param, AQHREACT_DATAOBJECTTYPE_STRING);
  AQHREACT_Unit_AddParam(unit, param);

  return unit;
}



void _cbInputData(AQHREACT_UNIT *unit, AQHREACT_PORT *port, const AQHREACT_DATAOBJECT *dataObject)
{
  if (unit && port && dataObject && AQHREACT_Port_GetIdForUnit(port)==AQHOMEREACT_UNIT_VALUESET_INSLOT_VALUE) {
    const char *sValueName;

    sValueName=AQHREACT_Unit_GetParamValueString(unit, AQHOMEREACT_UNIT_VALUESET_PARAM_VALUENAME, NULL);
    if (sValueName && *sValueName) {
      GWEN_MSG_ENDPOINT *brokerEndpoint;

      brokerEndpoint=AqHomeReact_GetBrokerEndpoint(AQHREACT_Unit_GetAqHomeReact(unit));
      if (brokerEndpoint) {
        GWEN_MSG *msgOut;

        switch(AQHREACT_DataObject_GetDataType(dataObject)) {
        case AQHREACT_DATAOBJECTTYPE_DOUBLE:
          msgOut=_mkSetDataMsgDouble(sValueName, dataObject);
          break;
        case AQHREACT_DATAOBJECTTYPE_STRING:
          msgOut=_mkSetDataMsgString(sValueName, dataObject);
          break;
        default:
          DBG_INFO(NULL, "Unhandled data type (%d)", AQHREACT_DataObject_GetDataType(dataObject));
          msgOut=NULL;
          break;
        }
        if (msgOut) {
#if DEBUG_DRY_RUN
          DBG_ERROR(NULL, "Would send data for value \"%s\"", sValueName);
          GWEN_Msg_free(msgOut);
#else
          DBG_INFO(NULL, "Sending data for value \"%s\"", sValueName);
          GWEN_MsgEndpoint_AddSendMessage(brokerEndpoint, msgOut);
#endif
        }
      }
    }
  }
}


GWEN_MSG *_mkSetDataMsgString(const char *sValueName, const AQHREACT_DATAOBJECT *dataObject)
{
  GWEN_MSG *msgOut;
  AQH_VALUE *v;

  v=AQH_Value_new();
  AQH_Value_SetNameForSystem(v, sValueName);

  msgOut=AQH_SetDataIpcMsg_new(AQH_MSGTYPE_IPC_DATA_SETDATA, v, AQHREACT_DataObject_GetStringData(dataObject));
  AQH_Value_free(v);
  return msgOut;
}



GWEN_MSG *_mkSetDataMsgDouble(const char *sValueName, const AQHREACT_DATAOBJECT *dataObject)
{
  GWEN_MSG *msgOut;
  AQH_VALUE *v;
  double data;
  GWEN_BUFFER *buf;
  int rv;

  v=AQH_Value_new();
  AQH_Value_SetNameForSystem(v, sValueName);

  data=AQHREACT_DataObject_GetDoubleData(dataObject);
  buf=GWEN_Buffer_new(0, 64, 0, 1);
  rv=GWEN_Text_DoubleToBuffer(data, buf);
  if (rv<0) {
    GWEN_Buffer_free(buf);
    AQH_Value_free(v);
    return NULL;
  }

  msgOut=AQH_SetDataIpcMsg_new(AQH_MSGTYPE_IPC_DATA_SETDATA, v, GWEN_Buffer_GetStart(buf));
  GWEN_Buffer_free(buf);
  AQH_Value_free(v);
  return msgOut;
}





