/**
 * @author Rémi Heredero (remi@heredero.ch)
 * @version. 1.0.0
 * @date 2023-06-15
 */

#ifndef CLICKHANDLER_H
#define	CLICKHANDLER_H

#include "../xf/xf.h"
#include "../board/button/button.h"

#define CH_CLICK_TIMER 250

typedef enum {
    STCH_INIT,
    STCH_WAIT,
    STCH_LONG_CLICK,
    STCH_SINGLE_CLICK,
    STCH_DOUBLE_CLICK
} CLICK_HANDLER_STATES;

typedef enum {
    evCHinit = 100,
    evCHtimer,
    evCHpbPress,
    evCHpbRelease
} CLICK_HANDLER_EVENTS;

typedef void (*fClickHandlerCallBack)(void*);
typedef struct {
    fClickHandlerCallBack fCallBack;
    void* param;
} clickHandlerCallBack;

typedef struct {
    BUTTON* button; // The button connected to the clickHandler
    CLICK_HANDLER_STATES state; // The actual state
    clickHandlerCallBack longClick; // Callback for longClick
    clickHandlerCallBack singleClick; // Callback for singleClick
    clickHandlerCallBack doubleClick; // Callback for doubleClick
}CLICK_HANDLER;

/**
 * Initialize the clickHandler
 * @param me the clickHandler itself
 * @param b the button to connect on the clickHandler
 */
void CLICK_HANDLER_init(CLICK_HANDLER* me, BUTTON* b);

/**
 * Define a callback for CLICKHANDLER
 * @param f  callback function for CLICKHANDLER
 * @param param callback parameter for the function
 * @return the callback struct
 */
clickHandlerCallBack CLICK_HANDLER_defineCallBack(fClickHandlerCallBack f, void* param);

/**
 * Start state machine of the CLICKHANDLER
 * @param me the clickHandler itself
 */
void CLICK_HANDLER_startBehaviour(CLICK_HANDLER* me);

/**
 * Set Callback eventfunction for long click
 * @param me the clickHandler itself
 * @param callBack callback function when the click handler have a long click
 */
void CLICK_HANDLER_setLongClickCallback(CLICK_HANDLER* me, clickHandlerCallBack callBack);

/**
 * Set Callback event function for single click
 * @param me the clickHandler itself
 * @param callBack callback function when the click handler have a single click
 */
void CLICK_HANDLER_setSingleClickCallback(CLICK_HANDLER* me, clickHandlerCallBack callBack);

/**
 * Set Callback evenet function for double click
 * @param me the clickHandler itself
 * @param callBack callback function when the click handler have a double click
 */
void CLICK_HANDLER_setDoubleClickCallback(CLICK_HANDLER* me, clickHandlerCallBack callBack);

/**
 * State machine of the CLICKHANDLER
 * @param ev event to process on the state machine
 */
bool CLICK_HANDLER_processEvent(Event* ev);


#endif	/* CLICKHANDLER_H */

