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

#include <gwenhywfar/debug.h>



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

#define AQHOMEREACT_UNIT_STATFNS_INSLOT_INPUT     100
#define AQHOMEREACT_UNIT_STATFNS_INSLOT_AUTOINPUT 101

#define AQHOMEREACT_UNIT_STATFNS_OUTSLOT_RESULT   0



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

static AQHREACT_UNIT *_unitStatFns_new(AQH_OBJECT *aqh);
static int _outputResult(AQHREACT_UNIT *unit, AQHREACT_PORT *port, double result);
static int _cbProcessAvg(AQHREACT_UNIT *unit);
static int _cbProcessMin(AQHREACT_UNIT *unit);
static int _cbProcessMax(AQHREACT_UNIT *unit);



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


AQHREACT_UNIT *AqHomeReact_UnitAverage_new(AQH_OBJECT *aqh)
{
  AQHREACT_UNIT *unit;

  unit=_unitStatFns_new(aqh);
  AQHREACT_Unit_SetTypeName(unit, "average");
  AQHREACT_Unit_SetDescription(unit, "Average over inputs");
  AQHREACT_Unit_SetProcessFn(unit, _cbProcessAvg);
  return unit;
}



AQHREACT_UNIT *AqHomeReact_UnitMinValue_new(AQH_OBJECT *aqh)
{
  AQHREACT_UNIT *unit;

  unit=_unitStatFns_new(aqh);
  AQHREACT_Unit_SetTypeName(unit, "minvalue");
  AQHREACT_Unit_SetDescription(unit, "Smallest value from all inputs");
  AQHREACT_Unit_SetProcessFn(unit, _cbProcessMin);
  return unit;
}



AQHREACT_UNIT *AqHomeReact_UnitMaxValue_new(AQH_OBJECT *aqh)
{
  AQHREACT_UNIT *unit;

  unit=_unitStatFns_new(aqh);
  AQHREACT_Unit_SetTypeName(unit, "maxvalue");
  AQHREACT_Unit_SetDescription(unit, "Highest value from all inputs");
  AQHREACT_Unit_SetProcessFn(unit, _cbProcessMax);
  return unit;
}



AQHREACT_UNIT *_unitStatFns_new(AQH_OBJECT *aqh)
{
  AQHREACT_UNIT *unit;
  AQHREACT_PORT *port;

  unit=AQHREACT_Unit_new(aqh);

  AQHREACT_Unit_SetNextInputPortId(unit, AQHOMEREACT_UNIT_STATFNS_INSLOT_AUTOINPUT); /* for auto-gen multi-slots */

  port=AQHREACT_Port_new();
  AQHREACT_Port_SetName(port, "output");
  AQHREACT_Port_SetIdForUnit(port, AQHOMEREACT_UNIT_STATFNS_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_STATFNS_INSLOT_INPUT);
  AQHREACT_Port_SetDataType(port, AQHREACT_DATAOBJECTTYPE_DOUBLE);
  AQHREACT_Port_AddFlags(port, AQHREACT_UNIT_FLAGS_MULTI);
  AQHREACT_Unit_AddInputPort(unit, port);

  return unit;
}



int _outputResult(AQHREACT_UNIT *unit, AQHREACT_PORT *port, double result)
{
  DBG_INFO(NULL, "%s: Sending result %f", AQHREACT_Unit_GetId(unit), result);
  AQHREACT_Unit_OutputDoubleData(unit, port, result);
  return 1; /* we changed something */
}



int _cbProcessAvg(AQHREACT_UNIT *unit)
{
  if (unit && AQHREACT_Unit_InputHasChanged(unit)) {
    AQHREACT_PORT *outputPort;

    outputPort=AQHREACT_Unit_GetOutputPortByIdForUnit(unit, AQHOMEREACT_UNIT_STATFNS_OUTSLOT_RESULT);
    if (outputPort) {
      const AQHREACT_PORT *port;
      double sum=0.0;
      int numHandledInputs=0;

      port=AQHREACT_Port_List_First(AQHREACT_Unit_GetInputPortList(unit));
      while(port) {
        const char *portName;
        const AQHREACT_DATAOBJECT *dataObject;

        portName=AQHREACT_Port_GetName(port);
        dataObject=AQHREACT_Port_GetCurrentDataObject(port);
        if (dataObject) {
          if (AQHREACT_DataObject_GetDataType(dataObject)==AQHREACT_DATAOBJECTTYPE_DOUBLE) {
            double d;

            d=AQHREACT_DataObject_GetDoubleData(dataObject);
            sum+=d;
            numHandledInputs++;
          }
          else {
            DBG_ERROR(NULL, "Data at input slot \"%s\" isn't DOUBLE, ignoring", portName?portName:"<unnamed>");
          }
        }
        else {
          DBG_ERROR(NULL, "Data at input slot \"%s\" has no current data, ignoring", portName?portName:"<unnamed>");
        }
        port=AQHREACT_Port_List_Next(port);
      }

      AQHREACT_Unit_ClearChangeFlagsInUnitAndInputPorts(unit);
      if (numHandledInputs) {
        double result;

        result=sum/numHandledInputs;
        return _outputResult(unit, outputPort, result);
      }
    }
  }

  return 0;
}



