package com.sobot.network.apiUtils;


import android.content.Context;

import com.sobot.network.http.HttpUtils;
import com.sobot.network.http.log.SobotNetLogUtils;

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

import okhttp3.MediaType;
import okhttp3.Response;

public class SobotHttpUtils {

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

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

    public static void doPost(Object cancelTag, String url, Map<String, String> paramsMap, Map<String, String> headerMap,
                              HttpUtils.StringCallBack call) {
        HttpUtils.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,
                                      HttpUtils.StringCallBack call) {
        HttpUtils.getInstance().doPostByString(cancelTag, url, paramsMap, headerMap, mediaType, call);
    }

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

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

    public static void doGet(Object cancelTag, String url, Map<String, String> paramsMap, Map<String, String> headerMap,
                             HttpUtils.StringCallBack call) {
        HttpUtils.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 HttpUtils.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, HttpUtils.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));
        }

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

    }

    public interface SobotRequestCallBack {
        void onSuccess(String result);

        void onFailure(String result);
    }

    /**
     * 初始化
     *
     * @param context
     */
    public static void init(Context context) {
        if (context == null) {
            SobotNetLogUtils.e("SobotHttpUtils init: context is null, please check!");
        } else {
            Context applicationContext = context.getApplicationContext();
            SobotHttpGlobalContext.getInstance(applicationContext);
        }
    }
}