package com.kfzs.duanduan.utils.net;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.android.volleyplus.Response;
import com.kfzs.duanduan.proto.ApiResponseOuterClass;
import com.kfzs.duanduan.bean.BaseMsg;

import java.util.HashMap;

/**
 * <pre>
 *     sinlov
 *
 *     /\__/\
 *    /`    '\
 *  ≈≈≈ 0  0 ≈≈≈ Hello world!
 *    \  --  /
 *   /        \
 *  /          \
 * |            |
 *  \  ||  ||  /
 *   \_oo__oo_/≡≡≡≡≡≡≡≡o
 *
 * </pre>
 * Created by sinlov on 17/2/17.
 */
public class APIRequestInstance {

    /**
     * @param tag
     * @param url
     * @param headParams
     * @param responseListener 改为Nullable了
     * @param errorListener    改为Nullable了
     * @return
     */
    public static APIRequest getReq(Object tag, @NonNull String url, HashMap<String, String> headParams,
                                    @Nullable Response.Listener<ApiResponseOuterClass.ApiResponse> responseListener,
                                    @Nullable Response.ErrorListener errorListener) {
        return new APIRequestOption.Builder()
                .setCategory(APIRequestOption.GET)
                .setTag(tag)
                .setUrl(url)
                .setHeadParams(headParams)
                .setResponseListener(responseListener)
                .setErrorListener(errorListener)
                .build().create();
    }

    /**
     *
     * @param tag
     * @param url
     * @param headParams 协议头
     * @param urlParams 网址参数
     * @param responseListener
     * @param errorListener
     * @return
     */
    public static APIRequest getReqUrlWithHead(Object tag, @NonNull String url, HashMap<String, String> headParams,
                                               HashMap<String, String> urlParams,
                                    @Nullable Response.Listener<ApiResponseOuterClass.ApiResponse> responseListener,
                                    @Nullable Response.ErrorListener errorListener) {
        return new APIRequestOption.Builder()
                .setCategory(APIRequestOption.GET)
                .setTag(tag)
                .setUrl(url)
                .setHeadParams(headParams)
                .setUrlParams(urlParams)
                .setResponseListener(responseListener)
                .setErrorListener(errorListener)
                .build().create();
    }

    public static APIRequest getReqUrl(Object tag, @NonNull String url, HashMap<String, String> urlParams,
                                       @Nullable Response.Listener<ApiResponseOuterClass.ApiResponse> responseListener,
                                       @Nullable Response.ErrorListener errorListener) {
        return new APIRequestOption.Builder()
                .setCategory(APIRequestOption.GET)
                .setTag(tag)
                .setUrl(url)
                .setUrlParams(urlParams)
                .setResponseListener(responseListener)
                .setErrorListener(errorListener)
                .build().create();
    }

    public static APIRequest postReq(Object tag, @NonNull String url, HashMap<String, String> headParams, byte[] bodyByte,
                                     @Nullable Response.Listener<ApiResponseOuterClass.ApiResponse> responseListener,
                                     @Nullable Response.ErrorListener errorListener) {
        return new APIRequestOption.Builder()
                .setCategory(APIRequestOption.POST)
                .setTag(tag)
                .setUrl(url)
                .setBodyByte(bodyByte)
                .setMaxNumRetries(0)
                .setHeadParams(headParams)
                .setResponseListener(responseListener)
                .setErrorListener(errorListener)
                .build().create();
    }

    public static APIRequest postReq(Object tag, @NonNull String url, HashMap<String, String> headParams, byte[] bodyByte, int timeOutms,
                                     @Nullable Response.Listener<ApiResponseOuterClass.ApiResponse> responseListener,
                                     @Nullable Response.ErrorListener errorListener) {
        return new APIRequestOption.Builder()
                .setCategory(APIRequestOption.POST)
                .setTag(tag)
                .setUrl(url)
                .setBodyByte(bodyByte)
                .setInitialTimeoutMs(timeOutms)
                .setMaxNumRetries(0)
                .setHeadParams(headParams)
                .setResponseListener(responseListener)
                .setErrorListener(errorListener)
                .build().create();
    }


    public static APIRequest putReq(Object tag, @NonNull String url, byte[] bodyByte, HashMap<String, String> headParams,
                                    @Nullable Response.Listener<ApiResponseOuterClass.ApiResponse> responseListener,
                                    @Nullable Response.ErrorListener errorListener) {
        return new APIRequestOption.Builder()
                .setCategory(APIRequestOption.PUT)
                .setTag(tag)
                .setHeadParams(headParams)
                .setUrl(url)
                .setBodyByte(bodyByte)
                .setMaxNumRetries(0)
                .setResponseListener(responseListener)
                .setErrorListener(errorListener)
                .build().create();
    }

    private APIRequestInstance() {
    }



    public static APIRequestV2 getReqUrlV2(Object tag, @NonNull String url, HashMap<String, String> urlParams,
                                       @Nullable Response.Listener<BaseMsg> responseListener,
                                       @Nullable Response.ErrorListener errorListener) {
        return new APIRequestOptionV2.Builder()
                .setCategory(APIRequestOption.GET)
                .setTag(tag)
                .setUrl(url)
                .setUrlParams(urlParams)
                .setResponseListenerV2(responseListener)
                .setErrorListener(errorListener)
                .build().create();
    }
}
