package p138.p176.p291.p315.p317;

import android.util.Base64;
import android.util.JsonWriter;
import com.google.firebase.encoders.EncodingException;
import java.io.IOException;
import java.io.Writer;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import p138.p176.p291.p315.InterfaceC5883;
import p138.p176.p291.p315.InterfaceC5884;
import p138.p176.p291.p315.InterfaceC5885;
import p138.p176.p291.p315.InterfaceC5886;

/* JADX INFO: renamed from: ދ.ހ.ށ.ޅ.ޅ.ރ, reason: contains not printable characters */
/* JADX INFO: loaded from: classes.dex */
public final class C5895 implements InterfaceC5884, InterfaceC5886 {

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public C5895 f16654 = null;

    /* JADX INFO: renamed from: ؠ, reason: contains not printable characters */
    public boolean f16655 = true;

    /* JADX INFO: renamed from: ހ, reason: contains not printable characters */
    public final JsonWriter f16656;

    /* JADX INFO: renamed from: ށ, reason: contains not printable characters */
    public final Map<Class<?>, InterfaceC5883<?>> f16657;

    /* JADX INFO: renamed from: ނ, reason: contains not printable characters */
    public final Map<Class<?>, InterfaceC5885<?>> f16658;

    /* JADX INFO: renamed from: ރ, reason: contains not printable characters */
    public final InterfaceC5883<Object> f16659;

    /* JADX INFO: renamed from: ބ, reason: contains not printable characters */
    public final boolean f16660;

    public C5895(Writer writer, Map<Class<?>, InterfaceC5883<?>> map, Map<Class<?>, InterfaceC5885<?>> map2, InterfaceC5883<Object> interfaceC5883, boolean z) {
        this.f16656 = new JsonWriter(writer);
        this.f16657 = map;
        this.f16658 = map2;
        this.f16659 = interfaceC5883;
        this.f16660 = z;
    }

    @Override // p138.p176.p291.p315.InterfaceC5884
    /* JADX INFO: renamed from: ֏ */
    public InterfaceC5884 mo9217(String str, int i) throws IOException {
        m9228();
        this.f16656.name(str);
        m9228();
        this.f16656.value(i);
        return this;
    }

    @Override // p138.p176.p291.p315.InterfaceC5884
    /* JADX INFO: renamed from: ֏ */
    public InterfaceC5884 mo9218(String str, long j) throws IOException {
        m9228();
        this.f16656.name(str);
        m9228();
        this.f16656.value(j);
        return this;
    }

    @Override // p138.p176.p291.p315.InterfaceC5884
    /* JADX INFO: renamed from: ֏ */
    public InterfaceC5884 mo9220(String str, boolean z) throws IOException {
        m9228();
        this.f16656.name(str);
        m9228();
        this.f16656.value(z);
        return this;
    }

    @Override // p138.p176.p291.p315.InterfaceC5886
    /* JADX INFO: renamed from: ֏ */
    public InterfaceC5886 mo9221(String str) throws IOException {
        m9228();
        this.f16656.value(str);
        return this;
    }

