package com.sobot.network.apiUtils;


import android.content.Context;
import android.text.TextUtils;

import com.sobot.network.http.HttpBaseUtils;
import com.sobot.network.http.SobotOkHttpUtils;
import com.sobot.network.http.log.SobotNetLogUtils;

import java.io.IOException;
import java.util.Map;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;

import okhttp3.MediaType;
import okhttp3.Response;

public class SobotHttpUtils {

    // ##################### 网络请求 #########################

    public static void doPost(Object cancelTag, String url, Map<String, String> paramsMap,
                              HttpBaseUtils.StringCallBack call) {
        HttpBaseUtils.getInstance().doPost(cancelTag, url, paramsMap, null, call);
    }

    public static void doPost(Object cancelTag, String url, Map<String, String> paramsMap, Map<String, String> headerMap,
                              HttpBaseUtils.StringCallBack call) {
        HttpBaseUtils.getInstance().doPost(cancelTag, url, paramsMap, headerMap, call);
    }

    public static void doPostByString(Object cancelTag, String url, Map<String, String> paramsMap, Map<String, String> headerMap, MediaType mediaType,
                                      HttpBaseUtils.StringCallBack call) {
        HttpBaseUtils.getInstance().doPostByString(cancelTag, url, paramsMap, headerMap, mediaType, call);
    }

    public static void doPostByJsonString(Object cancelTag, String url, Map<String, String> paramsMap, Map<String, String> headerMap,
                                          HttpBaseUtils.StringCallBack call) {
        HttpBaseUtils.getInstance().doPostByJsonString(cancelTag, url, paramsMap, headerMap, call);
    }

    public static void doGet(Object cancelTag, String url, Map<String, String> paramsMap,
                             HttpBaseUtils.StringCallBack call) {
        HttpBaseUtils.getInstance().doGet(cancelTag, url, paramsMap, null, call);
    }

    public static void doGet(Object cancelTag, String url, Map<String, String> paramsMap, Map<String, String> headerMap,
                             HttpBaseUtils.StringCallBack call) {
        HttpBaseUtils.getInstance().doGet(cancelTag, url, paramsMap, headerMap, call);
    }

    //同步请求
    public static Response doPostSync(Object cancelTag, String url, Map<String, String> paramsMap, Map<String, String> headerMap) throws IOException {
        return HttpBaseUtils.getInstance().doPostSync(cancelTag, url, paramsMap, headerMap);
    }


    /**
     * 发送文件请求
     *
     * @param url
     * @param paramsMap
     * @param filePath
     * @param callBack
     */
    public static void uploadFile(Object cancelTag, String url, Map<String, String> paramsMap, Map<String, String> headerMap,
                                  final String filePath, HttpBaseUtils.StringCallBack callBack) {
        ///////////////////////////////////////////////////////
        if (paramsMap != null) {
            // 基本传递参数
            StringBuilder sb = new StringBuilder();
            sb.append("sobot---请求参数： url = " + url + ", filePath=" + filePath + "  ");
            for (String key : paramsMap.keySet()) {
                sb.append(key + "=" + paramsMap.get(key) + ", ");
            }
            SobotNetLogUtils.i(sb.toString().substring(0, sb.toString().length() - 2));
        }

        HttpBaseUtils.getInstance().uploadFile(cancelTag, url, paramsMap, headerMap, filePath, callBack);

    }

    public interface SobotRequestCallBack {
        void onSuccess(String result);

        void onFailure(String result);
    }

    /**
     * 初始化
     *
     * @param context
     * @param baseUrl 服务器接口的校验域名 例如（http://www.sobot.com）
     */
    public static void init(Context context, final String baseUrl) {
        if (context == null) {
            SobotNetLogUtils.e("SobotHttpUtils init: context is null, please check!");
        } else {
            Context applicationContext = context.getApplicationContext();
            SobotHttpGlobalContext.getInstance(applicationContext);
        }
        SobotOkHttpUtils.getInstance().setHostNameVerifier(new HostnameVerifier() {
            @Override
            public boolean verify(String hostname, SSLSession session) {
                if (!TextUtils.isEmpty(baseUrl) && baseUrl.contains(hostname)) {
                    return true;
                } else {
                    HostnameVerifier hv = HttpsURLConnection.getDefaultHostnameVerifier();
                    boolean verifyResult = hv.verify(hostname, session);
                    return verifyResult;
                }
            }
        });
    }
}