#include "../thread/thread.hpp"
#include <stdint.h>
#include <boost/signals2.hpp>

#include <map>

// combiner which perfoms a kind of and function on all signal returns, returns true if no signals connected
class ButtonTimer : protected Thread
{
    public:
        ButtonTimer(uint32_t shortpress_min_ms, uint32_t longpress_ms);
        ~ButtonTimer();
        
        void RegisterPress(const uint8_t keycode);
        void RegisterRelease(const uint8_t keycode);
        void CancelPress(const uint8_t keycode);
        
        boost::signals2::signal<void (const uint8_t keycode)> onShortPress;
        boost::signals2::signal<void (const uint8_t keycode)> onLongPress;
        boost::signals2::signal<bool (const uint8_t keycode)> onValidatePress;
    
    protected:
        virtual void ThreadLoop(void);
    
    private:
        boost::signals2::connection onThreadErrorConnection;
        uint32_t longpressTime;
        uint32_t shortpressMinTime;
        std::map<const uint8_t, uint64_t> pressRegistry;

        bool eventLock;
        static int64_t now_ms(void);

};

