package com.shuimin.pond.core;


import com.shuimin.common.SPILoader;
import com.shuimin.common.f.Tuple;
import com.shuimin.common.sql.Criterion;
import com.shuimin.pond.core.spi.MultipartRequestResolver;

import javax.servlet.http.Cookie;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

import static com.shuimin.common.S._for;

/**
 * event
 *
 * @author ed
 */
public interface Request {

    InputStream in() throws IOException;

    String path();

    String uri();

    @Deprecated
    Locale locale();

    Map<String, String[]> headers();

    String[] header(String string);

    Map<String, String[]> params();

    String param(String para);

    Integer paramInt(String para);

    Boolean paramBool(String para);

    Double paramDouble(String para);

    Long paramLong(String para);

    Request param(String para, String value);

    String[] params(String para);

    String method();

    String remoteIp();

    Iterable<Cookie> cookies();

    Cookie cookie(String s);

    //    HttpServletRequest raw();
    String characterEncoding();

    default Ctx ctx() {
        throw new UnsupportedOperationException("use wrapper");
    }
//    Route route();
//
//    Route route(Route r);
//

    //TODO
    default List<Tuple.T3<String, Criterion, Object[]>>

    toQuery(Iterable<String> declaredFields) {
        List<Tuple.T3<String, Criterion, Object[]>>
                conditions = new ArrayList<>();
        for (String f : declaredFields) {
            String ori_c_and_v = this.param(f);
            if (ori_c_and_v == null) continue;
            String[] c_and_v = ori_c_and_v.split(",");
            if (c_and_v.length > 0) {
                if (c_and_v.length == 1) {
                    //&uid=xxx;
                    //eq
                    conditions.add(Tuple.t3(f, Criterion.EQ, c_and_v));
                } else {
                    conditions.add(Tuple.t3(f,
                            Criterion.of(c_and_v[0]),
                            Arrays.copyOfRange(c_and_v, 1, c_and_v.length)
                    ));
                }
            }
        }
        return conditions;
    }

    final MultipartRequestResolver mResolver =
            SPILoader.service(MultipartRequestResolver.class);

    default Map<String, Object> toMap() {
        if (mResolver.isMultipart(this)) {
            return mResolver.resolve(this);
        }
        Map<String, Object> map = new HashMap<>();

        _for(params()).each(e -> {
            Object val = e.getValue() != null && e.getValue().length > 0
                    ? e.getValue()[0]
                    : null;
            map.put(e.getKey(), val);
        });
        return map;
    }

}

@@ -135,24 +135,70 @@
 .Criterion;%0A
+import com.shuimin.pond.core.http.UploadFile;%0A
 import com.s
@@ -244,16 +244,16 @@
 solver;%0A
-
 %0Aimport 
@@ -460,16 +460,134 @@
 uest %7B%0A%0A
+    final static MultipartRequestResolver mResolver =%0A            SPILoader.service(MultipartRequestResolver.class);%0A%0A
     Inpu
@@ -805,24 +805,24 @@
  params();%0A%0A
-
     String p
@@ -837,24 +837,280 @@
 ing para);%0A%0A
+    default UploadFile paramFile(String name) %7B%0A        Object ret = this.toMap().get(name);%0A        if ( ret instanceof UploadFile ) %7B%0A            return (UploadFile) ret;%0A        %7D%0A        throw new RuntimeException(%22%22 + name + %22 is not a file%22);%0A    %7D%0A%0A
     Integer 
@@ -2574,118 +2574,8 @@
  %7D%0A%0A
-    final MultipartRequestResolver mResolver =%0A            SPILoader.service(MultipartRequestResolver.class);%0A
 %0A   

add Request#paramFile