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

#include <gwenhywfar/debug.h>



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

#define AQHOMEREACT_UNIT_LOGICAL_INSLOT_INPUT     0
#define AQHOMEREACT_UNIT_LOGICAL_INSLOT_AUTOINPUT 100

#define AQHOMEREACT_UNIT_LOGICAL_OUTSLOT_RESULT   0



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

static AQHREACT_UNIT *_unitLogical_new(AQHOME_REACT *aqh);
static int _cbProcessOr(AQHREACT_UNIT *unit);
static int _cbProcessAnd(AQHREACT_UNIT *unit);
static int _cbProcessXor(AQHREACT_UNIT *unit);



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


AQHREACT_UNIT *AqHomeReact_UnitOr_new(AQHOME_REACT *aqh)
{
  AQHREACT_UNIT *unit;

  unit=_unitLogical_new(aqh);
  AQHREACT_Unit_SetTypeName(unit, "or");
  AQHREACT_Unit_SetDescription(unit, "Logical OR inputs");
  AQHREACT_Unit_SetProcessFn(unit, _cbProcessOr);
  return unit;
}



AQHREACT_UNIT *AqHomeReact_UnitAnd_new(AQHOME_REACT *aqh)
{
  AQHREACT_UNIT *unit;

  unit=_unitLogical_new(aqh);
  AQHREACT_Unit_SetTypeName(unit, "and");
  AQHREACT_Unit_SetDescription(unit, "Logical AND inputs");
  AQHREACT_Unit_SetProcessFn(unit, _cbProcessAnd);
  return unit;
}



AQHREACT_UNIT *AqHomeReact_UnitXor_new(AQHOME_REACT *aqh)
{
  AQHREACT_UNIT *unit;

  unit=_unitLogical_new(aqh);
  AQHREACT_Unit_SetTypeName(unit, "xor");
  AQHREACT_Unit_SetDescription(unit, "Logical XOR inputs");
  AQHREACT_Unit_SetProcessFn(unit, _cbProcessXor);
  return unit;
}




AQHREACT_UNIT *_unitLogical_new(AQHOME_REACT *aqh)
{
  AQHREACT_UNIT *unit;
  AQHREACT_PORT *port;

  unit=AQHREACT_Unit_new(aqh);
  AQHREACT_Unit_SetTypeName(unit, "or");
  AQHREACT_Unit_SetDescription(unit, "Logical OR inputs");

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

  port=AQHREACT_Port_new();
  AQHREACT_Port_SetName(port, "output");
  AQHREACT_Port_SetIdForUnit(port, AQHOMEREACT_UNIT_LOGICAL_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_LOGICAL_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 _cbProcessOr(AQHREACT_UNIT *unit)
{
  if (unit && AQHREACT_Unit_InputHasChanged(unit)) {
    AQHREACT_PORT *outputPort;

    outputPort=AQHREACT_Unit_GetOutputPortByIdForUnit(unit, AQHOMEREACT_UNIT_LOGICAL_OUTSLOT_RESULT);
    if (outputPort) {
      AQHREACT_PORT_LIST *portList;

      portList=AQHREACT_Unit_GetInputPortList(unit);
      if (portList) {
        const AQHREACT_PORT *port;
        int result=0;

        port=AQHREACT_Port_List_First(portList);
        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 (d>0.0)
                result|=1;
            }
            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 not current data, ignoring", portName?portName:"<unnamed>");
          }
          port=AQHREACT_Port_List_Next(port);
        }

        AQHREACT_Unit_ClearChangeFlagsInUnitAndInputPorts(unit);
        AQHREACT_Unit_OutputDoubleData(unit, outputPort, result?1.0:0.0);
        return 1;
      }
    }
  }

  return 0;
}



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

    outputPort=AQHREACT_Unit_GetOutputPortByIdForUnit(unit, AQHOMEREACT_UNIT_LOGICAL_OUTSLOT_RESULT);
    if (outputPort) {
      AQHREACT_PORT_LIST *portList;

      portList=AQHREACT_Unit_GetInputPortList(unit);
      if (portList) {
        AQHREACT_PORT *port;
        int result=1;

        port=AQHREACT_Port_List_First(portList);
        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 (!(d>0.0))
                result=0;
            }
            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 not current data, ignoring", portName?portName:"<unnamed>");
          }
          port=AQHREACT_Port_List_Next(port);
        }

        AQHREACT_Unit_ClearChangeFlagsInUnitAndInputPorts(unit);
        AQHREACT_Unit_OutputDoubleData(unit, port, result?1.0:0.0);
        return 1;
      }
    }
  }

  return 0;
}



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

    outputPort=AQHREACT_Unit_GetOutputPortByIdForUnit(unit, AQHOMEREACT_UNIT_LOGICAL_OUTSLOT_RESULT);
    if (outputPort) {
      AQHREACT_PORT_LIST *portList;

      portList=AQHREACT_Unit_GetInputPortList(unit);
      if (portList) {
        AQHREACT_PORT *port;
        int result=0;

        port=AQHREACT_Port_List_First(portList);
        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 (d>0.0)
                result^=1; /*only xor when >0.0, otherwise no change (x XOR 0 is still x) */
            }
            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 not current data, ignoring", portName?portName:"<unnamed>");
          }
          port=AQHREACT_Port_List_Next(port);
        }

        AQHREACT_Unit_ClearChangeFlagsInUnitAndInputPorts(unit);
        AQHREACT_Unit_OutputDoubleData(unit, port, result?1.0:0.0);
        return 1;
      }
    }
  }

  return 0;
}



