#ifndef PHAPP_H
#define PHAPP_H
#define MAX_ADDONS 5

#include "debug_config.h"
#include "config.h"

#include <xmath.h>
#include <osr-base.h>
#include <App.h>
#include <Addon.h>
#include <Component.h>
#include <Bridge.h>
#include "SerialMessage.h"
#include "enums.h"
#include <ArduinoLog.h>

class POT;
class Relay;
class ModbusBridge;

// test <<COMPONENT_KEY;CALL_TYPE;FLAGS;1:foo:2300:5>>

// test app::foo <<1;2;64;1:foo:2300:5>>
// test relay::_set <<51;2;64;1:set:1:0>>

class PHApp : public App
{
public:
    SerialMessage *com_serial;
    POT *pot1;
    Relay *relay1;
    ModbusBridge *modbusBridge;

    PHApp() : App()
    {
    }

    short foo(lint arg1, lint arg2)
    {
        Log.verbose(F("* App:foo arg1=%d arg2=%d = %d" CR), arg1, arg2, arg1 + arg2);
        return E_FATAL;
    }

    millis_t comTS;
    millis_t loopTS;
    millis_t wait;
    millis_t waitTS;
    Timer<10, millis> timer; // 10 concurrent tasks, using micros as resolution

    // states
    enum CONTROLLER_STATE
    {
        E_CS_OK = 0,
        E_CS_ERROR = 10
    };

    enum APP_STATE
    {
        RESET = 0,
        EXTRUDING = 1,
        STANDBY = 2,
        ERROR = 5
    };

    short _state;
    short _cstate;
    short _error;
    short getLastError(short val = 0)
    {
        return _error;
    }
    short setLastError(short val = 0);
    short setAppState(short newState);
    short getAppState(short val);

    // component methods
    virtual short setup();
    // virtual short loop();
    virtual short onRegisterMethods(Bridge *bridge);
};

#endif // PHAPP_H