package de.fortunify;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.fortunify.api.FortuneDispatcherFactory;
import de.fortunify.api.FortuneGeneratorFactory;
import de.fortunify.api.FortuneSink;
import de.fortunify.api.FortuneSource;
import de.fortunify.spi.FortuneDispatcher;
import de.fortunify.spi.FortuneGenerator;

public class Main {
    
    private static final Logger logger = LoggerFactory.getLogger(Main.class.getSimpleName());

    private static final int MAX_CHARS_TWITTER = 140;

    private Main() {}
    
    public static void main(String[] args) {

        // TODO pass source and sink instead of hard code them here

        FortuneGenerator fortuneGenerator = FortuneGeneratorFactory.createFortuneGenerator(FortuneSource.SPOTIFY);
        FortuneDispatcher fortuneDispatcher = FortuneDispatcherFactory.createFortuneGenerator(FortuneSink.TWITTER);

        try {
            fortuneDispatcher.dispatchFortune(fortuneGenerator.generateFortune(MAX_CHARS_TWITTER));
        } catch (IOException e) {
            logger.error("Fortunify failed", e);
            System.exit(1);
        }

    }

}

@@ -43,16 +43,64 @@
 ception;
+%0Aimport java.util.HashSet;%0Aimport java.util.Set;
 %0A%0Aimport
@@ -432,20 +432,16 @@
  Main %7B%0A
-    
 %0A    pri
@@ -524,24 +524,74 @@
 leName());%0A%0A
+    private static final String DELIMITER = %22,%22;%0A%0A
     private 
@@ -616,24 +616,16 @@
 AX_CHARS
-_TWITTER
  = 140;%0A
@@ -649,253 +649,802 @@
 () %7B
-%7D
+%0A    %7D%0A
 %0A    
+/**
 %0A    
-public static void main(String%5B%5D args) %7B%0A
+ * @param args%0A     *            fist argument depicts the fortune source, second argument
 %0A     
+*
    
-// TODO pass source and sink instead of hard code them here%0A%0A        FortuneGenerator fortuneGenerator = FortuneGeneratorFactory.createFortuneGenerator(FortuneSource.SPOTIFY);%0A        
+         depicts the fortune sinks (comma-separated list)%0A     */%0A    public static void main(String%5B%5D args) %7B%0A%0A        FortuneGenerator fortuneGenerator;%0A        try %7B%0A            fortuneGenerator = Main.createFortuneGenerator(args%5B0%5D);%0A        %7D catch (IOException e) %7B%0A            logger.error(%22Fortunify failed - %22 + e.getMessage());%0A            System.exit(1);%0A            return;%0A        %7D%0A%0A        String fortune;%0A        try %7B%0A            fortune = fortuneGenerator.generateFortune(MAX_CHARS);%0A        %7D catch (IOException e) %7B%0A            logger.error(%22Fortunify failed - %22 + e.getMessage());%0A            System.exit(1);%0A            return;%0A        %7D%0A%0A        for (
 Fort
@@ -1475,18 +1475,29 @@
 patcher 
-= 
+: Main.create
 FortuneD
@@ -1509,195 +1509,1096 @@
 cher
-Factory.createFortuneGenerator(FortuneSink.TWITTER);%0A%0A        try %7B%0A            fortuneDispatcher.dispatchFortune(fortuneGenerator.generateFortune(MAX_CHARS_TWITTER));%0A        %7D catch (IO
+s(args%5B1%5D)) %7B%0A            try %7B%0A                fortuneDispatcher.dispatchFortune(fortune);%0A            %7D catch (IOException e) %7B%0A                // provide details about the dispatcher that failed%0A                logger.error(%22Failed to dispatch fortune - %22 + e.getMessage());%0A            %7D%0A        %7D%0A%0A    %7D%0A%0A    private static FortuneGenerator createFortuneGenerator(String sourceArg) throws IOException %7B%0A%0A        FortuneSource source;%0A        try %7B%0A            source = FortuneSource.valueOf(sourceArg.toUpperCase());%0A        %7D catch (IllegalArgumentException e) %7B%0A            throw new IOException(%22unknown fortune source: %22 + sourceArg);%0A        %7D%0A        return FortuneGeneratorFactory.createFortuneGenerator(source);%0A    %7D%0A%0A    private static Set%3CFortuneDispatcher%3E createFortuneDispatchers(String sinkArg) %7B%0A%0A        Set%3CFortuneDispatcher%3E fortuneDispatchers = new HashSet%3C%3E();%0A        for (String arg : sinkArg.split(DELIMITER)) %7B%0A            FortuneSink sink;%0A            try %7B%0A                sink = FortuneSink.valueOf(arg.toUpperCase());%0A            %7D catch (IllegalArgument
 Exce
@@ -2600,32 +2600,36 @@
 tException e) %7B%0A
+    
             logg
@@ -2643,68 +2643,245 @@
 r(%22F
-ortunify failed%22, e);%0A            System.exit(1);%0A        %7D%0A
+ailed to dispatch fortune - unknown fortune sink: %22 + arg);%0A                continue;%0A            %7D%0A            fortuneDispatchers.add(FortuneDispatcherFactory.createFortuneDispatcher(sink));%0A        %7D%0A%0A        return fortuneDispatchers;
 %0A   

set fortune source and sinks from vm args