/*
 * buttoneventslogger.cpp
 *
 *  Created on: Nov 7, 2023
 *      Author: remi.heredero
 */

#include <app/buttoneventslogger.h>
#include "trace/trace.h"
#include "ButtonEvent.h"

ButtonEventsLogger::ButtonEventsLogger() {
	curentState_ = state::initial;
}

void ButtonEventsLogger::onButtonShortPressed(ButtonIndex buttonIndex) {
	GEN(ButtonEvent(event::evButtonShortPressed, this, buttonIndex));
}

void ButtonEventsLogger::onButtonLongPressed(ButtonIndex buttonIndex) {
	GEN(ButtonEvent(event::evButtonLongPressed, this, buttonIndex));
}

XFEventStatus ButtonEventsLogger::processEvent() {
	eEventStatus eventStatus = XFEventStatus::NotConsumed;

	ButtonEvent* ev = (ButtonEvent*)getCurrentEvent();
	XFEvent::XFEventType evType = ev->getEventType();
	int evid = ev->getId();

	oldState_ = curentState_;
	changeXFState = false;



	switch (curentState_) { //on state
		case state::initial:
			if(evType == XFEvent::Initial) {
				curentState_ = state::waitButtonPressed;
				changeXFState = true;
				eventStatus = XFEventStatus::Consumed;
			}
			break;

		case state::waitButtonPressed:
			if(evid == event::evButtonShortPressed){
					Trace::out("ButtonEventLogger: Button %d short pressed", ev->getButtonId());
					curentState_ = state::waitButtonPressed;
					changeXFState = true;
					eventStatus = XFEventStatus::Consumed;
			}
			if(evid == event::evButtonLongPressed){
					Trace::out("ButtonEventLogger: Button %d long pressed", ev->getButtonId());
					curentState_ = state::waitButtonPressed;
					changeXFState = true;
					eventStatus = XFEventStatus::Consumed;
			}
			break;
	}

	if(changeXFState) {
		switch (oldState_) { // onExit
			case state::initial:
				break;

			case state::waitButtonPressed:
				break;
		}

		switch (curentState_) { // onEntry
			case state::initial:
				break;

			case state::waitButtonPressed:
				break;

		}
		changeXFState = false;
	}
	return eventStatus;
}

