package com.fq.channel.sdk.base.net;

import com.fq.channel.sdk.base.net.callback.BaseCallback;
import com.fq.threelib.okhttp3.FormBody;
import com.fq.threelib.okhttp3.MediaType;
import com.fq.threelib.okhttp3.OkHttpClient;
import com.fq.threelib.okhttp3.Request;
import com.fq.threelib.okhttp3.RequestBody;

import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @Description: 用来发送get, post请求的工具类
 * @Author: FLuty
 * @CreateDate: 2020/3/30 16:49
 */
public class HttpManager {

    private static HttpManager instance = null;
    //okhttp对象
    private OkHttpClient client;

    private static final int TIMEOUT = 10; //连接/写入/读取超时时间 单位:秒

    private HttpManager() {
    }

    public static HttpManager getInstance() {
        if (instance == null) {
            instance = new HttpManager();
        }
        return instance;
    }

    /**
     * 初始化OkHttpClient
     * 这个方法必需要调用一次
     */
    public void initHttp() {
        client = new OkHttpClient.Builder()
                .connectTimeout(TIMEOUT, TimeUnit.SECONDS)
                .writeTimeout(TIMEOUT, TimeUnit.SECONDS)
                .readTimeout(TIMEOUT, TimeUnit.SECONDS)
                .build();
    }

    public void request(final Request request, final BaseCallback callback) {
        client.newCall(request).enqueue(callback);
    }

    /**
     * 对外公开的get方法
     *
     * @param url
     * @param callback
     */
    public void get(String url, BaseCallback callback) {
        Request request = buildRequest(url, null,null, HttpMethodType.GET);
        request(request, callback);
    }

    /**
     * 对外公开的post方法
     *
     * @param url
     * @param params
     * @param callback
     */
    public void post(String url, Map<String, String> params, BaseCallback callback) {
        Request request = buildRequest(url,null, params, HttpMethodType.POST);
        request(request, callback);
    }

    /**
     * 对外公开的post方法
     *
     * @param url
     * @param requestStr
     * @param callback
     */
    public void post(String url, String requestStr, BaseCallback callback) {
        RequestBody requestBody = RequestBody
                .create(MediaType.parse("application/json; charset=utf-8"), requestStr);
        Request.Builder builder = new Request.Builder();
        builder.url(url);
        builder.post(requestBody);
        request(builder.build(), callback);
    }

    /**
     * 构建请求对象
     *
     * @param url
     * @param params
     * @param type
     * @return
     */
    private Request buildRequest(String url, Map<String, String> headers, Map<String, String> params, HttpMethodType type) {
        Request.Builder builder = new Request.Builder();
        builder.url(url);
        //请求头信息
        if (headers != null && headers.size() > 0) {
            for (Map.Entry<String, String> entry : headers.entrySet()) {
                builder.addHeader(entry.getKey(), entry.getValue());
            }
        }
        if (type == HttpMethodType.GET) {
            builder.get();
        } else if (type == HttpMethodType.POST) {
            builder.post(buildRequestBody(params));
        }
        return builder.build();
    }

    /**
     * 通过Map的键值对构建请求对象的body
     *
     * @param params
     * @return
     */
    private RequestBody buildRequestBody(Map<String, String> params) {
        FormBody.Builder builder = new FormBody.Builder();
        if (params != null) {
            for (Map.Entry<String, String> entity : params.entrySet()) {
                builder.add(entity.getKey(), entity.getValue());
            }
        }
        return builder.build();
    }

    /**
     * 这个枚举用于指明是哪一种提交方式
     */
    enum HttpMethodType {
        GET,
        POST
    }

}
