package U2;

import S2.f;
import S2.g;
import android.util.Base64;
import android.util.JsonWriter;
import java.io.BufferedWriter;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/* JADX INFO: loaded from: classes.dex */
public final class e implements S2.e, g {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final boolean f2917a = true;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final JsonWriter f2918b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final HashMap f2919c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final HashMap f2920d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final a f2921e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final boolean f2922f;

    public e(BufferedWriter bufferedWriter, HashMap map, HashMap map2, a aVar, boolean z5) {
        this.f2918b = new JsonWriter(bufferedWriter);
        this.f2919c = map;
        this.f2920d = map2;
        this.f2921e = aVar;
        this.f2922f = z5;
    }

    @Override // S2.e
    public final S2.e a(S2.c cVar, int i) throws IOException {
        String str = cVar.f2809a;
        h();
        JsonWriter jsonWriter = this.f2918b;
        jsonWriter.name(str);
        h();
        jsonWriter.value(i);
        return this;
    }

    @Override // S2.e
    public final S2.e b(S2.c cVar, long j5) throws IOException {
        String str = cVar.f2809a;
        h();
        JsonWriter jsonWriter = this.f2918b;
        jsonWriter.name(str);
        h();
        jsonWriter.value(j5);
        return this;
    }

    @Override // S2.g
    public final g c(String str) throws IOException {
        h();
        this.f2918b.value(str);
        return this;
    }

    @Override // S2.g
    public final g d(boolean z5) throws IOException {
        h();
        this.f2918b.value(z5);
        return this;
    }

    @Override // S2.e
    public final S2.e e(S2.c cVar, Object obj) throws IOException {
        g(cVar.f2809a, obj);
        return this;
    }

    public final e f(Object obj) throws IOException {
        JsonWriter jsonWriter = this.f2918b;
        if (obj == null) {
            jsonWriter.nullValue();
            return this;
        }
        if (obj instanceof Number) {
            jsonWriter.value((Number) obj);
            return this;
        }
        if (!obj.getClass().isArray()) {
            if (obj instanceof Collection) {
                jsonWriter.beginArray();
                Iterator it = ((Collection) obj).iterator();
                while (it.hasNext()) {
                    f(it.next());
                }
                jsonWriter.endArray();
                return this;
            }
            if (obj instanceof Map) {
                jsonWriter.beginObject();
                for (Map.Entry entry : ((Map) obj).entrySet()) {
                    Object key = entry.getKey();
                    try {
                        g((String) key, entry.getValue());
                    } catch (ClassCastException e2) {
                        throw new S2.b(String.format("Only String keys are currently supported in maps, got %s of type %s instead.", key, key.getClass()), e2);
                    }
                }
                jsonWriter.endObject();
                return this;
            }
            S2.d dVar = (S2.d) this.f2919c.get(obj.getClass());
            if (dVar != null) {
                jsonWriter.beginObject();
                dVar.a(obj, this);
                jsonWriter.endObject();
                return this;
            }
            f fVar = (f) this.f2920d.get(obj.getClass());
            if (fVar != null) {
                fVar.a(obj, this);
                return this;
            }
            if (!(obj instanceof Enum)) {
                jsonWriter.beginObject();
                this.f2921e.a(obj, this);
                throw null;
            }
            String strName = ((Enum) obj).name();
            h();
            jsonWriter.value(strName);
            return this;
        }
        if (obj instanceof byte[]) {
            h();
            jsonWriter.value(Base64.encodeToString((byte[]) obj, 2));
            return this;
        }
        jsonWriter.beginArray();
        int i = 0;
        if (obj instanceof int[]) {
            int length = ((int[]) obj).length;
            while (i < length) {
                jsonWriter.value(r6[i]);
                i++;
            }
        } else if (obj instanceof long[]) {
            long[] jArr = (long[]) obj;
            int length2 = jArr.length;
            while (i < length2) {
                long j5 = jArr[i];
                h();
                jsonWriter.value(j5);
                i++;
            }
        } else if (obj instanceof double[]) {
            double[] dArr = (double[]) obj;
            int length3 = dArr.length;
            while (i < length3) {
                jsonWriter.value(dArr[i]);
                i++;
            }
        } else if (obj instanceof boolean[]) {
            boolean[] zArr = (boolean[]) obj;
            int length4 = zArr.length;
            while (i < length4) {
                jsonWriter.value(zArr[i]);
                i++;
            }
        } else if (obj instanceof Number[]) {
            Number[] numberArr = (Number[]) obj;
            int length5 = numberArr.length;
            while (i < length5) {
                f(numberArr[i]);
                i++;
            }
        } else {
            Object[] objArr = (Object[]) obj;
            int length6 = objArr.length;
            while (i < length6) {
                f(objArr[i]);
                i++;
            }
        }
        jsonWriter.endArray();
        return this;
    }

    public final e g(String str, Object obj) throws IOException {
        boolean z5 = this.f2922f;
        JsonWriter jsonWriter = this.f2918b;
        if (z5) {
            if (obj == null) {
                return this;
            }
            h();
            jsonWriter.name(str);
            f(obj);
            return this;
        }
        h();
        jsonWriter.name(str);
        if (obj == null) {
            jsonWriter.nullValue();
            return this;
        }
        f(obj);
        return this;
    }

    public final void h() {
        if (!this.f2917a) {
            throw new IllegalStateException("Parent context used since this context was created. Cannot use this context anymore.");
        }
    }
}
