package org.rakam.util;

import io.netty.handler.codec.http.HttpResponseStatus;

import javax.annotation.Nullable;

import static io.netty.handler.codec.http.HttpResponseStatus.BAD_REQUEST;
import static java.util.Locale.ENGLISH;

public final class ValidationUtil {
    private ValidationUtil()
            throws InstantiationException {
        throw new InstantiationException("The class is not created for instantiation");
    }

    public static String checkProject(String project) {
        checkArgument(project != null, "project is null");
        if (!project.matches("^[0-9A-Za-z_]+$")) {
            throw new IllegalArgumentException("Project id is not valid. It must be alphanumeric and should not include empty space.");
        }
        return project.toLowerCase(ENGLISH);
    }

    public static String checkProject(String project, char character) {
        return character + checkProject(project).replaceAll("\"", "") + character;
    }

    public static <T> T checkNotNull(T value, String name) {
        checkArgument(value != null, name + " is null");
        return value;
    }

    public static String checkCollection(String collection) {
        return checkCollection(collection, '"');
    }

    public static String checkCollection(String collection, char character) {
        checkCollectionValid(collection);
        return character + collection.replaceAll("\"", "") + character;
    }

    public static String checkCollectionValid(String collection) {
        checkArgument(collection != null, "collection is null");
        checkArgument(!collection.isEmpty(), "collection is empty string");
        if (collection.length() > 100) {
            throw new IllegalArgumentException("Collection name must have maximum 250 characters.");
        }
        return collection;
    }

    public static String checkTableColumn(String column, char escape) {
        return checkTableColumn(column, column, escape);
    }

    public static String checkTableColumn(String column) {
        return checkTableColumn(column, column, '"');
    }

    public static String checkLiteral(String value) {
        return value.replaceAll("'", "''");
    }

    public static String checkTableColumn(String column, String type, char escape) {
        if (column == null) {
            throw new IllegalArgumentException(type + " is null");
        }

        return escape + stripName(column, "field name") + escape;
    }

    public static void checkArgument(boolean expression, @Nullable String errorMessage) {
        if (!expression) {
            if (errorMessage == null) {
                throw new RakamException(BAD_REQUEST);
            } else {
                throw new RakamException(errorMessage, BAD_REQUEST);
            }
        }
    }

    public static String stripName(String name, String type) {
        if (name.isEmpty()) {
            throw new RakamException(type + " is empty", HttpResponseStatus.BAD_REQUEST);
        }

        StringBuilder builder = new StringBuilder(name.length());
        for (int i = 0; i < name.length(); i++) {
            char charAt = name.charAt(i);
            if (charAt == '"' || (i == 0 && charAt == ' ')) {
                continue;
            }

            if (Character.isUpperCase(charAt)) {
                if (i > 0) {
                    if (Character.isLowerCase(name.charAt(i - 1))) {
                        builder.append("_");
                    }
                }

                builder.append(Character.toLowerCase(charAt));
            } else {
                builder.append(charAt);
            }
        }

        if (builder.length() == 0) {
            throw new RakamException("Invalid " + type + ": " + name, HttpResponseStatus.BAD_REQUEST);
        }

        int lastIdx = builder.length() - 1;
        if (builder.charAt(lastIdx) == ' ') {
            builder.deleteCharAt(lastIdx);
        }

        return builder.toString();
    }

}

@@ -109,16 +109,46 @@
 lable;%0A%0A
+import java.text.Normalizer;%0A%0A
 import s
@@ -287,17 +287,17 @@
 tionUtil
- 
+%0A
 %7B%0A    pr
@@ -360,16 +360,20 @@
 xception
+%0A   
  %7B%0A     
@@ -515,16 +515,20 @@
 project)
+%0A   
  %7B%0A     
@@ -891,32 +891,36 @@
  char character)
+%0A   
  %7B%0A        retur
@@ -1054,16 +1054,20 @@
 ng name)
+%0A   
  %7B%0A     
@@ -1198,32 +1198,36 @@
 ring collection)
+%0A   
  %7B%0A        retur
@@ -1344,16 +1344,20 @@
 aracter)
+%0A   
  %7B%0A     
@@ -1532,24 +1532,28 @@
  collection)
+%0A   
  %7B%0A        c
@@ -1931,32 +1931,36 @@
 mn, char escape)
+%0A   
  %7B%0A        retur
@@ -2066,16 +2066,20 @@
  column)
+%0A   
  %7B%0A     
@@ -2185,16 +2185,20 @@
 g value)
+%0A   
  %7B%0A     
@@ -2325,16 +2325,20 @@
  escape)
+%0A   
  %7B%0A     
@@ -2600,16 +2600,20 @@
 Message)
+%0A   
  %7B%0A     
@@ -2734,32 +2734,44 @@
 );%0A            %7D
+%0A           
  else %7B%0A        
@@ -2922,16 +2922,20 @@
 ng type)
+%0A   
  %7B%0A     
@@ -3630,16 +3630,268 @@
        %7D
+%0A            else if (Character.UnicodeBlock.of(charAt) != Character.UnicodeBlock.BASIC_LATIN) %7B%0A                throw new RakamException(%22Unicode characters are not supported. (%22 + name + %22)%22, HttpResponseStatus.BAD_REQUEST);%0A            %7D%0A           
  else %7B%0A
@@ -4291,11 +4291,10 @@
 ;%0A    %7D%0A
-%0A
 %7D%0A

Throw exception for unicode properties