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

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



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

static void _cbInputData(AQHREACT_UNIT *unit, AQHREACT_PORT *port, const AQHREACT_DATAOBJECT *dataObject);
static void _setDoubleValue(AQHREACT_UNIT *unit, const char *sValueName, const AQHREACT_DATAOBJECT *dataObject);
static void _setStringValue(AQHREACT_UNIT *unit, const char *sValueName, const AQHREACT_DATAOBJECT *dataObject);



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

AQHREACT_UNIT *AqHomeReact_UnitVarSet_new(AQH_OBJECT *aqh)
{
  AQHREACT_UNIT *unit;
  AQHREACT_PORT *port;
  AQHREACT_PARAM *param;

  unit=AQHREACT_Unit_new(aqh);
  AQHREACT_Unit_SetTypeName(unit, "varset");
  AQHREACT_Unit_SetDescription(unit, "Set variable");
  AQHREACT_Unit_SetInputDataFn(unit, _cbInputData);

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

  param=AQHREACT_Param_new();
  AQHREACT_Param_SetName(param, AQHOMEREACT_UNIT_VARSET_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_VARSET_INSLOT_VALUE) {
    const char *sValueName;

    sValueName=AQHREACT_Unit_GetParamValueString(unit, AQHOMEREACT_UNIT_VARSET_PARAM_VALUENAME, NULL);
    if (sValueName && *sValueName) {
      switch(AQHREACT_DataObject_GetDataType(dataObject)) {
      case AQHREACT_DATAOBJECTTYPE_DOUBLE:
        _setDoubleValue(unit, sValueName, dataObject);
        break;
      case AQHREACT_DATAOBJECTTYPE_STRING:
        _setStringValue(unit, sValueName, dataObject);
        break;
      default:
        DBG_INFO(NULL, "Unhandled data type (%d)", AQHREACT_DataObject_GetDataType(dataObject));
        break;
      }
    }
  }
}



void _setDoubleValue(AQHREACT_UNIT *unit, const char *sValueName, const AQHREACT_DATAOBJECT *dataObject)
{
  AQH_OBJECT *aqh;
  int rv;

  aqh=AQHREACT_Unit_GetAqHomeReact(unit);
  rv=AQH_ReactServer_SetDoubleValue(aqh, sValueName, AQHREACT_DataObject_GetDoubleData(dataObject));
  if (rv<0) {
    DBG_INFO(NULL, "here (%d)", rv);
  }
}



void _setStringValue(AQHREACT_UNIT *unit, const char *sValueName, const AQHREACT_DATAOBJECT *dataObject)
{
  AQH_OBJECT *aqh;
  int rv;

  aqh=AQHREACT_Unit_GetAqHomeReact(unit);
  rv=AQH_ReactServer_SetCharValue(aqh, sValueName, AQHREACT_DataObject_GetStringData(dataObject));
  if (rv<0) {
    DBG_INFO(NULL, "here (%d)", rv);
  }
}



