package com.ifpdos.autotest.okhttp;

import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.util.Log;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import java.io.IOException;
import java.util.Map;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/* JADX INFO: compiled from: OkHttpHelper.java */
/* JADX INFO: loaded from: classes.dex */
public class d {

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private OkHttpClient f2306c = b.b.e.a.f.e().build();

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private Gson f2307d = new Gson();

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    private Handler f2308e = new Handler(Looper.getMainLooper());

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public static final MediaType f2305b = MediaType.parse("application/json; charset=utf-8");

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    private static d f2304a = new d();

    /* JADX INFO: compiled from: OkHttpHelper.java */
    class a implements Callback {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        final /* synthetic */ c f2309a;

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        final /* synthetic */ Request f2310b;

        a(c cVar, Request request) {
            this.f2309a = cVar;
            this.f2310b = request;
        }

        @Override // okhttp3.Callback
        public void onFailure(Call call, IOException iOException) {
            this.f2309a.d(this.f2310b, iOException);
        }

        @Override // okhttp3.Callback
        public void onResponse(Call call, Response response) throws IOException {
            this.f2309a.e(response);
            if (!response.isSuccessful()) {
                d.this.h(this.f2309a, response, null);
                return;
            }
            String strString = response.body().string();
            Log.d("OkHttpHelper", "result=" + strString);
            if (this.f2309a.f2303a == String.class || TextUtils.isEmpty(strString)) {
                d.this.i(this.f2309a, response, strString);
                return;
            }
            try {
                d.this.i(this.f2309a, response, d.this.f2307d.fromJson(strString, this.f2309a.f2303a));
            } catch (JsonParseException e2) {
                this.f2309a.c(response, response.code(), e2);
            }
        }
    }

    /* JADX INFO: compiled from: OkHttpHelper.java */
    enum b {
        GET,
        POST
    }

    private d() {
    }

    private Request d(String str) {
        return f(str, b.GET, null);
    }

    private Request e(String str, Map<String, String> map) {
        return f(str, b.POST, map);
    }

    private Request f(String str, b bVar, Map<String, String> map) {
        Request.Builder builderUrl = new Request.Builder().url(b.b.e.a.f.a(str));
        if (bVar == b.POST) {
            builderUrl.post(g(map));
        } else if (bVar == b.GET) {
            builderUrl.get();
        }
        return builderUrl.build();
    }

    private RequestBody g(Map<String, String> map) {
        JsonObject jsonObject = new JsonObject();
        if (map != null) {
            for (Map.Entry<String, String> entry : map.entrySet()) {
                jsonObject.addProperty(entry.getKey(), entry.getValue());
            }
        }
        return RequestBody.create(f2305b, new Gson().toJson((JsonElement) jsonObject));
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void h(final c cVar, final Response response, final Exception exc) {
        this.f2308e.post(new Runnable() { // from class: com.ifpdos.autotest.okhttp.a
            @Override // java.lang.Runnable
            public final void run() {
                c cVar2 = cVar;
                Response response2 = response;
                cVar2.c(response2, response2.code(), exc);
            }
        });
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void i(final c cVar, final Response response, final Object obj) {
        this.f2308e.post(new Runnable() { // from class: com.ifpdos.autotest.okhttp.b
            @Override // java.lang.Runnable
            public final void run() {
                cVar.f(response, obj);
            }
        });
    }

    public static d l() {
        return f2304a;
    }

    public Response j(Request request) throws IOException {
        return this.f2306c.newCall(request).execute();
    }

    public <T> T k(String str, Class<T> cls) {
        return (T) p(d(str), cls);
    }

    public void o(String str, Map<String, String> map, c cVar) {
        q(e(str, map), cVar);
    }

    public synchronized <T> T p(Request request, Class<T> cls) {
        try {
            Response responseJ = j(request);
            Log.i("OkHttpHelper", "request result:" + responseJ.code());
            if (responseJ.code() != 200) {
                return null;
            }
            T t = (T) new Gson().fromJson(responseJ.body().string(), (Class) cls);
            Log.i("OkHttpHelper", "post result:" + t);
            return t;
        } catch (IOException unused) {
            return null;
        }
    }

    public void q(Request request, c cVar) {
        cVar.b(request);
        this.f2306c.newCall(request).enqueue(new a(cVar, request));
    }
}
