#ifndef RELAY_H
#define RELAY_H

#include <ArduinoLog.h>
#include "config.h"
#include <osr-base.h>
#include <App.h>
#include <Addon.h>
#include <Component.h>
#include "types.h"
#include "enums.h"
#include <Arduino_MachineControl.h>

using namespace machinecontrol;

class Relay : public Component
{

public:
    Relay(Component *owner, short _pin) : Component("Relay", 0, Component::COMPONENT_DEFAULT, owner),
                                          pin(_pin)
    {
        setFlag(OBJECT_RUN_FLAGS::E_OF_DEBUG);
        _c = 0;
    }

    int runFoo()
    {
        Log.verboseln("Relay Value %f", value);
        return E_OK;
    }

    short setup()
    {
        digital_outputs.setLatch();
        // Uncomment this line to set over current behavior of all
        // channels to auto retry mode instead of latch mode:
        //digital_outputs.setRetry();
        digital_outputs.set(pin, LOW);
        return E_OK;
    }

    short debug()
    {
        return E_OK;
    }

    short _set(int _value)
    {
        value = _value;
        if(_value == 1)
        {
            digital_outputs.set(pin,HIGH);
            Log.verbose(F("Relay:set pin=%d value=%d" CR), pin, _value);
        }
        else
        {
            digital_outputs.set(pin,LOW);
            Log.verbose(F("Relay:set pin=%d value=%d" CR), pin, _value);
        }
        //digital_outputs.set(pin, _value);
        return E_OK;
    }

    int pin;
    int value;
    int _c;
    
    short onRegisterMethods(Bridge *bridge)
    {
        //Log.verbose(F("* Relay:onRegisterMethods *" CR));
        //bridge->registerMemberFunction(COMPONENT_KEY_RELAY_1, this, C_STR("_set"), (ComponentFnPtr)&Relay::_set);
        return E_OK;
    }
};

#endif
