package com.kmall.manager.manager.common;

import okhttp3.*;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author Scott Chen
 * @date 2017/3/13
 */
public class OkHttpUtilss {

    public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
    public static final int CONNEC_TIME = 20;
    public static final int READ_TIME = 30;
    public static final int WRITE_TIME = 30;

    private static OkHttpClient okHttpClient = null;
    static{
        okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(CONNEC_TIME, TimeUnit.SECONDS)
                .readTimeout(READ_TIME, TimeUnit.SECONDS)
                .writeTimeout(WRITE_TIME, TimeUnit.SECONDS)
                .build();
    }

    /**
     * 构造RequestBody
     *
     * @param params
     * @return
     */
    public static RequestBody buildRequestBody(Map<String, String> params) {
        FormBody.Builder builder = new FormBody.Builder();
        Iterator<Map.Entry<String, String>> iterator = params.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, String> entry = iterator.next();
            builder.add(entry.getKey(), entry.getValue());
        }
        return builder.build();
    }


    /**
     * 以字符串数据构建Request(未使用)
     * @param url
     * @param json
     * @return
     */
    public static Request buildRequest(String url, String json) {
        RequestBody body = RequestBody.create(JSON, json);
        return buildRequest(url, body);
    }

    /**
     * 构建Request
     * @param url
     * @param body
     * @return
     */
    public static Request buildRequest(String url, RequestBody body) {
        return new Request.Builder()
                .url(url)
                .post(body)
                .build();
    }

    /**
     * 同步访问,返回结果字符串
     * 可能超时
     *
     * @param request
     * @return
     * @throws IOException
     */
    public static String post(Request request) throws IOException {
        Response response = okHttpClient.newCall(request).execute();
        String result = "";
        if (response.isSuccessful()) {
            result = response.body().string();
        }else {
            throw new IOException("okhttp3 post exception: " + response);
        }
        return result;
    }

    /**
     * 同步访问,返回Response
     * 可能超时
     * @param request
     * @return
     * @throws IOException
     */
    public static Response postReturnResponse(Request request)  throws IOException {
        return okHttpClient.newCall(request).execute();
    }

    /**
     * 异步访问,回调结果
     * @param request
     * @param responseCallback
     */
    public static void asyncPostCallback(Request request, Callback responseCallback) {
        okHttpClient.newCall(request).enqueue(responseCallback);
    }

    /**
     * 异步访问,无结果返回
     * @param request
     */
    public static void asyncPost(Request request) {
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

            }
        });
    }

}
