package won.bot.framework.extensions.matcher;

import won.bot.framework.eventbot.EventListenerContext;
import won.bot.framework.eventbot.action.impl.LogAction;
import won.bot.framework.eventbot.action.impl.RandomDelayedAction;
import won.bot.framework.eventbot.action.impl.matcher.RegisterMatcherAction;
import won.bot.framework.eventbot.behaviour.BotBehaviour;
import won.bot.framework.eventbot.bus.EventBus;
import won.bot.framework.eventbot.event.impl.lifecycle.ActEvent;
import won.bot.framework.eventbot.event.impl.matcher.MatcherExtensionRegisterFailedEvent;
import won.bot.framework.eventbot.event.impl.matcher.MatcherExtensionRegisterSucceededEvent;
import won.bot.framework.eventbot.listener.impl.ActionOnEventListener;

import java.util.Optional;

public class MatcherBehaviour extends BotBehaviour {
    private final EventBus eventBus;
    private static final long retryInterval = 30000L; // TODO: let this be injected via config

    public MatcherBehaviour(EventListenerContext context) {
        super(context);
        eventBus = context.getEventBus();
    }

    public MatcherBehaviour(EventListenerContext context, String name) {
        super(context, name);
        eventBus = context.getEventBus();
    }

    protected void onActivate(Optional<Object> message) {
        // register as matcher on node
        // TODO: move RegisterMatcherAction to extension
        RegisterMatcherAction registerMatcher = new RegisterMatcherAction(context);
        subscribeWithAutoCleanup(ActEvent.class, new ActionOnEventListener(context, registerMatcher, 1));
        // retry in case of failed registering
        // TODO: move MatcherExtensionRegisterFailedEvent to extension
        // using a random delayed action because we don't have a non-random one
        RandomDelayedAction delayedRegistration = new RandomDelayedAction(context, retryInterval, retryInterval, 0L,
                        registerMatcher);
        subscribeWithAutoCleanup(MatcherExtensionRegisterFailedEvent.class,
                        new ActionOnEventListener(context, delayedRegistration));
        // optional action on successful registering
        // TODO: move MatcherExtensionRegisterSucceededEvent to extension
        LogAction afterRegistration = new LogAction(context, "Successfully registered as Matcher");
        subscribeWithAutoCleanup(MatcherExtensionRegisterSucceededEvent.class,
                        new ActionOnEventListener(context, afterRegistration));
    }

    public EventBus getEventBus() {
        return eventBus;
    }
}

@@ -857,15 +857,8 @@
 ate 
-static 
 fina
@@ -881,59 +881,107 @@
 rval
- = 30000L; // TODO: let this be injected via config
+;%0A%0A    public MatcherBehaviour(EventListenerContext context) %7B%0A        this(context, 30000L);%0A    %7D
 %0A%0A  
@@ -1026,32 +1026,52 @@
 rContext context
+, long retryInterval
 ) %7B%0A        supe
@@ -1116,32 +1116,76 @@
 .getEventBus();%0A
+        this.retryInterval = retryInterval;%0A
     %7D%0A%0A    publi
@@ -1244,24 +1244,161 @@
 ing name) %7B%0A
+        this(context, name, 30000L);%0A    %7D%0A%0A    public MatcherBehaviour(EventListenerContext context, String name, long retryInterval) %7B%0A
         supe
@@ -1453,24 +1453,68 @@
 EventBus();%0A
+        this.retryInterval = retryInterval;%0A
     %7D%0A%0A    p
@@ -2288,67 +2288,8 @@
 ass,
-%0A                        new ActionOnEventListener(context,
  del
@@ -2301,25 +2301,24 @@
 egistration)
-)
 ;%0A        //
@@ -2438,108 +2438,8 @@
 ion%0A
-        LogAction afterRegistration = new LogAction(context, %22Successfully registered as Matcher%22);%0A
     
@@ -2545,56 +2545,63 @@
 new 
+Log
 Action
-OnEventListener(context, afterRegistration
+(context, %22Successfully registered as Matcher%22
 ));%0A

Add matcherBehaviour constructor param retryInterval