#ifndef APP_H
#define APP_H

#include "types.h"
#include "Addon.h"
#include "common/timer.h"

class DirectionSwitch;
class EnclosureSensor;
class OperationModeSwitch;
class Power;
class PPSerial;
class ModbusBridge;
class OmronPID;
class OmronVFD;
class TemperatureController;
class CRelays;
class H7PIDController;
class Communication;

class App : public Addon
{

public:
    App();

    TemperatureController *mTC;
    H7PIDController *h7PID;
    Communication *com;
    
    Addon *byId(short id);

    short setup();
    short loop();
    short debug();
    short info();
    short ok();


    void loop_normal();
    void loop_addons();
    void setup_addons();
    ushort numByFlag(ushort flag);
    void debug_mode_loop();
    
    Vector<Addon *> addons;

    // bridge
    short setFlag(ushort addonId, ushort flag);

#ifdef HAS_STATES
    short appState(short nop = 0);
    String state();
#endif

    millis_t comTS;
    millis_t loopTS;
    millis_t wait;
    millis_t waitTS;
    Timer<10, millis> timer; // 10 concurrent tasks, using micros as resolution

    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);

private:
#ifdef MEARSURE_PERFORMANCE
    millis_t addonLoopTime;
    millis_t bridgeLoopTime;
    millis_t printPerfTS;
#endif

    millis_t debugTS;
};

#endif