package grace.core;

import java.net.MalformedURLException;
import java.net.URL;

import grace.core.factory.GraceFactory;
import grace.core.mapper.Converter;
import grace.core.http.Executor;
import grace.core.http.Request;
import grace.core.mapper.GraceHttpCallMapper;
import grace.core.mapper.GraceMapper;
import grace.core.mapper.GraceRuleMapper;
import grace.core.mapper.MapperConfig;

/**
 * Created by hechao on 2017/4/4.
 */

public class Grace {

    private static Converter.Factory DEFAULT_CONVERTER_FACTORY = GraceFactory.createConvertFactory();

    private static Executor sExecutor = GraceFactory.createStringSynExecutor();

    public static <S> S from(Class<S> service){
        return new GraceHttpServiceMapperImpl().toService(service);
    }

    public static GraceHttpCallMapper from(Request request){
        return new GraceHttpCallMapperImpl(new MapperConfig.Builder<String>().executor(sExecutor).build(),request,DEFAULT_CONVERTER_FACTORY, GraceCall.CALL_FACTORY);
    }

    public static  GraceHttpCallMapper from(URL url){
        return from(Request.Builder.of(url).build());
    }

    public static  GraceHttpCallMapper fromUrl(String url){
        try {
            URL urlObj = new URL(url);
            return from(urlObj);
        } catch (MalformedURLException e) {
            e.printStackTrace();
            throw new IllegalArgumentException("invalid url:"+url);
        }
    }

    public static GraceMapper from(byte[] value){
        return new GraceMapperImpl(value,DEFAULT_CONVERTER_FACTORY);
    }

    public static GraceMapper from(String value){
        return new GraceMapperImpl(value, DEFAULT_CONVERTER_FACTORY);
    }

    public static String toString(Object value){
        return new GraceMapperImpl(value, DEFAULT_CONVERTER_FACTORY).to(String.class);
    }

    public static GraceRuleMapper filter(String value){
        return new GraceFilterMapperImpl(value,DEFAULT_CONVERTER_FACTORY);
    }

    public static void setDefaultExecutor(Executor executor){
        sExecutor = executor;
    }
}

@@ -627,24 +627,107 @@
 xecutor();%0A%0A
+    private static Converter.Factory sConvertFactory = DEFAULT_CONVERTER_FACTORY;%0A%0A
     public s
@@ -1016,33 +1016,23 @@
 est,
-DEFAULT_CONVERTER_FACTORY
+sConvertFactory
 , Gr
@@ -1071,33 +1071,32 @@
   public static 
- 
 GraceHttpCallMap
@@ -1193,17 +1193,16 @@
  static 
- 
 GraceHtt
@@ -1577,33 +1577,23 @@
 lue,
-DEFAULT_CONVERTER_FACTORY
+sConvertFactory
 );%0A 
@@ -1694,33 +1694,23 @@
 ue, 
-DEFAULT_CONVERTER_FACTORY
+sConvertFactory
 );%0A 
@@ -1810,33 +1810,23 @@
 ue, 
-DEFAULT_CONVERTER_FACTORY
+sConvertFactory
 ).to
@@ -1955,33 +1955,23 @@
 lue,
-DEFAULT_CONVERTER_FACTORY
+sConvertFactory
 );%0A 
@@ -2073,11 +2073,149 @@
 r;%0A    %7D
+%0A%0A    public static void setDefaultConverterFactory(Converter.Factory converterFactory)%7B%0A        sConvertFactory = converterFactory;%0A    %7D
 %0A%7D%0A

add set convert factory method for grace