package com.sipai.jspatrol.utils;
import com.sipai.jspatrol.utils.okHttpCookie.CookiesManager;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

/**
 * created by lvliming on 2020/5/8
 */
public class OKHttpUtils {

    public static OkHttpClient getClient(){
        OkHttpClient.Builder clientBuilder = new OkHttpClient().newBuilder();
        clientBuilder.readTimeout(8, TimeUnit.SECONDS);
        clientBuilder.connectTimeout(8, TimeUnit.SECONDS);
        clientBuilder.writeTimeout(8, TimeUnit.SECONDS);
        clientBuilder.cookieJar(new CookiesManager());
        OkHttpClient client = clientBuilder.build();
        return client;
    }


    /**
     * 带token的网络请求
     * @param map
     * @param server
     * @param url
     * @param token
     * @return
     */
    public static Request OKHttpHeaderRequest(HashMap<String, String> map, String server, String url,String token){
        FormBody.Builder builder = new FormBody.Builder();
        if (map!=null){
            for (String key:map.keySet()){
                builder.add(key,map.get(key));
            }
        }
        FormBody body = builder.build();
        Request request = new Request.Builder()
                .url(server+url)
                .addHeader("token",token)
                .post(body)
                .build();
        return request;
    }

    /**
     * 不带请求头的网络请求
     * @param map
     * @param server
     * @param url
     * @return
     */
    public static Request OKHttpRequest(HashMap<String, String> map, String server, String url){
        FormBody.Builder builder = new FormBody.Builder();
        if (map!=null){
            for (String key:map.keySet()){
                builder.add(key,map.get(key));
            }
        }
        FormBody body = builder.build();
        Request request = new Request.Builder()
                .url(server+url)
                .post(body)
                .build();
        return request;
    }

    /**
     * 不带请求头的Call
     * @param map
     * @param server
     * @param url
     * @return
     */
    public static Call OKCall(HashMap<String, String> map, String server, String url){
        FormBody.Builder builder = new FormBody.Builder();
        if (map!=null){
            for (String key:map.keySet()){
                builder.add(key,map.get(key));
            }
        }
        FormBody body = builder.build();
        Request request = new Request.Builder()
                .url(server+url)
                .post(body)
                .build();
        return OKHttpUtils.getClient().newCall(request);
    }


    /**
     * 不带请求头的Call
     * @param map
     * @param server
     * @param url
     * @return
     */
    public static Call OKCallGet(HashMap<String, String> map, String server, String url){
        String s_body = "";
        for (String key:map.keySet()){
            s_body = s_body+key+"="+map.get(key)+"&";
        }
        Request request = new Request.Builder()
                .url(server+url+"?"+(s_body.length()>0?s_body.substring(0,s_body.length()-1):""))
                .get()
                .build();
        return OKHttpUtils.getClient().newCall(request);
    }


    /**
     * 上传多图
     * @param map
     * @param server
     * @param url
     * @param picMark
     * @param datas
     * @return
     */
    public static Request OKHttpSubPics(HashMap<String, String> map, String server, String url,String picMark, List<Map<String, Object>> datas){
        MultipartBody.Builder builder = new MultipartBody.Builder();
        builder.setType(MultipartBody.FORM);
        for (int i=0;i<datas.size();i++){
            String _path = null;
            _path = datas.get(i).get("path").toString();
            File file = new File(_path);
            if (file!=null){
                builder.addFormDataPart(picMark,file.getName(), RequestBody.create(MediaType.parse("image/*"),file));
            }
        }
        if (map!=null){
            for (String key:map.keySet()){
                builder.addFormDataPart(key,map.get(key));
            }
        }
        MultipartBody requestBody = builder.build();
        //构建请求体
        Request request = new Request.Builder()
                .url(server+ url)
                .post(requestBody)
                .build();
        return request;
    }

}