    @Override // p138.p176.p291.p315.InterfaceC5886
    /* JADX INFO: renamed from: ֏ */
    public InterfaceC5886 mo9222(boolean z) throws IOException {
        m9228();
        this.f16656.value(z);
        return this;
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public C5895 m9227(Object obj, boolean z) throws IOException {
        int i = 0;
        if (z) {
            if (obj == null || obj.getClass().isArray() || (obj instanceof Collection) || (obj instanceof Date) || (obj instanceof Enum) || (obj instanceof Number)) {
                Object[] objArr = new Object[1];
                objArr[0] = obj == null ? null : obj.getClass();
                throw new EncodingException(String.format("%s cannot be encoded inline", objArr));
            }
        }
        if (obj == null) {
            this.f16656.nullValue();
            return this;
        }
        if (obj instanceof Number) {
            this.f16656.value((Number) obj);
            return this;
        }
        if (!obj.getClass().isArray()) {
            if (obj instanceof Collection) {
                this.f16656.beginArray();
                Iterator it = ((Collection) obj).iterator();
                while (it.hasNext()) {
                    m9227(it.next(), false);
                }
                this.f16656.endArray();
                return this;
            }
            if (obj instanceof Map) {
                this.f16656.beginObject();
                for (Map.Entry entry : ((Map) obj).entrySet()) {
                    Object key = entry.getKey();
                    try {
                        mo9219((String) key, entry.getValue());
                    } catch (ClassCastException e) {
                        throw new EncodingException(String.format("Only String keys are currently supported in maps, got %s of type %s instead.", key, key.getClass()), e);
                    }
                }
                this.f16656.endObject();
                return this;
            }
            InterfaceC5883<?> interfaceC5883 = this.f16657.get(obj.getClass());
            if (interfaceC5883 != null) {
                if (!z) {
                    this.f16656.beginObject();
                }
                interfaceC5883.mo5350(obj, this);
                if (!z) {
                    this.f16656.endObject();
                }
                return this;
            }
            InterfaceC5885<?> interfaceC5885 = this.f16658.get(obj.getClass());
            if (interfaceC5885 != null) {
                interfaceC5885.mo5350(obj, this);
                return this;
            }
            if (obj instanceof Enum) {
                String strName = ((Enum) obj).name();
                m9228();
                this.f16656.value(strName);
                return this;
            }
            InterfaceC5883<Object> interfaceC58832 = this.f16659;
            if (!z) {
                this.f16656.beginObject();
            }
            interfaceC58832.mo5350(obj, this);
            if (!z) {
                this.f16656.endObject();
            }
            return this;
        }
        if (obj instanceof byte[]) {
            m9228();
            this.f16656.value(Base64.encodeToString((byte[]) obj, 2));
            return this;
        }
        this.f16656.beginArray();
        if (obj instanceof int[]) {
            int length = ((int[]) obj).length;
            while (i < length) {
                this.f16656.value(r6[i]);
                i++;
            }
        } else if (obj instanceof long[]) {
            long[] jArr = (long[]) obj;
            int length2 = jArr.length;
            while (i < length2) {
                long j = jArr[i];
                m9228();
                this.f16656.value(j);
                i++;
            }
        } else if (obj instanceof double[]) {
            double[] dArr = (double[]) obj;
            int length3 = dArr.length;
            while (i < length3) {
                this.f16656.value(dArr[i]);
                i++;
            }
        } else if (obj instanceof boolean[]) {
            boolean[] zArr = (boolean[]) obj;
            int length4 = zArr.length;
            while (i < length4) {
                this.f16656.value(zArr[i]);
                i++;
            }
        } else if (obj instanceof Number[]) {
            for (Number number : (Number[]) obj) {
                m9227((Object) number, false);
            }
        } else {
            for (Object obj2 : (Object[]) obj) {
                m9227(obj2, false);
            }
        }
        this.f16656.endArray();
        return this;
    }

    @Override // p138.p176.p291.p315.InterfaceC5884
    /* JADX INFO: renamed from: ֏ */
    public C5895 mo9219(String str, Object obj) throws IOException {
        if (this.f16660) {
            if (obj == null) {
                return this;
            }
            m9228();
            this.f16656.name(str);
            return m9227(obj, false);
        }
        m9228();
        this.f16656.name(str);
        if (obj != null) {
            return m9227(obj, false);
        }
        this.f16656.nullValue();
        return this;
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public final void m9228() throws IOException {
        if (!this.f16655) {
            throw new IllegalStateException("Parent context used since this context was created. Cannot use this context anymore.");
        }
        C5895 c5895 = this.f16654;
        if (c5895 != null) {
            c5895.m9228();
            this.f16654.f16655 = false;
            this.f16654 = null;
            this.f16656.endObject();
        }
    }
}
