package com.maniu.lib_cache.network.proxy;

import android.content.Context;
import android.util.Log;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import java.util.Map;

public class VolleyModel implements IHttp {
    private static VolleyModel volleyModel;
    //初始化一个请求队列
    private static RequestQueue requestQueue;
    private static final String TAG = "VolleyModel";


    private VolleyModel(Context context){
        requestQueue = Volley.newRequestQueue(context);
    }

    public static VolleyModel getInstance(Context context){
        if(volleyModel == null){
            synchronized (VolleyModel.class){
                if(volleyModel == null){
                    volleyModel = new VolleyModel(context);
                }
            }
        }
        return volleyModel;
    }


    @Override
    public <T> void get(String url, Map<String, Object> params, final ICallBack iCallBack) {
        StringRequest request = new StringRequest(Request.Method.GET,
                url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
//                        System.out.println(response);
                        Log.d(TAG, "onResponse: " + response);
                        iCallBack.onSuccess(response);
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                iCallBack.onFailed(error.toString());
            }
        });
        requestQueue.add(request);
    }

    @Override
    public <T> void post(String url, Map<String, Object> params, ICallBack iCallBack) {

    }
}
