package com.tngtech.jenkins.notification;

import com.tngtech.jenkins.notification.camel.CamelApplication;
import com.tngtech.jenkins.notification.endpoints.MissileEndpoint;
import com.tngtech.jenkins.notification.endpoints.TtsEndpoint;
import com.tngtech.jenkins.notification.model.Config;
import com.tngtech.missile.usb.MissileController;
import com.tngtech.missile.usb.MissileLauncher;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Bootstrap {

    public static void main(String[] args) throws Exception {

        Config config = new ConfigLoader().load();

        String command = args[0];
        if (args.length == 1) {
            switch (command) {
                case "fire":
                    getMissileController().fire();
                    break;
                case "ledOn":
                    getMissileController().ledOn();
                    break;
                case "ledOff":
                    getMissileController().ledOff();
                    break;
                case "stalk":
                    new CamelApplication(config).run();
                    break;
                default:
                    throw new IllegalArgumentException("Could not parse commandline");
            }
        }
        if (args.length >= 2) {
            switch (command) {
                case "shootAt":
                    MissileEndpoint missileEndpoint = new MissileEndpoint(config.getMissile());
                    List<String> culprits = new ArrayList<>();
                    culprits.addAll(Arrays.asList(args).subList(1, args.length));
                    missileEndpoint.shootAt(culprits);
                    break;
                case "say":
                    TtsEndpoint ttsEndpoint = new TtsEndpoint(config.getTts());
                    StringBuilder text = new StringBuilder("");
                    for (int i = 1; i < args.length; i++) {
                        text.append(" ").append(args[i]);
                    }
                    ttsEndpoint.say(text.toString());
                    break;
                default:
                    MissileLauncher.Command direction =
                            MissileLauncher.Command.valueOf(args[0].toUpperCase());
                    int time = Integer.parseInt(args[1]);
                    getMissileController().move(direction, time);
                    break;
            }
        }
    }

    private static MissileController getMissileController() throws Exception {
        return new MissileController(MissileLauncher.findMissileLauncher());
    }
}

@@ -381,16 +381,59 @@
 auncher;
+%0Aimport org.apache.commons.lang.ArrayUtils;
 %0A%0Aimport
@@ -1867,167 +1867,64 @@
     
+List%3C
 String
-Builder text = new StringBuilder(%22%22);%0A                    for (int i = 1; i %3C args.length; i++) %7B%0A                        text.append(%22 %22).append
+%3E words = new ArrayList%3C%3E(Arrays.asList
 (args
-%5Bi%5D
+)
 );%0A 
@@ -1934,33 +1934,48 @@
                 
-%7D
+words.remove(0);
 %0A               
@@ -1999,23 +1999,13 @@
 say(
-text.toString()
+words
 );%0A 

Fix say command line call