package com.maniu.lib_cache.network.proxy;

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

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

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 专门用来处理OkHttp的请求业务逻辑的
 */
public class OkhttpModel implements IHttp {
    private static final String TAG = "OkHttpModel";

    private Context context;
    private OkHttpClient mOkHttpClient;

    public OkhttpModel(Context context){
        this.context = context;
        this.mOkHttpClient = new OkHttpClient();
    }


    @Override
    public <T> void get(String url, Map<String, Object> params,final ICallBack iCallBack) {
        final Request request = new Request.Builder()
                .url(url)
                .addHeader("User-Agent", "Zh")
                .build();
        mOkHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                iCallBack.onFailed(e.toString());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    final String result = response.body().string();
                    Log.d(TAG, "run: " + result);
                    iCallBack.onSuccess(result);
                } else {
                    iCallBack.onFailed(response.message().toString());
                }
            }
        });
    }

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

    }
}
