package defpackage;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Typeface;
import android.util.Base64;
import androidx.credentials.exceptions.publickeycredential.DomExceptionUtils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.zip.GZIPInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

/* JADX INFO: loaded from: classes.dex */
public class ek5 {
    public static final HashMap a = new HashMap();
    public static final HashSet b = new HashSet();
    public static final byte[] c = {80, 75, 3, 4};
    public static final byte[] d = {31, -117, 8};

    public static cl5 a(final String str, Callable callable, tb5 tb5Var) {
        zj5 zj5VarA = str == null ? null : ak5.b.a(str);
        cl5 cl5Var = zj5VarA != null ? new cl5(zj5VarA) : null;
        HashMap map = a;
        if (str != null && map.containsKey(str)) {
            cl5Var = (cl5) map.get(str);
        }
        if (cl5Var != null) {
            if (tb5Var != null) {
                tb5Var.run();
            }
            return cl5Var;
        }
        cl5 cl5Var2 = new cl5(callable, false);
        if (str != null) {
            final AtomicBoolean atomicBoolean = new AtomicBoolean(false);
            final int i = 0;
            cl5Var2.b(new sk5() { // from class: ck5
                @Override // defpackage.sk5
                public final void onResult(Object obj) {
                    switch (i) {
                        case 0:
                            HashMap map2 = ek5.a;
                            map2.remove(str);
                            atomicBoolean.set(true);
                            if (map2.size() == 0) {
                                ek5.j(true);
                            }
                            break;
                        default:
                            HashMap map3 = ek5.a;
                            map3.remove(str);
                            atomicBoolean.set(true);
                            if (map3.size() == 0) {
                                ek5.j(true);
                            }
                            break;
                    }
                }
            });
            final int i2 = 1;
            cl5Var2.a(new sk5() { // from class: ck5
                @Override // defpackage.sk5
                public final void onResult(Object obj) {
                    switch (i2) {
                        case 0:
                            HashMap map2 = ek5.a;
                            map2.remove(str);
                            atomicBoolean.set(true);
                            if (map2.size() == 0) {
                                ek5.j(true);
                            }
                            break;
                        default:
                            HashMap map3 = ek5.a;
                            map3.remove(str);
                            atomicBoolean.set(true);
                            if (map3.size() == 0) {
                                ek5.j(true);
                            }
                            break;
                    }
                }
            });
            if (!atomicBoolean.get()) {
                map.put(str, cl5Var2);
                if (map.size() == 1) {
                    j(false);
                }
            }
        }
        return cl5Var2;
    }

    public static bl5 b(Context context, String str, String str2) {
        zj5 zj5VarA = str2 == null ? null : ak5.b.a(str2);
        if (zj5VarA != null) {
            return new bl5(zj5VarA);
        }
        try {
            return c(context, context.getAssets().open(str), str2);
        } catch (IOException e) {
            return new bl5(e);
        }
    }

    public static bl5 c(Context context, InputStream inputStream, String str) {
        zj5 zj5VarA = str == null ? null : ak5.b.a(str);
        if (zj5VarA != null) {
            return new bl5(zj5VarA);
        }
        try {
            uu7 uu7VarD = eu6.d(eu6.i(inputStream));
            return i(uu7VarD, c).booleanValue() ? g(context, new ZipInputStream(new tu7(uu7VarD)), str) : i(uu7VarD, d).booleanValue() ? d(new GZIPInputStream(new tu7(uu7VarD)), str) : e(i05.z(uu7VarD), str, true);
        } catch (IOException e) {
            return new bl5(e);
        }
    }

    public static bl5 d(InputStream inputStream, String str) {
        return e(i05.z(eu6.d(eu6.i(inputStream))), str, true);
    }

