package io.cloudslang.lang.compiler;

import org.apache.commons.lang.Validate;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;

/**
 * User: bancl
 * Date: 3/1/2016
 */
public enum Extension {
    SL("sl"),
    SL_YAML("sl.yaml"),
    SL_YML("sl.yml"),
    PROP_SL("prop.sl"),
    YAML("yaml"),
    YML("yml");

    private final String value;
    private static final String[] extensionValues = new String[values().length];
    static {
        initExtensionValues();
    }

    private static void initExtensionValues() {
        Extension[] extensions = values();
        for(int i = 0; i < extensions.length; i++)
            extensionValues[i] = extensions[i].getValue();
    }

    Extension(String value) {
        this.value = value;
    }

    public String getValue() {
        return value;
    }

    public static String[] getSlangFileExtensionValues() {
        return Arrays.copyOfRange(extensionValues, 0, 3);
    }

    public static Extension[] getSlangFileExtensions() {
        return Arrays.copyOfRange(values(), 0, 3);
    }

    public static String[] getPropertiesFileExtensionValues() {
        return Arrays.copyOfRange(extensionValues, 3, 4);
    }

    public static Extension[] getPropertiesFileExtensions() {
        return Arrays.copyOfRange(values(), 3, 4);
    }

    public static String[] getYamlFileExtensionValues() {
        return Arrays.copyOfRange(extensionValues, 4, extensionValues.length);
    }

    public static void validateSlangFileExtension(String fileName) {
        Extension fileExtension = Extension.findExtension(fileName);
        Extension[] slangFileExtensions = getSlangFileExtensions();
        validateFileExtension(fileName, fileExtension, slangFileExtensions);
    }

    public static void validatePropertiesFileExtension(String fileName) {
        Extension fileExtension = Extension.findExtension(fileName);
        Extension[] propertiesFileExtensions = getPropertiesFileExtensions();
        validateFileExtension(fileName, fileExtension, propertiesFileExtensions);
    }

    private static void validateFileExtension(String fileName, Extension fileExtension, Extension[] extensions) {
        boolean validFileExtension = false;
        for (Extension extension : extensions) {
            if (extension.equals(fileExtension)) validFileExtension = true;
        }
        String extensionsAsString =  Arrays.toString(extensions);
        Validate.isTrue(validFileExtension,
                "File: " + fileName + " must have one of the following extensions: " +
                        extensionsAsString.substring(1, extensionsAsString.length() - 1) + "."
        );
    }

    public static String removeExtension(String fileName) {
        Extension extension = findExtension(fileName);
        if (extension != null) return StringUtils.removeEnd(fileName, "." + extension.getValue());
        return fileName;
    }

    public static Extension findExtension(String fileName) {
        Extension foundExtension = null;
        for (Extension extension : values()) {
            if (fileName.endsWith("." + extension.getValue()) &&
                    (foundExtension == null || extension.getValue().length() > foundExtension.getValue().length())) {
                foundExtension = extension;
            }
        }
        return foundExtension;
    }

    @Override
    public String toString() {
        return value;
    }
}
@@ -1607,26 +1607,41 @@
     
-Extension%5B%5D slangF
+validateFileExtension(fileName, f
 ileE
@@ -1644,27 +1644,25 @@
 ileExtension
-s =
+,
  getSlangFil
@@ -1678,66 +1678,14 @@
 ns()
-;%0A        validateFileExtension(fileName, fileExtension, s
+, getS
 lang
@@ -1689,33 +1689,40 @@
 angFileExtension
-s
+Values()
 );%0A    %7D%0A%0A    pu
@@ -1870,31 +1870,41 @@
     
-Extension%5B%5D propertiesF
+validateFileExtension(fileName, f
 ileE
@@ -1911,19 +1911,17 @@
 xtension
-s =
+,
  getProp
@@ -1946,66 +1946,14 @@
 ns()
-;%0A        validateFileExtension(fileName, fileExtension, p
+, getP
 rope
@@ -1966,25 +1966,32 @@
 ileExtension
-s
+Values()
 );%0A    %7D%0A%0A  
@@ -2075,38 +2075,110 @@
 tension,
- Extension%5B%5D extension
+%0A                                              Extension%5B%5D extensions, String%5B%5D extensionValue
 s) %7B%0A   
@@ -2415,16 +2415,21 @@
 xtension
+Value
 s);%0A    
@@ -3354,80 +3354,6 @@
    %7D
-%0A%0A    @Override%0A    public String toString() %7B%0A        return value;%0A    %7D
 %0A%7D

enforce flow name