package org.binwang.bard.basic.injector;

import com.github.drapostolos.typeparser.TypeParser;
import org.binwang.bard.core.BindTo;
import org.binwang.bard.core.Injector;
import org.binwang.bard.core.marker.Before;

import javax.ws.rs.FormParam;
import java.io.BufferedReader;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

@BindTo(FormParam.class)
public class FormParamInjector extends Injector<FormParam> {
    @Before public void getParam() throws IOException {
        // get from cache first
        Map<String, String> formParams = (Map<String, String>) context.custom.get("form");
        if (formParams == null) {
            formParams = new HashMap<>();
            BufferedReader reader = context.request.getReader();
            String line = reader.readLine();
            String[] pairs = line.split("&");

            for (String pair : pairs) {
                String[] fields = pair.split("=");
                String name = URLDecoder.decode(fields[0], "UTF-8");
                String value = URLDecoder.decode(fields[1], "UTF-8");
                formParams.put(name, value);
            }

            context.custom.put("form", formParams);
        }

        TypeParser parser = TypeParser.newBuilder().build();
        String param = formParams.get(annotation.value());
        injectorVariable = parser.parse(param, injectorVariableType);
    }

    @Override public void generateDoc() {
        docParameter.name = annotation.value();
        docParameter.type = injectorVariableType;
        docParameter.belongs = "form";
    }
}

@@ -164,16 +164,59 @@
 jector;%0A
+import org.binwang.bard.core.marker.After;%0A
 import o
@@ -557,16 +557,41 @@
 xception
+, InvalidateFormException
  %7B%0A     
@@ -890,24 +890,46 @@
 readLine();%0A
+            try %7B%0A    
             
@@ -963,16 +963,20 @@
 (%22&%22);%0A%0A
+    
         
@@ -1015,32 +1015,36 @@
                 
+    
 String%5B%5D fields 
@@ -1074,24 +1074,28 @@
             
+ 
+   
 String name 
@@ -1127,32 +1127,36 @@
 s%5B0%5D, %22UTF-8%22);%0A
+    
                 
@@ -1217,32 +1217,36 @@
                 
+   
+ 
 formParams.put(n
@@ -1254,24 +1254,202 @@
 me, value);%0A
+                %7D%0A            %7D catch (ArrayIndexOutOfBoundsException %7C NullPointerException e) %7B%0A                throw new InvalidateFormException(%22Invalidate form format%22, e);%0A
             
@@ -1720,181 +1720,613 @@
    @
-Override public void generateDoc() %7B%0A        docParameter.name = annotation.value();%0A        docParameter.type = injectorVariableType;%0A        docParameter.belongs = %22form%22;
+After public void cleanUp() %7B%0A        if (context.exception != null &&%0A            context.exception instanceof InvalidateFormException) %7B%0A            context.result = %22invalidate form format%22;%0A            context.exception = null;%0A        %7D%0A    %7D%0A%0A    @Override public void generateDoc() %7B%0A        docParameter.name = annotation.value();%0A        docParameter.type = injectorVariableType;%0A        docParameter.belongs = %22form%22;%0A    %7D%0A%0A    public static class InvalidateFormException extends Exception %7B%0A        public InvalidateFormException(String msg, Exception e) %7B%0A            super(msg, e);%0A        %7D
 %0A   

handle invalidate form in form params