    public static bl5 e(i05 i05Var, String str, boolean z) {
        zj5 zj5VarA;
        try {
            if (str == null) {
                zj5VarA = null;
            } else {
                try {
                    zj5VarA = ak5.b.a(str);
                } catch (Exception e) {
                    bl5 bl5Var = new bl5(e);
                    if (z) {
                        sda.b(i05Var);
                    }
                    return bl5Var;
                }
            }
            if (zj5VarA != null) {
                bl5 bl5Var2 = new bl5(zj5VarA);
                if (z) {
                    sda.b(i05Var);
                }
                return bl5Var2;
            }
            zj5 zj5VarA2 = fk5.a(i05Var);
            if (str != null) {
                ak5.b.a.put(str, zj5VarA2);
            }
            bl5 bl5Var3 = new bl5(zj5VarA2);
            if (z) {
                sda.b(i05Var);
            }
            return bl5Var3;
        } catch (Throwable th) {
            if (z) {
                sda.b(i05Var);
            }
            throw th;
        }
    }

    public static bl5 f(Context context, int i, String str) {
        zj5 zj5VarA = str == null ? null : ak5.b.a(str);
        if (zj5VarA != null) {
            return new bl5(zj5VarA);
        }
        try {
            uu7 uu7VarD = eu6.d(eu6.i(context.getResources().openRawResource(i)));
            if (i(uu7VarD, c).booleanValue()) {
                return g(context, new ZipInputStream(new tu7(uu7VarD)), str);
            }
            if (!i(uu7VarD, d).booleanValue()) {
                return e(i05.z(uu7VarD), str, true);
            }
            try {
                return d(new GZIPInputStream(new tu7(uu7VarD)), str);
            } catch (IOException e) {
                return new bl5(e);
            }
        } catch (Resources.NotFoundException e2) {
            return new bl5(e2);
        }
    }

    public static bl5 g(Context context, ZipInputStream zipInputStream, String str) {
        try {
            return h(context, zipInputStream, str);
        } finally {
            sda.b(zipInputStream);
        }
    }

