package com.yutou.okhttp.api;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.internal.bind.DateTypeAdapter;
import com.yutou.okhttp.HttpLoggingInterceptor;
import com.yutou.okhttp.ParamsContext;
import com.yutou.okhttp.converter.JsonCallAdapter;
import com.yutou.okhttp.converter.JsonConverterFactory;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.CallAdapter;
import retrofit2.Converter;
import retrofit2.Retrofit;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.logging.Level;

public class BaseApi {
    private String URL;
    private HashMap<String, String> params;

    public BaseApi setURL(String URL) {
        this.URL = URL;
        return this;
    }

    public BaseApi setParams(HashMap<String, String> params) {
        this.params = params;
        return this;
    }

    /**
     * 创建一个接口方法
     *
     * @param okHttpClient okhttp客户端
     * @param converterFactory 处理工厂类
     * @param callAdapterFactory 请求适配器工厂
     * @param baseUrl 基础地质
     * @param service 接口
     * @param <T> 接口泛型
     * @return 接口
     */
    public <T> T create(OkHttpClient okHttpClient, Converter.Factory converterFactory, CallAdapter.Factory callAdapterFactory, String baseUrl, Class<T> service) {
        Retrofit.Builder builder = new Retrofit.Builder()
                //基础url
                .baseUrl(baseUrl)
                //客户端OKHttp
                .client(okHttpClient);
        //添加转换工厂
        if (null != converterFactory) {
            builder.addConverterFactory(converterFactory);
        }
        //添加请求工厂
        if (null != callAdapterFactory) {
            builder.addCallAdapterFactory(callAdapterFactory);
        }
        //创建retrofit对象
        Retrofit retrofit = builder.build();

        //返回创建的api
        return retrofit.create(service);
    }

    public <T> T createApi(Class<T> apiClass) {
        Gson gson = new GsonBuilder()
                .registerTypeAdapter(Date.class, new DateTypeAdapter())
                .create();
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor("http");
        loggingInterceptor.setPrintLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient.Builder builder = new OkHttpClient()
                .newBuilder()

                .addInterceptor(initQuery())
                .addInterceptor(loggingInterceptor);
        return create(builder.build(),
                JsonConverterFactory.create(gson),
                JsonCallAdapter.create(),
                URL,
                apiClass);
    }
    public Interceptor initQuery() {
        Interceptor addQueryParameterInterceptor = new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Request request = chain.request();
                //配置公共参数
                request = new ParamsContext(params,request).getInRequest();
                return chain.proceed(request);
            }
        };
        return addQueryParameterInterceptor;
    }
}
