/****************************************************************************
 * 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/msg/ipc/data/m_ipcd.h"
#include "aqhome/msg/ipc/data/m_ipcd_setdata.h"
#include "aqhome/ipc2/endpoint.h"

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


#define DEBUG_DRY_RUN 0 /* 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 AQH_MESSAGE *_mkSetDataMsgDouble(AQH_OBJECT *brokerEndpoint, const char *sValueName, const AQHREACT_DATAOBJECT *dataObject);



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

AQHREACT_UNIT *AqHomeReact_UnitValueSet_new(AQH_OBJECT *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_DOUBLE);
  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) {
      AQH_OBJECT *brokerEndpoint;

      brokerEndpoint=AQH_ReactServer_GetBrokerEndpoint(AQHREACT_Unit_GetAqHomeReact(unit));
      if (brokerEndpoint) {
        AQH_MESSAGE *msgOut;

        switch(AQHREACT_DataObject_GetDataType(dataObject)) {
        case AQHREACT_DATAOBJECTTYPE_DOUBLE:
          msgOut=_mkSetDataMsgDouble(brokerEndpoint, sValueName, dataObject);
          break;
        default:
          DBG_ERROR(NULL, "Unhandled data type (%d)", AQHREACT_DataObject_GetDataType(dataObject));
          msgOut=NULL;
          break;
        }
        if (msgOut) {
#if DEBUG_DRY_RUN
          GWEN_BUFFER *dbuf;

          dbuf=GWEN_Buffer_new(0, 256, 0, 1);
          GWEN_Buffer_AppendArgs(dbuf, "Would send data for value \"%s\": ", sValueName);
          AQHREACT_DataObject_Dump(dataObject, dbuf, 0);
          DBG_ERROR(NULL, "%s\n", GWEN_Buffer_GetStart(dbuf));
          GWEN_Buffer_free(dbuf);
          AQH_Message_free(msgOut);
#else
          GWEN_BUFFER *dbuf;

          dbuf=GWEN_Buffer_new(0, 256, 0, 1);
          GWEN_Buffer_AppendArgs(dbuf, "Send: \"%s\" = ", sValueName);
          AQHREACT_DataObject_Dump(dataObject, dbuf, 0);
          DBG_ERROR(NULL, "%s\n", GWEN_Buffer_GetStart(dbuf));
          GWEN_Buffer_free(dbuf);

          //	  DBG_ERROR(NULL, "Sending data for value \"%s\"", sValueName);
          AQH_Endpoint_AddMsgOut(brokerEndpoint, msgOut);
#endif
        }
      }
    }
  }
}



AQH_MESSAGE *_mkSetDataMsgDouble(AQH_OBJECT *brokerEndpoint, const char *sValueName, const AQHREACT_DATAOBJECT *dataObject)
{
  AQH_MESSAGE *msgOut;
  AQH_VALUE *v;
  double data;

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

  data=AQHREACT_DataObject_GetDoubleData(dataObject);

  msgOut=AQH_IpcdMessageSetData_new(AQH_MSGTYPE_IPC_DATA_SETDATA,
                                    AQH_Endpoint_GetNextMessageId(brokerEndpoint), 0,
                                    v, data);
  AQH_Value_free(v);
  return msgOut;
}





