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

#include <gwenhywfar/debug.h>



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

#define AQHOMEREACT_UNIT_ZEROPOSNEGSTRING_INSLOT_VALUE   0

#define AQHOMEREACT_UNIT_ZEROPOSNEGSTRING_OUTSLOT_RESULT 0



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

static void _cbInputData(AQHREACT_UNIT *unit, AQHREACT_PORT *port, const AQHREACT_DATAOBJECT *dataObject);



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

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

  unit=AQHREACT_Unit_new(aqh);
  AQHREACT_Unit_SetTypeName(unit, "zeroPosNegString");
  AQHREACT_Unit_SetDescription(unit, "Translate double value into strings for zero, positive, negative values");
  AQHREACT_Unit_SetInputDataFn(unit, _cbInputData);

  port=AQHREACT_Port_new();
  AQHREACT_Port_SetName(port, "output");
  AQHREACT_Port_SetIdForUnit(port, AQHOMEREACT_UNIT_ZEROPOSNEGSTRING_OUTSLOT_RESULT);
  AQHREACT_Port_SetDataType(port, AQHREACT_DATAOBJECTTYPE_STRING);
  AQHREACT_Unit_AddOutputPort(unit, port);

  port=AQHREACT_Port_new();
  AQHREACT_Port_SetName(port, "input");
  AQHREACT_Port_SetIdForUnit(port, AQHOMEREACT_UNIT_ZEROPOSNEGSTRING_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_VALUE_NEG);
  AQHREACT_Param_SetDataType(param, AQHREACT_DATAOBJECTTYPE_STRING);
  AQHREACT_Unit_AddParam(unit, param);

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

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

  return unit;
}



void _cbInputData(AQHREACT_UNIT *unit, GWEN_UNUSED AQHREACT_PORT *port, const AQHREACT_DATAOBJECT *dataObject)
{
  if (unit && dataObject) {
    AQHREACT_PORT *outputPort;

    outputPort=AQHREACT_Unit_GetOutputPortByIdForUnit(unit, AQHOMEREACT_UNIT_ZEROPOSNEGSTRING_OUTSLOT_RESULT);
    if (outputPort) {
      const char *result=NULL;
      double data;

      data=AQHREACT_DataObject_GetDoubleData(dataObject);
      if (data>0.0)
        result=AQHREACT_Unit_GetParamValueString(unit, AQHOMEREACT_UNIT_VALUESET_PARAM_VALUE_POS, NULL);
      else if (data<0.0)
        result=AQHREACT_Unit_GetParamValueString(unit, AQHOMEREACT_UNIT_VALUESET_PARAM_VALUE_NEG, NULL);
      else
        result=AQHREACT_Unit_GetParamValueString(unit, AQHOMEREACT_UNIT_VALUESET_PARAM_VALUE_ZERO, NULL);

      if (result && *result) {
        DBG_INFO(NULL, "Sending \"%s\" to output", result);
        AQHREACT_Unit_OutputStringData(unit, outputPort, result);
      }
    }
  }
}




