package lh;

import android.util.Log;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

/* loaded from: classes4.dex */
public final class q extends Exception {

    /* renamed from: v, reason: collision with root package name */
    private static final StackTraceElement[] f28233v = new StackTraceElement[0];

    /* renamed from: p, reason: collision with root package name */
    private final List f28234p;

    /* renamed from: q, reason: collision with root package name */
    private jh.f f28235q;

    /* renamed from: r, reason: collision with root package name */
    private jh.a f28236r;

    /* renamed from: s, reason: collision with root package name */
    private Class f28237s;

    /* renamed from: t, reason: collision with root package name */
    private String f28238t;

    /* renamed from: u, reason: collision with root package name */
    private Exception f28239u;

    public q(String str) {
        this(str, Collections.emptyList());
    }

    private void a(Throwable th2, List list) {
        if (!(th2 instanceof q)) {
            list.add(th2);
            return;
        }
        Iterator it = ((q) th2).e().iterator();
        while (it.hasNext()) {
            a((Throwable) it.next(), list);
        }
    }

    private static void b(List list, Appendable appendable) {
        try {
            c(list, appendable);
        } catch (IOException e10) {
            throw new RuntimeException(e10);
        }
    }

    private static void c(List list, Appendable appendable) {
        int size = list.size();
        int i10 = 0;
        while (i10 < size) {
            int i11 = i10 + 1;
            appendable.append("Cause (").append(String.valueOf(i11)).append(" of ").append(String.valueOf(size)).append("): ");
            Throwable th2 = (Throwable) list.get(i10);
            if (th2 instanceof q) {
                ((q) th2).h(appendable);
            } else {
                d(th2, appendable);
            }
            i10 = i11;
        }
    }

    private static void d(Throwable th2, Appendable appendable) {
        try {
            appendable.append(th2.getClass().toString()).append(": ").append(th2.getMessage()).append('\n');
        } catch (IOException unused) {
            throw new RuntimeException(th2);
        }
    }

    private void h(Appendable appendable) {
        d(this, appendable);
        b(e(), new a(appendable));
    }

    public List e() {
        return this.f28234p;
    }

    public List f() {
        ArrayList arrayList = new ArrayList();
        a(this, arrayList);
        return arrayList;
    }

    @Override // java.lang.Throwable
    public Throwable fillInStackTrace() {
        return this;
    }

    public void g(String str) {
        List f10 = f();
        int size = f10.size();
        int i10 = 0;
        while (i10 < size) {
            StringBuilder sb2 = new StringBuilder();
            sb2.append("Root cause (");
            int i11 = i10 + 1;
            sb2.append(i11);
            sb2.append(" of ");
            sb2.append(size);
            sb2.append(")");
            Log.i(str, sb2.toString(), (Throwable) f10.get(i10));
            i10 = i11;
        }
    }

    @Override // java.lang.Throwable
    public String getMessage() {
        StringBuilder sb2 = new StringBuilder(71);
        sb2.append(this.f28238t);
        sb2.append(this.f28237s != null ? ", " + this.f28237s : "");
        sb2.append(this.f28236r != null ? ", " + this.f28236r : "");
        sb2.append(this.f28235q != null ? ", " + this.f28235q : "");
        List<Throwable> f10 = f();
        if (f10.isEmpty()) {
            return sb2.toString();
        }
        if (f10.size() == 1) {
            sb2.append("\nThere was 1 root cause:");
        } else {
            sb2.append("\nThere were ");
            sb2.append(f10.size());
            sb2.append(" root causes:");
        }
        for (Throwable th2 : f10) {
            sb2.append('\n');
            sb2.append(th2.getClass().getName());
            sb2.append('(');
            sb2.append(th2.getMessage());
            sb2.append(')');
        }
        sb2.append("\n call GlideException#logRootCauses(String) for more detail");
        return sb2.toString();
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public void i(jh.f fVar, jh.a aVar) {
        j(fVar, aVar, null);
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public void j(jh.f fVar, jh.a aVar, Class cls) {
        this.f28235q = fVar;
        this.f28236r = aVar;
        this.f28237s = cls;
    }

    public void k(Exception exc) {
        this.f28239u = exc;
    }

    @Override // java.lang.Throwable
    public void printStackTrace() {
        printStackTrace(System.err);
    }

    public q(String str, Throwable th2) {
        this(str, Collections.singletonList(th2));
    }

    @Override // java.lang.Throwable
    public void printStackTrace(PrintStream printStream) {
        h(printStream);
    }

    public q(String str, List list) {
        this.f28238t = str;
        setStackTrace(f28233v);
        this.f28234p = list;
    }

    @Override // java.lang.Throwable
    public void printStackTrace(PrintWriter printWriter) {
        h(printWriter);
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes4.dex */
    public static final class a implements Appendable {

        /* renamed from: p, reason: collision with root package name */
        private final Appendable f28240p;

        /* renamed from: q, reason: collision with root package name */
        private boolean f28241q = true;

        a(Appendable appendable) {
            this.f28240p = appendable;
        }

        private CharSequence a(CharSequence charSequence) {
            return charSequence == null ? "" : charSequence;
        }

        @Override // java.lang.Appendable
        public Appendable append(char c10) {
            if (this.f28241q) {
                this.f28241q = false;
                this.f28240p.append("  ");
            }
            this.f28241q = c10 == '\n';
            this.f28240p.append(c10);
            return this;
        }

        @Override // java.lang.Appendable
        public Appendable append(CharSequence charSequence) {
            CharSequence a10 = a(charSequence);
            return append(a10, 0, a10.length());
        }

        @Override // java.lang.Appendable
        public Appendable append(CharSequence charSequence, int i10, int i11) {
            CharSequence a10 = a(charSequence);
            boolean z10 = false;
            if (this.f28241q) {
                this.f28241q = false;
                this.f28240p.append("  ");
            }
            if (a10.length() > 0 && a10.charAt(i11 - 1) == '\n') {
                z10 = true;
            }
            this.f28241q = z10;
            this.f28240p.append(a10, i10, i11);
            return this;
        }
    }
}
