package io.github.ibuildthecloud.gdapi.util;

import static io.github.ibuildthecloud.gdapi.model.Schema.Method.*;
import io.github.ibuildthecloud.gdapi.request.ApiRequest;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.ObjectUtils;

public class RequestUtils {

    public static boolean isBrowser(HttpServletRequest request, boolean checkAccepts) {
        String accepts = request.getHeader("Accept");
        String userAgent = request.getHeader("User-Agent");

        if ( accepts == null || ! checkAccepts ) {
            accepts = "*/*";
        }

        accepts = accepts.toLowerCase();

        // User agent has Mozilla and browser accepts */*
        return ( userAgent != null && userAgent.toLowerCase().indexOf("mozilla") != -1 &&
                accepts.indexOf("*/*") != -1 );
    }

    public static boolean isReadMethod(String method) {
        return ! isWriteMethod(method);
    }

    public static boolean isWriteMethod(String method) {
        return POST.isMethod(method) ||
                PUT.isMethod(method) ||
                DELETE.isMethod(method);
    }

    public static boolean mayHaveBody(String method) {
        return POST.isMethod(method) ||
                PUT.isMethod(method);
    }

    public static String getSingularStringValue(String key, Map<String,Object> params) {
        Object obj = params.get(key);
        Object singleObj = makeSingular(obj);
        return ObjectUtils.toString(singleObj, null);
    }

    public static Object makeSingular(Object input) {
        if ( input instanceof List ) {
            List<?> list = (List<?>)input;
            return list.size() == 0 ? null : list.get(0);
        }

        if ( input instanceof String[] ) {
            String[] array = (String[])input;
            return array.length == 0 ? null : array[0];
        }

        return input;
    }
    public static Object makeSingularIfCan(Object input) {
        if ( input instanceof List ) {
            List<?> list = (List<?>)input;
            if ( list.size() == 1 )
                return list.get(0);
            if ( list.size() == 0 )
                return null;
        }

        if ( input instanceof String[] ) {
            String[] array = (String[])input;
            if ( array.length == 1 ) {
                return array[0];
            }
            if ( array.length == 0 ) {
                return null;
            }
            return Arrays.asList(array);
        }

        return input;
    }

    public static String makeSingularStringIfCan(Object input) {
        Object result = makeSingularIfCan(input);
        return result == null ? null : result.toString();
    }

    public static boolean hasBeenHandled(ApiRequest request) {
        if ( request.isCommitted() || request.getResponseObject() != null ) {
            return true;
        }

        return false;

    }

    public static List<?> toList(Object obj) {
        if ( obj instanceof List ) {
            return (List<?>)obj;
        } else if ( obj == null ) {
            return Collections.emptyList();
        } else {
            return Arrays.asList(obj);
        }
    }

    @SuppressWarnings("unchecked")
    public static <K,V> Map<K,V> toMap(Object obj) {
        if ( obj == null ) {
            return new HashMap<K, V>();
        }

        if ( obj instanceof Map ) {
            return (Map<K, V>) obj;
        } else {
            return new HashMap<K, V>();
        }
    }

}
@@ -106,16 +106,139 @@
 ethod.*;
+%0A%0Aimport io.github.ibuildthecloud.gdapi.condition.Condition;%0Aimport io.github.ibuildthecloud.gdapi.condition.ConditionType;
 %0Aimport 
@@ -2124,24 +2124,25 @@
 nput;%0A    %7D%0A
+%0A
     public s
@@ -2325,24 +2325,42 @@
      return 
+makeSingularIfCan(
 list.get(0);
@@ -2350,32 +2350,33 @@
 fCan(list.get(0)
+)
 ;%0A            if
@@ -2588,24 +2588,42 @@
  return 
+makeSingularIfCan(
 array%5B0%5D
 ;%0A      
@@ -2606,32 +2606,33 @@
 arIfCan(array%5B0%5D
+)
 ;%0A            %7D%0A
@@ -2705,32 +2705,32 @@
 ;%0A            %7D%0A
-
             retu
@@ -2757,32 +2757,219 @@
 ay);%0A        %7D%0A%0A
+        if ( input instanceof Condition && ((Condition) input).getConditionType() == ConditionType.EQ ) %7B%0A            return makeSingularIfCan(((Condition) input).getValue());%0A        %7D%0A%0A
         return i

Make singular simple condition objects