package com.bytedance.adsdk.ro;

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 android.util.JsonReader;
import com.bytedance.adsdk.ro.jnr.lse;
import defpackage.e;
import java.io.Closeable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

/* JADX INFO: loaded from: classes.dex */
public class wu {
    private static final Map<String, dsz<wsy>> fm = new HashMap();
    private static final Set<Object> ro = new HashSet();

    /* JADX INFO: renamed from: lb, reason: collision with root package name */
    private static final byte[] f6298lb = {80, 75, 3, 4};

    public static duv<wsy> lb(Context context, String str) {
        return lb(context, str, "asset_".concat(String.valueOf(str)));
    }

    public static dsz<wsy> ro(Context context, String str) {
        return ro(context, str, "asset_".concat(String.valueOf(str)));
    }

    public static dsz<wsy> fm(Context context, String str) {
        return fm(context, str, "url_".concat(String.valueOf(str)));
    }

    public static duv<wsy> lb(Context context, String str, String str2) {
        try {
            if (!str.endsWith(".zip") && !str.endsWith(".lottie")) {
                return ro(context.getAssets().open(str), str2);
            }
            return fm(context, new ZipInputStream(context.getAssets().open(str)), str2);
        } catch (IOException e10) {
            return new duv<>((Throwable) e10);
        }
    }

    public static dsz<wsy> ro(Context context, final String str, final String str2) {
        final Context applicationContext = context.getApplicationContext();
        return fm(str2, new Callable<duv<wsy>>() { // from class: com.bytedance.adsdk.ro.wu.4
            @Override // java.util.concurrent.Callable
            /* JADX INFO: renamed from: fm, reason: merged with bridge method [inline-methods] */
            public duv<wsy> call() throws Exception {
                return wu.lb(applicationContext, str, str2);
            }
        });
    }

    public static dsz<wsy> fm(final Context context, final String str, final String str2) {
        return fm(str2, new Callable<duv<wsy>>() { // from class: com.bytedance.adsdk.ro.wu.1
            @Override // java.util.concurrent.Callable
            /* JADX INFO: renamed from: fm, reason: merged with bridge method [inline-methods] */
            public duv<wsy> call() throws Exception {
                duv<wsy> duvVarFm = jnr.fm(context).fm(context, str, str2);
                if (str2 != null && duvVarFm.fm() != null) {
                    com.bytedance.adsdk.ro.lb.jnr.fm().fm(str2, duvVarFm.fm());
                }
                return duvVarFm;
            }
        });
    }

    public static dsz<wsy> fm(Context context, int i10) {
        return fm(context, i10, lb(context, i10));
    }

    public static duv<wsy> ro(Context context, int i10) {
        return ro(context, i10, lb(context, i10));
    }

    public static dsz<wsy> fm(Context context, final int i10, final String str) {
        final WeakReference weakReference = new WeakReference(context);
        final Context applicationContext = context.getApplicationContext();
        return fm(str, new Callable<duv<wsy>>() { // from class: com.bytedance.adsdk.ro.wu.5
            @Override // java.util.concurrent.Callable
            /* JADX INFO: renamed from: fm, reason: merged with bridge method [inline-methods] */
            public duv<wsy> call() throws Exception {
                Context context2 = (Context) weakReference.get();
                if (context2 == null) {
                    context2 = applicationContext;
                }
                return wu.ro(context2, i10, str);
            }
        });
    }

    public static duv<wsy> ro(Context context, int i10, String str) {
        try {
            return ro(context.getResources().openRawResource(i10), lb(context, i10));
        } catch (Resources.NotFoundException e10) {
            return new duv<>((Throwable) e10);
        }
    }

    private static String lb(Context context, int i10) {
        return e.n(new StringBuilder("rawRes"), fm(context) ? "_night_" : "_day_", i10);
    }

    public static duv<wsy> ro(InputStream inputStream, String str) {
        return fm(inputStream, str, true);
    }

    private static boolean fm(Context context) {
        return (context.getResources().getConfiguration().uiMode & 48) == 32;
    }