    public static bl5 h(Context context, ZipInputStream zipInputStream, String str) {
        zj5 zj5VarA;
        nk5 nk5Var;
        FileOutputStream fileOutputStream;
        FileOutputStream fileOutputStream2;
        HashMap map = new HashMap();
        HashMap map2 = new HashMap();
        if (str == null) {
            zj5VarA = null;
        } else {
            try {
                zj5VarA = ak5.b.a(str);
            } catch (IOException e) {
                return new bl5(e);
            }
        }
        if (zj5VarA != null) {
            return new bl5(zj5VarA);
        }
        ZipEntry nextEntry = zipInputStream.getNextEntry();
        zj5 zj5Var = null;
        while (nextEntry != null) {
            String name = nextEntry.getName();
            if (name.contains("__MACOSX")) {
                zipInputStream.closeEntry();
            } else if (nextEntry.getName().equalsIgnoreCase("manifest.json")) {
                zipInputStream.closeEntry();
            } else if (nextEntry.getName().contains(".json")) {
                zj5Var = e(i05.z(eu6.d(eu6.i(zipInputStream))), null, false).a;
            } else if (name.contains(".png") || name.contains(".webp") || name.contains(".jpg") || name.contains(".jpeg")) {
                String[] strArrSplit = name.split(DomExceptionUtils.SEPARATOR);
                map.put(strArrSplit[strArrSplit.length - 1], BitmapFactory.decodeStream(zipInputStream));
            } else if (name.contains(".ttf") || name.contains(".otf")) {
                String[] strArrSplit2 = name.split(DomExceptionUtils.SEPARATOR);
                String str2 = strArrSplit2[strArrSplit2.length - 1];
                String str3 = str2.split("\\.")[0];
                if (context == null) {
                    return new bl5(new IllegalStateException("Unable to extract font " + str3 + " please pass a non-null Context parameter"));
                }
                File file = new File(context.getCacheDir(), str2);
                try {
                    fileOutputStream = new FileOutputStream(file);
                    try {
                        fileOutputStream2 = new FileOutputStream(file);
                    } finally {
                    }
                } catch (Throwable th) {
                    gh5.c("Unable to save font " + str3 + " to the temporary file: " + str2 + ". ", th);
                }
                try {
                    byte[] bArr = new byte[4096];
                    while (true) {
                        int i = zipInputStream.read(bArr);
                        if (i == -1) {
                            break;
                        }
                        fileOutputStream2.write(bArr, 0, i);
                    }
                    fileOutputStream2.flush();
                    fileOutputStream2.close();
                    fileOutputStream.close();
                    Typeface typefaceCreateFromFile = Typeface.createFromFile(file);
                    if (!file.delete()) {
                        gh5.b("Failed to delete temp font file " + file.getAbsolutePath() + ".");
                    }
                    map2.put(str3, typefaceCreateFromFile);
                } catch (Throwable th2) {
                    try {
                        fileOutputStream2.close();
                    } catch (Throwable th3) {
                        th2.addSuppressed(th3);
                    }
                    throw th2;
                }
            } else {
                zipInputStream.closeEntry();
            }
            nextEntry = zipInputStream.getNextEntry();
        }
        if (zj5Var == null) {
            return new bl5(new IllegalArgumentException("Unable to parse composition"));
        }
        for (Map.Entry entry : map.entrySet()) {
            String str4 = (String) entry.getKey();
            Iterator it = ((HashMap) zj5Var.c()).values().iterator();
            while (true) {
                if (!it.hasNext()) {
                    nk5Var = null;
                    break;
                }
                nk5Var = (nk5) it.next();
                if (nk5Var.d.equals(str4)) {
                    break;
                }
            }
            if (nk5Var != null) {
                nk5Var.f = sda.d((Bitmap) entry.getValue(), nk5Var.a, nk5Var.b);
            }
        }
        for (Map.Entry entry2 : map2.entrySet()) {
            boolean z = false;
            for (wg3 wg3Var : zj5Var.f.values()) {
                if (wg3Var.a.equals(entry2.getKey())) {
                    wg3Var.d = (Typeface) entry2.getValue();
                    z = true;
                }
            }
            if (!z) {
                gh5.b("Parsed font for " + ((String) entry2.getKey()) + " however it was not found in the animation.");
            }
        }
        if (map.isEmpty()) {
            Iterator it2 = ((HashMap) zj5Var.c()).entrySet().iterator();
            while (it2.hasNext()) {
                nk5 nk5Var2 = (nk5) ((Map.Entry) it2.next()).getValue();
                if (nk5Var2 == null) {
                    return null;
                }
                String str5 = nk5Var2.d;
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inScaled = true;
                options.inDensity = 160;
                if (str5.startsWith("data:") && str5.indexOf("base64,") > 0) {
                    try {
                        byte[] bArrDecode = Base64.decode(str5.substring(str5.indexOf(44) + 1), 0);
                        Bitmap bitmapDecodeByteArray = BitmapFactory.decodeByteArray(bArrDecode, 0, bArrDecode.length, options);
                        if (bitmapDecodeByteArray != null) {
                            nk5Var2.f = sda.d(bitmapDecodeByteArray, nk5Var2.a, nk5Var2.b);
                        }
                    } catch (IllegalArgumentException e2) {
                        gh5.c("data URL did not have correct base64 format.", e2);
                        return null;
                    }
                }
            }
        }
        if (str != null) {
            ak5.b.a.put(str, zj5Var);
        }
        return new bl5(zj5Var);
    }

    public static Boolean i(uu7 uu7Var, byte[] bArr) {
        try {
            uu7 uu7VarPeek = uu7Var.peek();
            for (byte b2 : bArr) {
                if (uu7VarPeek.readByte() != b2) {
                    return Boolean.FALSE;
                }
            }
            uu7VarPeek.close();
            return Boolean.TRUE;
        } catch (Exception unused) {
            gh5.a.getClass();
            return Boolean.FALSE;
        } catch (NoSuchMethodError unused2) {
            return Boolean.FALSE;
        }
    }

    public static void j(boolean z) {
        ArrayList arrayList = new ArrayList(b);
        for (int i = 0; i < arrayList.size(); i++) {
            ((dl5) arrayList.get(i)).a();
        }
    }

    public static String k(Context context, int i) {
        StringBuilder sb = new StringBuilder("rawRes");
        sb.append((context.getResources().getConfiguration().uiMode & 48) == 32 ? "_night_" : "_day_");
        sb.append(i);
        return sb.toString();
    }
}
