package f;

import d.a0;
import java.io.IOException;
import java.util.Objects;
import javax.annotation.Nullable;
import javax.annotation.concurrent.GuardedBy;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

/* JADX INFO: compiled from: OkHttpCall.java */
/* JADX INFO: loaded from: classes.dex */
final class n<T> implements d<T> {

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private final s f1669b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private final Object[] f1670c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private final Call.Factory f1671d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    private final h<ResponseBody, T> f1672e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    private volatile boolean f1673f;

    @GuardedBy("this")
    @Nullable
    private Call g;

    @GuardedBy("this")
    @Nullable
    private Throwable h;

    @GuardedBy("this")
    private boolean i;

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

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

        a(f fVar) {
            this.f1674a = fVar;
        }

        private void a(Throwable th) {
            try {
                this.f1674a.a(n.this, th);
            } catch (Throwable th2) {
                y.s(th2);
                th2.printStackTrace();
            }
        }

        @Override // okhttp3.Callback
        public void onFailure(Call call, IOException iOException) {
            a(iOException);
        }

        @Override // okhttp3.Callback
        public void onResponse(Call call, Response response) {
            try {
                try {
                    this.f1674a.b(n.this, n.this.d(response));
                } catch (Throwable th) {
                    y.s(th);
                    th.printStackTrace();
                }
            } catch (Throwable th2) {
                y.s(th2);
                a(th2);
            }
        }
    }

    /* JADX INFO: compiled from: OkHttpCall.java */
    static final class b extends ResponseBody {

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        private final ResponseBody f1676b;

        /* JADX INFO: renamed from: c, reason: collision with root package name */
        private final d.h f1677c;

        /* JADX INFO: renamed from: d, reason: collision with root package name */
        @Nullable
        IOException f1678d;

        /* JADX INFO: compiled from: OkHttpCall.java */
        class a extends d.k {
            a(a0 a0Var) {
                super(a0Var);
            }

            @Override // d.k, d.a0
            public long read(d.f fVar, long j) throws IOException {
                try {
                    return super.read(fVar, j);
                } catch (IOException e2) {
                    b.this.f1678d = e2;
                    throw e2;
                }
            }
        }

        b(ResponseBody responseBody) {
            this.f1676b = responseBody;
            this.f1677c = d.p.d(new a(responseBody.source()));
        }

        @Override // okhttp3.ResponseBody, java.io.Closeable, java.lang.AutoCloseable
        public void close() {
            this.f1676b.close();
        }

        @Override // okhttp3.ResponseBody
        public long contentLength() {
            return this.f1676b.contentLength();
        }

        @Override // okhttp3.ResponseBody
        public MediaType contentType() {
            return this.f1676b.contentType();
        }

        @Override // okhttp3.ResponseBody
        public d.h source() {
            return this.f1677c;
        }

        void v() throws IOException {
            IOException iOException = this.f1678d;
            if (iOException != null) {
                throw iOException;
            }
        }
    }

    /* JADX INFO: compiled from: OkHttpCall.java */
    static final class c extends ResponseBody {

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        @Nullable
        private final MediaType f1680b;

        /* JADX INFO: renamed from: c, reason: collision with root package name */
        private final long f1681c;

        c(@Nullable MediaType mediaType, long j) {
            this.f1680b = mediaType;
            this.f1681c = j;
        }

        @Override // okhttp3.ResponseBody
        public long contentLength() {
            return this.f1681c;
        }

        @Override // okhttp3.ResponseBody
        public MediaType contentType() {
            return this.f1680b;
        }

        @Override // okhttp3.ResponseBody
        public d.h source() {
            throw new IllegalStateException("Cannot read raw response body of a converted body.");
        }
    }

    n(s sVar, Object[] objArr, Call.Factory factory, h<ResponseBody, T> hVar) {
        this.f1669b = sVar;
        this.f1670c = objArr;
        this.f1671d = factory;
        this.f1672e = hVar;
    }

    private Call b() throws IOException {
        Call callNewCall = this.f1671d.newCall(this.f1669b.a(this.f1670c));
        if (callNewCall != null) {
            return callNewCall;
        }
        throw new NullPointerException("Call.Factory returned null.");
    }

    @GuardedBy("this")
    private Call c() throws IOException {
        Call call = this.g;
        if (call != null) {
            return call;
        }
        Throwable th = this.h;
        if (th != null) {
            if (th instanceof IOException) {
                throw ((IOException) th);
            }
            if (th instanceof RuntimeException) {
                throw ((RuntimeException) th);
            }
            throw ((Error) th);
        }
        try {
            Call callB = b();
            this.g = callB;
            return callB;
        } catch (IOException | Error | RuntimeException e2) {
            y.s(e2);
            this.h = e2;
            throw e2;
        }
    }

    @Override // f.d
    /* JADX INFO: renamed from: a, reason: merged with bridge method [inline-methods] and merged with bridge method [inline-methods] */
    public n<T> m6clone() {
        return new n<>(this.f1669b, this.f1670c, this.f1671d, this.f1672e);
    }

    @Override // f.d
    public void cancel() {
        Call call;
        this.f1673f = true;
        synchronized (this) {
            call = this.g;
        }
        if (call != null) {
            call.cancel();
        }
    }

    t<T> d(Response response) throws IOException {
        ResponseBody responseBodyBody = response.body();
        Response responseBuild = response.newBuilder().body(new c(responseBodyBody.contentType(), responseBodyBody.contentLength())).build();
        int iCode = responseBuild.code();
        if (iCode < 200 || iCode >= 300) {
            try {
                return t.c(y.a(responseBodyBody), responseBuild);
            } finally {
                responseBodyBody.close();
            }
        }
        if (iCode == 204 || iCode == 205) {
            responseBodyBody.close();
            return t.f(null, responseBuild);
        }
        b bVar = new b(responseBodyBody);
        try {
            return t.f(this.f1672e.a(bVar), responseBuild);
        } catch (RuntimeException e2) {
            bVar.v();
            throw e2;
        }
    }

    @Override // f.d
    public t<T> execute() throws IOException {
        Call callC;
        synchronized (this) {
            if (this.i) {
                throw new IllegalStateException("Already executed.");
            }
            this.i = true;
            callC = c();
        }
        if (this.f1673f) {
            callC.cancel();
        }
        return d(callC.execute());
    }

    @Override // f.d
    public boolean isCanceled() {
        boolean z = true;
        if (this.f1673f) {
            return true;
        }
        synchronized (this) {
            if (this.g == null || !this.g.isCanceled()) {
                z = false;
            }
        }
        return z;
    }

    @Override // f.d
    public synchronized Request request() {
        try {
        } catch (IOException e2) {
            throw new RuntimeException("Unable to create request.", e2);
        }
        return c().request();
    }

    @Override // f.d
    public void v(f<T> fVar) {
        Call call;
        Throwable th;
        Objects.requireNonNull(fVar, "callback == null");
        synchronized (this) {
            if (this.i) {
                throw new IllegalStateException("Already executed.");
            }
            this.i = true;
            call = this.g;
            th = this.h;
            if (call == null && th == null) {
                try {
                    Call callB = b();
                    this.g = callB;
                    call = callB;
                } catch (Throwable th2) {
                    th = th2;
                    y.s(th);
                    this.h = th;
                }
            }
        }
        if (th != null) {
            fVar.a(this, th);
            return;
        }
        if (this.f1673f) {
            call.cancel();
        }
        call.enqueue(new a(fVar));
    }
}