    private static duv<wsy> ro(Context context, ZipInputStream zipInputStream, String str) {
        HashMap map = new HashMap();
        HashMap map2 = new HashMap();
        try {
            ZipEntry nextEntry = zipInputStream.getNextEntry();
            wsy wsyVarFm = 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().endsWith(".json")) {
                    wsyVarFm = fm(new JsonReader(new InputStreamReader(zipInputStream)), (String) null, false).fm();
                } else if (!name.endsWith(".png") && !name.endsWith(".webp") && !name.endsWith(".jpg") && !name.endsWith(".jpeg")) {
                    if (!name.endsWith(".ttf") && !name.endsWith(".otf")) {
                        zipInputStream.closeEntry();
                    } else if (name.contains("../")) {
                        zipInputStream.closeEntry();
                        nextEntry = zipInputStream.getNextEntry();
                    } else {
                        String[] strArrSplit = name.split("/");
                        String str2 = strArrSplit[strArrSplit.length - 1];
                        String str3 = str2.split("\\.")[0];
                        File file = new File(context.getCacheDir(), str2);
                        new FileOutputStream(file);
                        try {
                            FileOutputStream fileOutputStream = new FileOutputStream(file);
                            try {
                                byte[] bArr = new byte[4096];
                                while (true) {
                                    int i10 = zipInputStream.read(bArr);
                                    if (i10 == -1) {
                                        break;
                                    }
                                    fileOutputStream.write(bArr, 0, i10);
                                }
                                fileOutputStream.flush();
                                fileOutputStream.close();
                            } catch (Throwable th) {
                                try {
                                    throw th;
                                } catch (Throwable th2) {
                                    try {
                                        fileOutputStream.close();
                                    } catch (Throwable th3) {
                                        th.addSuppressed(th3);
                                    }
                                    throw th2;
                                }
                            }
                        } catch (Throwable unused) {
                        }
                        Typeface typefaceCreateFromFile = Typeface.createFromFile(file);
                        if (!file.delete()) {
                            file.getAbsolutePath();
                        }
                        map2.put(str3, typefaceCreateFromFile);
                    }
                } else if (name.contains("../")) {
                    zipInputStream.closeEntry();
                    nextEntry = zipInputStream.getNextEntry();
                } else {
                    String[] strArrSplit2 = name.split("/");
                    map.put(strArrSplit2[strArrSplit2.length - 1], BitmapFactory.decodeStream(zipInputStream));
                }
                nextEntry = zipInputStream.getNextEntry();
            }
            if (wsyVarFm == null) {
                return new duv<>((Throwable) new IllegalArgumentException("Unable to parse composition"));
            }
            for (Map.Entry entry : map.entrySet()) {
                vt vtVarFm = fm(wsyVarFm, (String) entry.getKey());
                if (vtVarFm != null) {
                    vtVarFm.fm(com.bytedance.adsdk.ro.ajl.ajl.fm((Bitmap) entry.getValue(), vtVarFm.fm(), vtVarFm.ro()));
                }
            }
            for (Map.Entry entry2 : map2.entrySet()) {
                boolean z10 = false;
                for (com.bytedance.adsdk.ro.lb.lb lbVar : wsyVarFm.fhx().values()) {
                    if (lbVar.fm().equals(entry2.getKey())) {
                        lbVar.fm((Typeface) entry2.getValue());
                        z10 = true;
                    }
                }
                if (!z10) {
                }
            }
            if (map.isEmpty()) {
                Iterator<Map.Entry<String, vt>> it = wsyVarFm.irt().entrySet().iterator();
                while (it.hasNext()) {
                    vt value = it.next().getValue();
                    if (value == null) {
                        return null;
                    }
                    String strEf = value.ef();
                    BitmapFactory.Options options = new BitmapFactory.Options();
                    options.inScaled = true;
                    options.inDensity = 160;
                    if (strEf.startsWith("data:") && strEf.indexOf("base64,") > 0) {
                        try {
                            byte[] bArrDecode = Base64.decode(strEf.substring(strEf.indexOf(44) + 1), 0);
                            value.fm(BitmapFactory.decodeByteArray(bArrDecode, 0, bArrDecode.length, options));
                        } catch (IllegalArgumentException unused2) {
                            return null;
                        }
                    }
                }
            }
            for (Map.Entry<String, vt> entry3 : wsyVarFm.irt().entrySet()) {
                if (entry3.getValue().ku() == null) {
                    return new duv<>((Throwable) new IllegalStateException("There is no image for " + entry3.getValue().ef()));
                }
            }
            if (str != null) {
                com.bytedance.adsdk.ro.lb.jnr.fm().fm(str, wsyVarFm);
            }
            return new duv<>(wsyVarFm);
        } catch (IOException e10) {
            return new duv<>((Throwable) e10);
        }
    }

    public static dsz<wsy> fm(final InputStream inputStream, final String str) {
        return fm(str, new Callable<duv<wsy>>() { // from class: com.bytedance.adsdk.ro.wu.6
            @Override // java.util.concurrent.Callable
            /* JADX INFO: renamed from: fm, reason: merged with bridge method [inline-methods] */
            public duv<wsy> call() throws Exception {
                return wu.ro(inputStream, str);
            }
        });
    }

    private static duv<wsy> fm(InputStream inputStream, String str, boolean z10) {
        try {
            return fm(new JsonReader(new InputStreamReader(inputStream)), str);
        } finally {
            if (z10) {
                com.bytedance.adsdk.ro.ajl.ajl.fm(inputStream);
            }
        }
    }

    public static duv<wsy> fm(JsonReader jsonReader, String str) {
        return fm(jsonReader, str, true);
    }

    private static duv<wsy> fm(JsonReader jsonReader, String str, boolean z10) {
        try {
            try {
                wsy wsyVarFm = lse.fm(jsonReader);
                com.bytedance.adsdk.ro.lb.jnr.fm().fm(str, wsyVarFm);
                duv<wsy> duvVar = new duv<>(wsyVarFm);
                if (z10) {
                    fm(jsonReader);
                }
                return duvVar;
            } catch (Exception e10) {
                duv<wsy> duvVar2 = new duv<>(e10);
                if (z10) {
                    fm(jsonReader);
                }
                return duvVar2;
            }
        } catch (Throwable th) {
            if (z10) {
                fm(jsonReader);
            }
            throw th;
        }
    }

    public static void fm(Closeable closeable) {
        if (closeable != null) {
            try {
                closeable.close();
            } catch (RuntimeException e10) {
                throw e10;
            } catch (Exception unused) {
            }
        }
    }

    public static duv<wsy> fm(Context context, ZipInputStream zipInputStream, String str) {
        try {
            return ro(context, zipInputStream, str);
        } finally {
            com.bytedance.adsdk.ro.ajl.ajl.fm(zipInputStream);
        }
    }

    private static vt fm(wsy wsyVar, String str) {
        for (vt vtVar : wsyVar.irt().values()) {
            if (vtVar.ef().equals(str)) {
                return vtVar;
            }
        }
        return null;
    }

    private static dsz<wsy> fm(final String str, Callable<duv<wsy>> callable) {
        final wsy wsyVarFm = str == null ? null : com.bytedance.adsdk.ro.lb.jnr.fm().fm(str);
        if (wsyVarFm != null) {
            return new dsz<>(new Callable<duv<wsy>>() { // from class: com.bytedance.adsdk.ro.wu.7
                @Override // java.util.concurrent.Callable
                /* JADX INFO: renamed from: fm, reason: merged with bridge method [inline-methods] */
                public duv<wsy> call() throws Exception {
                    return new duv<>(wsyVarFm);
                }
            });
        }
        if (str != null) {
            Map<String, dsz<wsy>> map = fm;
            if (map.containsKey(str)) {
                return map.get(str);
            }
        }
        dsz<wsy> dszVar = new dsz<>(callable);
        if (str != null) {
            final AtomicBoolean atomicBoolean = new AtomicBoolean(false);
            dszVar.fm(new ku<wsy>() { // from class: com.bytedance.adsdk.ro.wu.2
                @Override // com.bytedance.adsdk.ro.ku
                public void fm(wsy wsyVar) {
                    wu.fm.remove(str);
                    atomicBoolean.set(true);
                    if (wu.fm.size() == 0) {
                        wu.ro(true);
                    }
                }
            });
            dszVar.lb(new ku<Throwable>() { // from class: com.bytedance.adsdk.ro.wu.3
                @Override // com.bytedance.adsdk.ro.ku
                public void fm(Throwable th) {
                    wu.fm.remove(str);
                    atomicBoolean.set(true);
                    if (wu.fm.size() == 0) {
                        wu.ro(true);
                    }
                }
            });
            if (!atomicBoolean.get()) {
                Map<String, dsz<wsy>> map2 = fm;
                map2.put(str, dszVar);
                if (map2.size() == 1) {
                    ro(false);
                }
            }
        }
        return dszVar;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static void ro(boolean z10) {
        ArrayList arrayList = new ArrayList(ro);
        for (int i10 = 0; i10 < arrayList.size(); i10++) {
            arrayList.get(i10);
        }
    }
}
