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

import java.util.List;

import org.jboss.dmr.ModelNode;
import org.jboss.forge.roaster.Roaster;
import org.jboss.forge.roaster.model.JavaType;
import org.jboss.forge.roaster.model.source.EnumConstantSource;
import org.jboss.forge.roaster.model.source.JavaEnumSource;
import org.jboss.forge.roaster.model.source.MethodSource;

import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.ALLOWED;

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

    public JavaEnumSource create(ClassIndex index, EnumPlan plan) {
        final JavaEnumSource enumType = Roaster.create(JavaEnumSource.class)
                .setName(plan.getClassName())
                .setPublic();

        if ( plan.getPackageName() != null ) {
            enumType.setPackage(plan.getPackageName());
        }

        // Create a field to indicate the value the model expects
        enumType.addProperty(String.class, "allowedValue")
                .getAccessor()
                .getJavaDoc()
                .setText("Returns the allowed value for the management model.")
                .addTagValue("@return", "the allowed model value");

        final MethodSource<JavaEnumSource> constructor = enumType.addMethod()
                .setConstructor(true);
        constructor.addParameter(String.class, "allowedValue");
        constructor.setBody("this.allowedValue = allowedValue;");

        // Override the toString() to return the allowedValue so it can be used to determine the correct enum to use
        enumType.addMethod()
                .setName("toString")
                .setReturnType(String.class)
                .setPublic()
                .setBody("return allowedValue;")
                .addAnnotation(Override.class);


        List<ModelNode> allowedValues = plan.getAllowedValues();

        // For each allowed value add an enum constant
        allowedValues.forEach(value -> {
            final String v = value.asString();
            // Replace - and . with _ and uppercase each character
            final StringBuilder sb = new StringBuilder();
            for (char c : v.toCharArray()) {
                switch (c) {
                    case '-':
                    case '.': {
                        sb.append('_');
                        break;
                    }
                    default:
                        sb.append(Character.toUpperCase(c));
                }
            }
            final EnumConstantSource constantSource = enumType.addEnumConstant(sb.toString());
            constantSource.setConstructorArguments("\"" + value.asString() + "\"");
        });

        return enumType;
    }

}

@@ -148,55 +148,8 @@
 er;%0A
-import org.jboss.forge.roaster.model.JavaType;%0A
 impo
@@ -331,95 +331,8 @@
 e;%0A%0A
-import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.ALLOWED;%0A%0A
 /**%0A
@@ -1993,24 +1993,60 @@
 gBuilder();%0A
+%0A            boolean first = true;%0A%0A
             
@@ -2240,16 +2240,523 @@
  break;%0A
+                    %7D%0A                    case '0':%0A                    case '1':%0A                    case '2':%0A                    case '3':%0A                    case '4':%0A                    case '5':%0A                    case '6':%0A                    case '7':%0A                    case '8':%0A                    case '9': %7B%0A                        if ( first ) %7B%0A                            sb.append( %22_%22 );%0A                        %7D%0A                        sb.append( c );%0A                        break;%0A%0A
         
@@ -2869,32 +2869,63 @@
               %7D%0A
+                first = false;%0A
             %7D%0A  
@@ -3001,16 +3001,42 @@
 onstant(
+fixSingleDigitsInEnumName(
 sb.toStr
@@ -3041,16 +3041,17 @@
 tring())
+)
 ;%0A      
@@ -3173,10 +3173,550 @@
 %0A    %7D%0A%0A
+    static String fixSingleDigitsInEnumName(String input) %7B%0A        return input.replaceAll(%22%5E_1_%22, %22ONE_%22 )%0A                .replaceAll( %22%5E_2_%22, %22TWO_%22)%0A                .replaceAll( %22%5E_3_%22, %22THREE_%22 )%0A                .replaceAll( %22%5E_4_%22, %22FOUR_%22 )%0A                .replaceAll( %22%5E_5_%22, %22FIVE_%22 )%0A                .replaceAll( %22%5E_6_%22, %22SIX_%22 )%0A                .replaceAll( %22%5E_7_%22, %22SEVEN_%22 )%0A                .replaceAll( %22%5E_8_%22, %22EIGHT_%22 )%0A                .replaceAll( %22%5E_9_%22, %22NINE_%22 )%0A                .replaceAll( %22%5E_10_%22, %22TEN_%22 );%0A    %7D%0A%0A
 %7D%0A

Fix enumeration generation for enums starting with a number.