package com.canoo.dolphin.icos.poc.platform;

import org.opendolphin.core.server.ServerDolphin;
import org.opendolphin.core.server.action.DolphinServerAction;
import org.opendolphin.core.server.comm.ActionRegistry;
import org.reflections.Reflections;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service
public class DolphinCommandRepository {

    @Value("${dolphin.commands.basePackage:com.canoo}")
    private String mainPackage;

    @Inject
    private ApplicationContext applicationContext;

    private List<Class<AbstractDolphinCommand>> commandClasses;

    @PostConstruct
    private void init() {
        //Alle DolphinCommands suchen
        Reflections reflections = new Reflections(mainPackage);
        Set<Class<?>> annotated = reflections.getTypesAnnotatedWith(DolphinCommand.class);
        commandClasses = new ArrayList<>();
        annotated.stream().filter(AbstractDolphinCommand.class::isAssignableFrom).forEach(c -> commandClasses.add((Class<AbstractDolphinCommand>) c));
    }

    public void initCommandsForDolphin(ServerDolphin dolphin) {
        dolphin.register(new DolphinServerAction() {
            @Override
            public void registerIn(ActionRegistry registry) {
                //Alle registrieren
                for (Class<AbstractDolphinCommand> commandClass : commandClasses) {
                    AbstractDolphinCommand handler = applicationContext.getAutowireCapableBeanFactory().createBean(commandClass);
                    handler.setDolphin(dolphin);

                    DolphinCommand commandAnnotation = handler.getClass().getAnnotation(DolphinCommand.class);
                    registry.register(commandAnnotation.value(), (command, list) -> {
                        try {
                            handler.action();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    });
                }
            }
        });
    }

    public List<Class<AbstractDolphinCommand>> getCommandClasses() {
        return commandClasses;
    }
}

@@ -39,16 +39,58 @@
 tform;%0A%0A
+import org.opendolphin.core.comm.Command;%0A
 import o
@@ -250,16 +250,72 @@
 gistry;%0A
+import org.opendolphin.core.server.comm.CommandHandler;%0A
 import o
@@ -643,16 +643,89 @@
 til.Set;
+%0Aimport java.util.function.Consumer;%0Aimport java.util.function.Predicate;
 %0A%0A@Servi
@@ -1302,16 +1302,137 @@
 .filter(
+new Predicate%3CClass%3C?%3E%3E() %7B%0A            @Override%0A            public boolean test(Class%3C?%3E cls) %7B%0A                return 
 Abstract
@@ -1455,10 +1455,9 @@
 lass
-::
+.
 isAs
@@ -1472,22 +1472,159 @@
 From
-).forEach(c -%3E
+(cls);%0A            %7D%0A        %7D).forEach(new Consumer%3CClass%3C?%3E%3E() %7B%0A            @Override%0A            public void accept(Class%3C?%3E cls) %7B%0A               
  com
@@ -1672,17 +1672,44 @@
 mand%3E) c
-)
+ls);%0A            %7D%0A        %7D
 );%0A    %7D
@@ -1749,16 +1749,22 @@
 Dolphin(
+final 
 ServerDo
@@ -2057,16 +2057,22 @@
         
+final 
 Abstract
@@ -2403,17 +2403,131 @@
 (), 
-(
+new CommandHandler() %7B%0A                        @Override%0A                        public void handleCommand(Command 
 command,
  lis
@@ -2526,20 +2526,26 @@
 and,
+ List
  list) 
--%3E %7B%0A
+%7B%0A    
     
@@ -2590,32 +2590,36 @@
                 
+    
 handler.action()
@@ -2616,24 +2616,28 @@
 r.action();%0A
+    
             
@@ -2700,16 +2700,20 @@
         
+    
 e.printS
@@ -2721,24 +2721,54 @@
 ackTrace();%0A
+                            %7D%0A
             

Fix for broken example