package com.ytpm.hydtw.network;

import static com.ytpm.hydtw.network.api.api.BASE_URL_V2;

import com.blankj.utilcode.util.LogUtils;
import com.ytpm.hydtw.network.api.MRefrofitInterface;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * @author mmmmg
 * @description:
 * @date :2019/12/4 10:52
 */
public class BaseRetrofit {

    private static BaseRetrofit instance;
    private final MRefrofitInterface apiService;
    private int tokenTime;
    private int reTokenTime;

    public static BaseRetrofit getInstance() {

        if (instance == null) {
            synchronized (BaseRetrofit.class) {
                if (instance == null) {
                    instance = new BaseRetrofit();
                }
            }
        }

        return instance;
    }

    private BaseRetrofit() {

        this.apiService = new Retrofit.Builder()
                .baseUrl(BASE_URL_V2)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build().create(MRefrofitInterface.class);
    }

    public MRefrofitInterface getApiService() {
        return this.apiService;
    }

    /**
     * 转换为 form-data
     *
     * @param requestDataMap
     * @return
     */
    public static Map<String, RequestBody> generateRequestBody(Map<String, String> requestDataMap) {
        Map<String, RequestBody> requestBodyMap = new HashMap<>();
        for (String key : requestDataMap.keySet()) {
            RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data; charset=utf-8"), requestDataMap.get(key) == null ? "" : requestDataMap.get(key));
            requestBodyMap.put(key, requestBody);
        }
        return requestBodyMap;
    }

    public static RequestBody getRequestBody(HashMap<String, String> hashMap) {
        StringBuffer data = new StringBuffer();
        if (hashMap != null && hashMap.size() > 0) {
            Iterator iter = hashMap.entrySet().iterator();
            while (iter.hasNext()) {
                Map.Entry entry = (Map.Entry) iter.next();
                Object key = entry.getKey();
                Object val = entry.getValue();
                data.append(key).append("=").append(val).append("&");
            }
        }
        String jso = data.substring(0, data.length() - 1);
        LogUtils.e("dfdfdfdf" + jso);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), jso);

        return requestBody;
    }

    public static RequestBody getRequestBodyByMapToJson(HashMap<String, Object> hashMap) {
        JSONObject jsonObject = new JSONObject(hashMap);
        LogUtils.i("BaseRetrofit", jsonObject.toString());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), jsonObject.toString());
        return requestBody;
    }

    /**
     * 转换为 form-data
     *
     * @param requestDataMap
     * @return
     */
    public static IdentityHashMap<String, RequestBody> gerIdentitiMap(IdentityHashMap<String, String> requestDataMap) {
        IdentityHashMap<String, RequestBody> requestBodyMap = new IdentityHashMap<>();
        for (String key : requestDataMap.keySet()) {
            RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), requestDataMap.get(key) == null ? "" : requestDataMap.get(key));
            requestBodyMap.put(key, requestBody);
        }
        return requestBodyMap;
    }

}
