#ifndef COMPONENT_H
#define COMPONENT_H


#include "common/enums.h"
#include "common/constants.h"
#include "common/macros.h"
#include "common/error_codes.h"

typedef int byte;

class Component
{
public:
    
    static const int COMPONENT_DEFAULT = 1 << OBJECT_RUN_FLAGS::E_OF_LOOP | 1 << OBJECT_RUN_FLAGS::E_OF_INFO | 1 << OBJECT_RUN_FLAGS::E_OF_SETUP;
    static const int COMPONENT_NO_ID = 0;

    Component() : id(0), flags(OBJECT_RUN_FLAGS::E_OF_NONE) {}
    
    ~Component() {
        owner = nullptr;
    }

    virtual short destroy() { return E_OK; }
    virtual short setup() { return E_OK; }
    virtual short loop() { return E_OK; }

    bool hasFlag(byte flag)
    {
        return TEST(flags, flag);
    }
    void setFlag(byte flag)
    {
        SBI(flags, flag);
    }
    void clearFlag(byte flag)
    {
        CBI(flags, flag);
    }

    void enable(){
        clearFlag(OBJECT_RUN_FLAGS::E_OF_DISABLED);
    }
    void disable(){
        setFlag(OBJECT_RUN_FLAGS::E_OF_DISABLED);
    }
    bool enabled(){
        return !hasFlag(OBJECT_RUN_FLAGS::E_OF_DISABLED);
    }
    
    Component *owner;
    const short id;
    byte flags;
    millis_t now;

    
};

typedef short (Component::*ComponentVarArgsFnPtr)( ...);
typedef short (Component::*ComponentFnPtr)(short, short);



#endif
