package pl.mkrystek.mkbot.task;

import java.util.List;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pl.mkrystek.mkbot.window.SkypeWindow;

public class TaskExecutionEngine {

    private static final Logger LOGGER = LoggerFactory.getLogger(TaskExecutionEngine.class);

    private final ScheduledExecutorService scheduler;
    private final SkypeWindow skypeWindow;
    private List<ReplyTask> replyTasks;
    private List<ScheduledTask> scheduledTasks;

    public TaskExecutionEngine(ScheduledExecutorService scheduler, SkypeWindow skypeWindow) {
        this.skypeWindow = skypeWindow;
        this.scheduler = scheduler;
    }

    public void init(List<ReplyTask> replyTasks, List<ScheduledTask> scheduledTasks) {
        this.replyTasks = replyTasks;
        this.scheduledTasks = scheduledTasks;
    }

    public void start() {
        scheduler.scheduleAtFixedRate(() -> skypeWindow.getNewMessages().forEach(skypeMessage -> replyTasks.forEach(replyTask -> {
            if (replyTask.checkIfApplies(skypeMessage)) {
                LOGGER.debug("Writing message on skype: {}", replyTask.performAction(skypeMessage));
            }
        })), 0, 1, TimeUnit.SECONDS);
    }

    public void shutdown() {
        scheduler.shutdownNow();
    }
}

@@ -1127,24 +1127,94 @@
 Message)) %7B%0A
+                String reply = replyTask.performAction(skypeMessage);%0A
             
@@ -1271,39 +1271,51 @@
 eply
-Task.performAction(skyp
+);%0A                skypeWindow.writ
 eMessage
 ));%0A
@@ -1310,17 +1310,22 @@
 eMessage
-)
+(reply
 );%0A     
@@ -1350,16 +1350,18 @@
 )), 0, 1
+00
 , TimeUn
@@ -1363,16 +1363,21 @@
 imeUnit.
+MILLI
 SECONDS)

Write task output to skype window