/****************************************************************************
 * 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     100
#define AQHOMEREACT_UNIT_LOGICAL_INSLOT_AUTOINPUT 101

#define AQHOMEREACT_UNIT_LOGICAL_OUTSLOT_RESULT   0



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

static AQHREACT_UNIT *_unitLogical_new(AQH_OBJECT *aqh);
static int _outputResult(AQHREACT_UNIT *unit, AQHREACT_PORT *port, int result);
static int _cbProcessOr(AQHREACT_UNIT *unit);
static int _cbProcessAnd(AQHREACT_UNIT *unit);
static int _cbProcessXor(AQHREACT_UNIT *unit);
static int _cbProcessInvert(AQHREACT_UNIT *unit);



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


AQHREACT_UNIT *AqHomeReact_UnitOr_new(AQH_OBJECT *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_UnitNor_new(AQH_OBJECT *aqh)
{
  AQHREACT_UNIT *unit;

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



AQHREACT_UNIT *AqHomeReact_UnitAnd_new(AQH_OBJECT *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_UnitNand_new(AQH_OBJECT *aqh)
{
  AQHREACT_UNIT *unit;

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



AQHREACT_UNIT *AqHomeReact_UnitXor_new(AQH_OBJECT *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 *AqHomeReact_UnitInvert_new(AQH_OBJECT *aqh)
{
  AQHREACT_UNIT *unit;

  unit=_unitLogical_new(aqh);
  AQHREACT_Unit_SetTypeName(unit, "invert");
  AQHREACT_Unit_SetDescription(unit, "Logical invert input");
  AQHREACT_Unit_SetProcessFn(unit, _cbProcessInvert);
  return unit;
}




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

  unit=AQHREACT_Unit_new(aqh);

  AQHREACT_Unit_SetGpInt(unit, -1);
  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 _outputResult(AQHREACT_UNIT *unit, AQHREACT_PORT *port, int result)
{
  int currentState;

  currentState=AQHREACT_Unit_GetGpInt(unit);
  if (AQHREACT_Unit_GetFlags(unit) & AQHREACT_UNIT_FLAGS_INVERT)
    result^=1;
  if (currentState!=result) {
    DBG_INFO(NULL, "%s: Changing result to %d", AQHREACT_Unit_GetId(unit), result);
    AQHREACT_Unit_OutputDoubleData(unit, port, result?1.0:0.0);
    AQHREACT_Unit_SetGpInt(unit, result);
    return 1; /* we changed something */
  }
  return 0; /* nothing changed */
}



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

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

  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 *port;
      int result=1;
      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 (!(d>0.0))
              result=0;
            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\" (%d) has no current data, assuming 0",
                    portName?portName:"<unnamed>", AQHREACT_Port_GetIdForUnit(port));
          result=0;
        }
        port=AQHREACT_Port_List_Next(port);
      }

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

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

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

  return 0;
}



int _cbProcessInvert(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 *port;
      int result=1;

      port=AQHREACT_Port_List_First(AQHREACT_Unit_GetInputPortList(unit));
      if(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; /* invert */
            AQHREACT_Unit_ClearChangeFlagsInUnitAndInputPorts(unit);
            return _outputResult(unit, outputPort, result);
          }
          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>");
        }
      }
    }
  }

  return 0;
}



