#include "trace/trace.h"
#include "touchgfxtask.h"
#include "factory.h"
#include "interface/buttonscontrollercallbackprovider.h"

namespace app {

ButtonEventsLogger Factory::buttonEventLogger_;

Factory::Factory() {

}

// static
void Factory::initialize() {
    Trace::initialize();



#if defined(TOUCHGFX_ENABLED) && (TOUCHGFX_ENABLED != 0)
    getTouchGfxTask().initialize(_buttonEventsHandler);
#endif
}

// static
void Factory::build() {
    Trace::out("Factory: Creating app components...");

    // Start SM of ButtonEventLogger & ButtonsController
    buttonEventLogger_.startBehavior();
    ButtonsController::getInstance()->startBehavior();

    // Register CallBack of ButtonController with himself for the provider and onButtonChangeState for the CallBack Method
    ButtonsController::getInstance()->registerCallback (
    		ButtonEventsHandler::getInstance(),
			(interface::ButtonsControllerCallbackProvider::CallbackMethod) &ButtonEventsHandler::onButtonChangeState
			);

    // Subscribe ButtonEventLogger to ButtonEventsHandler & start SM of ButtonEventsHandler
    ButtonEventsHandler::getInstance()->subscribe(&buttonEventLogger_);
    ButtonEventsHandler::getInstance()->startBehavior();


#if defined(TOUCHGFX_ENABLED) && (TOUCHGFX_ENABLED != 0)
    getTouchGfxTask().start();
#endif
}

#if defined(TOUCHGFX_ENABLED) && (TOUCHGFX_ENABLED != 0)
// static
TouchGfxTask & Factory::getTouchGfxTask() {
    static TouchGfxTask touchGfxTask;
    return touchGfxTask;
}
#endif // TOUCHGFX_ENABLED

} /* namespace app */

void Factory_initialize() {
    app::Factory::initialize();
}

void Factory_build() {
    app::Factory::build();
}