int _cbProcessMin(AQHREACT_UNIT *unit)
{
  if (unit && AQHREACT_Unit_InputHasChanged(unit)) {
    AQHREACT_PORT *outputPort;

    outputPort=AQHREACT_Unit_GetOutputPortByIdForUnit(unit, AQHOMEREACT_UNIT_STATFNS_OUTSLOT_RESULT);
    if (outputPort) {
      const AQHREACT_PORT *port;
      double value=0.0;
      int numHandledInputs=0;

      port=AQHREACT_Port_List_First(AQHREACT_Unit_GetInputPortList(unit));
      while(port) {
        const char *portName;
        const AQHREACT_DATAOBJECT *dataObject;

        portName=AQHREACT_Port_GetName(port);
        dataObject=AQHREACT_Port_GetCurrentDataObject(port);
        if (dataObject) {
          if (AQHREACT_DataObject_GetDataType(dataObject)==AQHREACT_DATAOBJECTTYPE_DOUBLE) {
            double d;

            d=AQHREACT_DataObject_GetDoubleData(dataObject);
            if (numHandledInputs==0)
              value=d;
            else
              value=(d<value)?d:value;
            numHandledInputs++;
          }
          else {
            DBG_ERROR(NULL, "Data at input slot \"%s\" isn't DOUBLE, ignoring", portName?portName:"<unnamed>");
          }
        }
        else {
          DBG_ERROR(NULL, "Data at input slot \"%s\" has no current data, ignoring", portName?portName:"<unnamed>");
        }
        port=AQHREACT_Port_List_Next(port);
      }

      AQHREACT_Unit_ClearChangeFlagsInUnitAndInputPorts(unit);
      if (numHandledInputs) {
        return _outputResult(unit, outputPort, value);
      }
    }
  }

  return 0;
}



int _cbProcessMax(AQHREACT_UNIT *unit)
{
  if (unit && AQHREACT_Unit_InputHasChanged(unit)) {
    AQHREACT_PORT *outputPort;

    outputPort=AQHREACT_Unit_GetOutputPortByIdForUnit(unit, AQHOMEREACT_UNIT_STATFNS_OUTSLOT_RESULT);
    if (outputPort) {
      const AQHREACT_PORT *port;
      double value=0.0;
      int numHandledInputs=0;

      port=AQHREACT_Port_List_First(AQHREACT_Unit_GetInputPortList(unit));
      while(port) {
        const char *portName;
        const AQHREACT_DATAOBJECT *dataObject;

        portName=AQHREACT_Port_GetName(port);
        dataObject=AQHREACT_Port_GetCurrentDataObject(port);
        if (dataObject) {
          if (AQHREACT_DataObject_GetDataType(dataObject)==AQHREACT_DATAOBJECTTYPE_DOUBLE) {
            double d;

            d=AQHREACT_DataObject_GetDoubleData(dataObject);
            if (numHandledInputs==0)
              value=d;
            else
              value=(d>value)?d:value;
            numHandledInputs++;
          }
          else {
            DBG_ERROR(NULL, "Data at input slot \"%s\" isn't DOUBLE, ignoring", portName?portName:"<unnamed>");
          }
        }
        else {
          DBG_ERROR(NULL, "Data at input slot \"%s\" has no current data, ignoring", portName?portName:"<unnamed>");
        }
        port=AQHREACT_Port_List_Next(port);
      }

      AQHREACT_Unit_ClearChangeFlagsInUnitAndInputPorts(unit);
      if (numHandledInputs) {
        return _outputResult(unit, outputPort, value);
      }
    }
  }

  return 0;
}



