package o;

import android.util.Base64;
import android.util.JsonWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/* compiled from: r8-map-id-3b7cc9102578c509342fa2f0fabed2bfc72239cd258d579fc679bfcbc198b048 */
/* renamed from: o.tJ, reason: case insensitive filesystem */
/* loaded from: classes.dex */
public final class C2514tJ implements InterfaceC1002dT, InterfaceC3023yi0 {
    public final boolean a = true;
    public final JsonWriter b;
    public final Map c;
    public final Map d;
    public final InterfaceC0906cT e;
    public final boolean f;

    public C2514tJ(Writer writer, HashMap map, HashMap map2, C1755lJ c1755lJ, boolean z) {
        this.b = new JsonWriter(writer);
        this.c = map;
        this.d = map2;
        this.e = c1755lJ;
        this.f = z;
    }

    @Override // o.InterfaceC1002dT
    public final InterfaceC1002dT a(C0412Px c0412Px, boolean z) throws IOException {
        String str = c0412Px.a;
        j();
        JsonWriter jsonWriter = this.b;
        jsonWriter.name(str);
        j();
        jsonWriter.value(z);
        return this;
    }

    @Override // o.InterfaceC1002dT
    public final InterfaceC1002dT b(C0412Px c0412Px, int i) throws IOException {
        String str = c0412Px.a;
        j();
        JsonWriter jsonWriter = this.b;
        jsonWriter.name(str);
        j();
        jsonWriter.value(i);
        return this;
    }

    @Override // o.InterfaceC1002dT
    public final InterfaceC1002dT c(C0412Px c0412Px, long j) throws IOException {
        String str = c0412Px.a;
        j();
        JsonWriter jsonWriter = this.b;
        jsonWriter.name(str);
        j();
        jsonWriter.value(j);
        return this;
    }

    @Override // o.InterfaceC1002dT
    public final InterfaceC1002dT d(C0412Px c0412Px, double d) throws IOException {
        String str = c0412Px.a;
        j();
        JsonWriter jsonWriter = this.b;
        jsonWriter.name(str);
        j();
        jsonWriter.value(d);
        return this;
    }

    @Override // o.InterfaceC3023yi0
    public final InterfaceC3023yi0 e(String str) throws IOException {
        j();
        this.b.value(str);
        return this;
    }

    @Override // o.InterfaceC3023yi0
    public final InterfaceC3023yi0 f(boolean z) throws IOException {
        j();
        this.b.value(z);
        return this;
    }

    @Override // o.InterfaceC1002dT
    public final InterfaceC1002dT g(C0412Px c0412Px, Object obj) throws IOException {
        i(obj, c0412Px.a);
        return this;
    }

    public final C2514tJ h(Object obj) {
        JsonWriter jsonWriter = this.b;
        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()) {
                    h(it.next());
                }
                jsonWriter.endArray();
                return this;
            }
            if (obj instanceof Map) {
                jsonWriter.beginObject();
                for (Map.Entry entry : ((Map) obj).entrySet()) {
                    Object key = entry.getKey();
                    try {
                        i(entry.getValue(), (String) key);
                    } catch (ClassCastException e) {
                        throw new C0644Yv(String.format("Only String keys are currently supported in maps, got %s of type %s instead.", key, key.getClass()), e);
                    }
                }
                jsonWriter.endObject();
                return this;
            }
            InterfaceC0906cT interfaceC0906cT = (InterfaceC0906cT) this.c.get(obj.getClass());
            if (interfaceC0906cT != null) {
                jsonWriter.beginObject();
                interfaceC0906cT.a(obj, this);
                jsonWriter.endObject();
                return this;
            }
            InterfaceC2928xi0 interfaceC2928xi0 = (InterfaceC2928xi0) this.d.get(obj.getClass());
            if (interfaceC2928xi0 != null) {
                interfaceC2928xi0.a(obj, this);
                return this;
            }
            if (!(obj instanceof Enum)) {
                jsonWriter.beginObject();
                this.e.a(obj, this);
                jsonWriter.endObject();
                return this;
            }
            if (obj instanceof InterfaceC0715aT) {
                int iA = ((InterfaceC0715aT) obj).a();
                j();
                jsonWriter.value(iA);
                return this;
            }
            String strName = ((Enum) obj).name();
            j();
            jsonWriter.value(strName);
            return this;
        }
        if (obj instanceof byte[]) {
            j();
            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 j = jArr[i];
                j();
                jsonWriter.value(j);
                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) {
                h(numberArr[i]);
                i++;
            }
        } else {
            Object[] objArr = (Object[]) obj;
            int length6 = objArr.length;
            while (i < length6) {
                h(objArr[i]);
                i++;
            }
        }
        jsonWriter.endArray();
        return this;
    }

    public final C2514tJ i(Object obj, String str) throws IOException {
        boolean z = this.f;
        JsonWriter jsonWriter = this.b;
        if (z) {
            if (obj == null) {
                return this;
            }
            j();
            jsonWriter.name(str);
            h(obj);
            return this;
        }
        j();
        jsonWriter.name(str);
        if (obj == null) {
            jsonWriter.nullValue();
            return this;
        }
        h(obj);
        return this;
    }

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