package com.hongtu.wf.helper;

import com.hongtu.wf.annotation.*;
import com.hongtu.wf.bean.Handler;
import com.hongtu.wf.bean.Request;
import com.hongtu.wf.utils.ArrayUtil;
import com.hongtu.wf.utils.CollectionUtil;
import com.sun.org.apache.regexp.internal.RE;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * map(request -> handler)
 * Created by hongtu on 16-11-9.
 */
public final class ControllerHelper {
    private static final Map<Request, Handler> ACTION_MAP = new HashMap<Request, Handler>();
    private static final Map<Request, Handler> GET_ACTION_MAP = new HashMap<Request, Handler>();
    private static final Map<Request, Handler> POST_ACTION_MAP = new HashMap<Request, Handler>();
    private static final Map<Request, Handler> DELETE_ACTION_MAP = new HashMap<Request, Handler>();

    static {
        Set<Class<?>> controllerClassSet = ClassHelper.getControllerClassSet();
        if (CollectionUtil.isNotEmpty(controllerClassSet)) {
            for (Class<?> controllerClass : controllerClassSet) {
                Method[] methods = controllerClass.getDeclaredMethods();
                if (ArrayUtil.isNotEmpty(methods)) {
                    for (Method method : methods) {
                        if (method.isAnnotationPresent(Path.class)) {
                            Path action = method.getAnnotation(Path.class);
                            String mapping = action.value();
                            Handler handler = new Handler(controllerClass, method);
                            if (mapping.matches("/\\w*")) {
                                String requestPath = mapping;
                                Request request = null;
                                if (method.isAnnotationPresent(Get.class)) {
                                    request = new Request("get", requestPath);
                                }
                                if (method.isAnnotationPresent(Post.class)) {
                                    request = new Request("post", requestPath);
                                }
                                if (method.isAnnotationPresent(Delete.class)) {
                                    request = new Request("delete", requestPath);
                                }
                                if (request != null) {
                                    ACTION_MAP.put(request, handler);
                                }
                            }
                        }
//                        if (method.isAnnotationPresent(Action.class)) {
//                            Action action = method.getAnnotation(Action.class);
//                            String mapping = action.value();
//                            //check url role
//                            if (mapping.matches("\\w+:/\\w*")) {
//                                String[] array = mapping.split(":");
//                                if (ArrayUtil.isNotEmpty(array) && array.length == 2) {
//                                    String requestMethod = array[0];//get,post...
//                                    String requestPath = array[1];
//                                    Request request = new Request(requestMethod, requestPath);
//                                    Handler handler = new Handler(controllerClass, method);
//                                    ACTION_MAP.put(request, handler);
//                                }
//                            }
//                        }
                    }
                }
            }
        }
    }

    public static Handler getHandler(String requestMethod, String requestPath) {
        Request request = new Request(requestMethod, requestPath);
        return ACTION_MAP.get(request);
    }
}

@@ -1430,32 +1430,34 @@
       String
+%5B%5D
  mapping
  = action.va
@@ -1436,32 +1436,33 @@
 String%5B%5D mapping
+s
  = action.value(
@@ -1460,24 +1460,90 @@
 on.value();%0A
+                            for (String mapping : mappings) %7B%0A    
             
@@ -1634,32 +1634,36 @@
                 
+   
+ 
 if (mapping.matc
@@ -1674,24 +1674,28 @@
 %22/%5C%5Cw*%22)) %7B%0A
+    
             
@@ -1768,32 +1768,36 @@
                 
+    
 Request request 
@@ -1828,32 +1828,36 @@
                 
+ 
+   
 if (method.isAnn
@@ -1913,32 +1913,36 @@
                 
+    
 request = new Re
@@ -1960,32 +1960,36 @@
 , requestPath);%0A
+    
                 
@@ -2030,32 +2030,36 @@
                 
+    
 if (method.isAnn
@@ -2116,32 +2116,36 @@
                 
+    
 request = new Re
@@ -2196,34 +2196,42 @@
                 
+  
-%7D%0A
+  %7D%0A    
                 
@@ -2286,32 +2286,36 @@
 elete.class)) %7B%0A
+    
                 
@@ -2404,34 +2404,42 @@
                 
-%7D%0A
+    %7D%0A    
                 
@@ -2505,32 +2505,36 @@
                 
+  
+  
 ACTION_MAP.put(r
@@ -2551,16 +2551,54 @@
 ndler);%0A
+                                    %7D%0A
         

fix bug:Path us String[] as values instead of String