package io.viper.core.server.router;

import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RouteUtil
{
  public static List<String> parsePath(String path)
  {
    int queryParamStart = path.indexOf("?");

    if (queryParamStart > 0)
    {
      path = path.substring(0, path.indexOf("?"));
    }

    String[] parts = path.split("\\/");

    List<String> parsedPath = new ArrayList<String>();

    for (String part : parts)
    {
      if (part.isEmpty()) continue;
      parsedPath.add(part);
    }

    return parsedPath;
  }

  public static Map<String, String> extractQueryParams(URI uri)
    throws UnsupportedEncodingException
  {
    return extractQueryParams(uri.getQuery());
  }

  public static Map<String, String> extractQueryParams(String queryString)
    throws UnsupportedEncodingException
  {
    Map<String, String> map = new HashMap<String, String>();
    
    if (queryString != null)
    {
      String[] parts = queryString.split("&");
      for (String part : parts)
      {
        String[] pair = part.split("=");
        if (pair.length != 2) continue;
        map.put(pair[0], URLDecoder.decode(pair[1], "UTF-8"));
      }
    }

    return map;
  }
  
  public static boolean match(List<String> route, List<String> path)
  {
    if (path.size() < route.size()) return false;
    if (path.size() > route.size() && route.size() == 0) return false;
    if ((path.size() > route.size()) && (!route.get(route.size()-1).startsWith("$"))) return false;

    for (int i = 0; i < route.size(); i++)
    {
      if (route.get(i).startsWith("$")) continue;
      if (route.get(i).equals(path.get(i))) continue;
      return false;
    }

    return true;
  }


  public static Map<String, String> extractPathArgs(List<String> route, List<String> path)
  {
    Map<String, String> args = new HashMap<String, String>();

    for (int i = 0; i < route.size(); i++)
    {
      if (!route.get(i).startsWith("$")) continue;

      String varName = route.get(i).substring(1);

      if (i < route.size()-1)
      {
        args.put(varName, path.get(i));
      }
      else
      {
        args.put(varName, join(path.subList(i, path.size()), "/"));
      }
    }

    return args;
  }

  private static String join(List<String> s, String delim)
  {
    StringBuilder sb = new StringBuilder();

    for (int i = 0; i < s.size() - 1; i++)
    {
      sb.append(s.get(i));
      sb.append(delim);
    }

    if (s.size() > 0) sb.append(s.get(s.size()-1));

    return sb.toString();
  }
}

@@ -42,170 +42,344 @@
 ort 
-java.io.UnsupportedEncodingException;%0Aimport java.net.URI;%0Aimport java.net.URLDecoder;%0Aimport java.util.ArrayList;%0Aimport java.util.HashMap;%0Aimport java.util.List
+org.jboss.netty.buffer.ChannelBuffer;%0Aimport org.jboss.netty.handler.codec.http.HttpHeaders;%0Aimport org.jboss.netty.handler.codec.http.HttpRequest;%0Aimport org.json.JSONException;%0Aimport org.json.JSONObject;%0A%0Aimport java.io.UnsupportedEncodingException;%0Aimport java.net.URI;%0Aimport java.net.URISyntaxException;%0Aimport java.net.URLDecoder
 ;%0Aim
@@ -393,19 +393,17 @@
 va.util.
-Map
+*
 ;%0A%0Apubli
@@ -1996,16 +1996,1274 @@
 ;%0A  %7D%0A%0A%0A
+  public static Map%3CString, String%3E extractArgs(HttpRequest request, List%3CString%3E route, List%3CString%3E path)%0A    throws URISyntaxException, UnsupportedEncodingException, JSONException%0A  %7B%0A    Map%3CString, String%3E args = RouteUtil.extractPathArgs(route, path);%0A    args.putAll(RouteUtil.extractQueryParams(new URI(request.getUri())));%0A%0A    ChannelBuffer content = request.getContent();%0A    if (!content.hasArray())%0A    %7B%0A      return null;%0A    %7D%0A%0A    byte%5B%5D body = content.array();%0A    String contentLengthHeader = request.getHeader(HttpHeaders.Names.CONTENT_LENGTH);%0A%0A    int contentLength =%0A      contentLengthHeader != null%0A        ? Integer.parseInt(request.getHeader(HttpHeaders.Names.CONTENT_LENGTH))%0A        : body.length;%0A%0A    if (contentLength != body.length) %7B%0A      body = Arrays.copyOfRange(body, 0, contentLength);%0A    %7D%0A%0A    String rawContent = new String(body, %22UTF-8%22);%0A%0A    if (rawContent.startsWith(%22%7B%22))%0A    %7B%0A      JSONObject json = new JSONObject(rawContent);%0A%0A      Iterator keys = json.keys();%0A      while(keys.hasNext())%0A      %7B%0A        String key = keys.next().toString();%0A        args.put(key, json.getString(key));%0A      %7D%0A    %7D%0A    else%0A    %7B%0A      args.putAll(RouteUtil.extractQueryParams(rawContent));%0A    %7D%0A%0A    return args;%0A  %7D%0A%0A
   public

update routes