package org.wildfly.swarm.config.generator.generator;

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

/**
 * @author Bob McWhirter
 */
public class NameFixer {

    public interface Fix {
        String fix(String input);
    }

    public static class SimpleFix implements Fix {

        private final String key;

        public SimpleFix(String key) {
            this.key = key;
        }

        @Override
        public String fix(String input) {
            StringBuffer output = new StringBuffer();

            int cur = 0;

            while ( cur < input.length() ) {
                int loc = input.indexOf( this.key, cur );
                if ( loc < 0 ) {
                    System.err.println( "did not find: " + this.key );
                    output.append( input.substring( cur ) );
                    break;
                } else {
                    output.append(input.substring(cur, loc));

                    int nextCharLoc = loc + this.key.length();
                    if ( nextCharLoc < input.length() && Character.isUpperCase(input.charAt( nextCharLoc )) ) {
                        System.err.println("found " + this.key + " at " + loc);
                        output.append(this.key.toUpperCase());
                    } else {
                        output.append(this.key);
                    }
                    cur = loc + this.key.length();

                }
            }
            /*
            return input.replace(this.key, this.key.toUpperCase());
            int loc = input.indexOf( this.key );
            int nextCharLoc = loc + this.key.length() + 1;
            if ( input.length() > nextCharLoc ) {
                char nextChar = input.charAt( nextCharLoc );
            } else {

            }
            */

            System.err.println( input + " to " + output );

            return output.toString();
        }
    }

    public static class CustomFix implements Fix {

        private final String key;

        private final String replacement;

        public CustomFix(String key, String replacement) {
            this.key = key;
            this.replacement = replacement;
        }

        @Override
        public String fix(String input) {
            return input.replace(this.key, this.replacement);
        }
    }

    private static List<Fix> FIXES = new ArrayList<Fix>();

    private static void simpleFix(String s) {
        FIXES.add(new SimpleFix(s));
    }

    private static void customFix(String s, String r) {
        FIXES.add(new CustomFix(s, r));
    }

    static {
        customFix("Activemq", "ActiveMQ");
        simpleFix("Acl");
        simpleFix("Ajp");
        simpleFix("Ee");
        simpleFix("Ejb");
        simpleFix("Ha");
        simpleFix("Http");
        simpleFix("Io");
        simpleFix("Iiop");
        simpleFix("Imap");
        customFix("InVm", "InVM");
        simpleFix("Jaxrs");
        simpleFix("Jca");
        simpleFix("Jdbc");
        customFix("Jgroups", "JGroups");
        simpleFix("Jms");
        simpleFix("Jmx");
        simpleFix("Jpa");
        simpleFix("Jsf");
        simpleFix("Jsp");
        simpleFix("Jsse");
        simpleFix("Mdb");
        simpleFix("Msc");
        simpleFix("Pop3");
        simpleFix("Sasl");
        simpleFix("Smtp");
        simpleFix("Xa");
    }

    public static String fix(String input) {
        for (Fix s : FIXES) {
            input = s.fix(input);
        }

        return input;
    }
}

@@ -673,79 +673,8 @@
 ) %7B%0A
-                    System.err.println( %22did not find: %22 + this.key );%0A
     
@@ -726,24 +726,24 @@
 g( cur ) );%0A
+
             
@@ -1024,88 +1024,8 @@
 ) %7B%0A
-                        System.err.println(%22found %22 + this.key + %22 at %22 + loc);%0A
     
@@ -1245,32 +1245,32 @@
               %7D%0A
+
             %7D%0A  
@@ -1270,421 +1270,8 @@
    %7D
-%0A            /*%0A            return input.replace(this.key, this.key.toUpperCase());%0A            int loc = input.indexOf( this.key );%0A            int nextCharLoc = loc + this.key.length() + 1;%0A            if ( input.length() %3E nextCharLoc ) %7B%0A                char nextChar = input.charAt( nextCharLoc );%0A            %7D else %7B%0A%0A            %7D%0A            */%0A%0A            System.err.println( input + %22 to %22 + output );
 %0A%0A  

Remove debug printlns.