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

#include <gwenhywfar/debug.h>



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

#define AQHOMEREACT_UNIT_VALUEFILTER_INSLOT_VALUE   0

#define AQHOMEREACT_UNIT_VALUEFILTER_OUTSLOT_RESULT 0



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

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



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

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

  unit=AQHREACT_Unit_new(aqh);
  AQHREACT_Unit_SetTypeName(unit, "valuefilter");
  AQHREACT_Unit_SetDescription(unit, "Filter values received from unit VarChanges by value path");
  AQHREACT_Unit_SetInputDataFn(unit, _cbInputData);

  port=AQHREACT_Port_new();
  AQHREACT_Port_SetName(port, "output");
  AQHREACT_Port_SetIdForUnit(port, AQHOMEREACT_UNIT_VALUEFILTER_OUTSLOT_RESULT);
  AQHREACT_Port_SetDataType(port, AQHREACT_DATAOBJECTTYPE_DOUBLE);
  AQHREACT_Unit_AddOutputPort(unit, port);

  port=AQHREACT_Port_new();
  AQHREACT_Port_SetName(port, "input");
  AQHREACT_Port_SetIdForUnit(port, AQHOMEREACT_UNIT_VALUEFILTER_INSLOT_VALUE);
  AQHREACT_Port_SetDataType(port, AQHREACT_DATAOBJECTTYPE_DOUBLE);
  AQHREACT_Unit_AddInputPort(unit, port);

  param=AQHREACT_Param_new();
  AQHREACT_Param_SetName(param, AQHOMEREACT_UNIT_VALUEFILTER_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_VALUEFILTER_INSLOT_VALUE) {
    AQHREACT_PORT *outputPort;

    outputPort=AQHREACT_Unit_GetOutputPortByIdForUnit(unit, AQHOMEREACT_UNIT_VALUEFILTER_OUTSLOT_RESULT);
    if (outputPort) {
      const char *sSystemValueId;

      sSystemValueId=AQHREACT_DataObject_GetSystemValueId(dataObject);
      if (sSystemValueId && *sSystemValueId) {
        const char *sFilter;

        sFilter=AQHREACT_Unit_GetParamValueString(unit, AQHOMEREACT_UNIT_VALUEFILTER_PARAM_VALUENAME, NULL);
        if (sFilter && *sFilter && strcasecmp(sSystemValueId, sFilter)==0) {
          DBG_DEBUG(NULL, "Value \"%s\" matches", sSystemValueId);
          AQHREACT_Unit_OutputData(unit, outputPort, dataObject);
        }
        else {
          DBG_DEBUG(NULL, "Value \"%s\" does not match", sSystemValueId);
        }
      }
    }
  }
}




