package z1;

import C1.l;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.net.Uri;
import android.util.Log;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.util.List;
import n.d0;
import n2.AbstractC2612c;
import n2.AbstractC2613d;

/* JADX INFO: loaded from: classes.dex */
public final class h extends AbstractC2612c {

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public static final Class f26284c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public static final Constructor f26285d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public static final Method f26286e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public static final Method f26287f;

    static {
        Class<?> cls;
        Constructor<?> constructor;
        Method method;
        Method method2;
        try {
            cls = Class.forName("android.graphics.FontFamily");
            constructor = cls.getConstructor(new Class[0]);
            Class cls2 = Integer.TYPE;
            method = cls.getMethod("addFontWeightStyle", ByteBuffer.class, cls2, List.class, cls2, Boolean.TYPE);
            method2 = Typeface.class.getMethod("createFromFamiliesWithDefault", Array.newInstance(cls, 1).getClass());
        } catch (ClassNotFoundException | NoSuchMethodException e6) {
            Log.e("TypefaceCompatApi24Impl", e6.getClass().getName(), e6);
            cls = null;
            constructor = null;
            method = null;
            method2 = null;
        }
        f26285d = constructor;
        f26284c = cls;
        f26286e = method;
        f26287f = method2;
    }

    public static boolean x(Object obj, ByteBuffer byteBuffer, int i6, int i7, boolean z6) {
        try {
            return ((Boolean) f26286e.invoke(obj, byteBuffer, Integer.valueOf(i6), null, Integer.valueOf(i7), Boolean.valueOf(z6))).booleanValue();
        } catch (IllegalAccessException | InvocationTargetException unused) {
            return false;
        }
    }

    public static Typeface y(Object obj) {
        try {
            Object objNewInstance = Array.newInstance((Class<?>) f26284c, 1);
            Array.set(objNewInstance, 0, obj);
            return (Typeface) f26287f.invoke(null, objNewInstance);
        } catch (IllegalAccessException | InvocationTargetException unused) {
            return null;
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:33:0x005c  */
    /* JADX WARN: Removed duplicated region for block: B:54:0x0068 A[SYNTHETIC] */
    @Override // n2.AbstractC2612c
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Typeface f(Context context, y1.e eVar, Resources resources) {
        Object objNewInstance;
        MappedByteBuffer map;
        FileInputStream fileInputStream;
        try {
            objNewInstance = f26285d.newInstance(new Object[0]);
        } catch (IllegalAccessException | InstantiationException | InvocationTargetException unused) {
            objNewInstance = null;
        }
        if (objNewInstance != null) {
            for (y1.f fVar : eVar.f26149a) {
                int i6 = fVar.f26155f;
                File fileH = AbstractC2613d.h(context);
                if (fileH != null) {
                    try {
                        if (AbstractC2613d.f(fileH, resources, i6)) {
                            try {
                                fileInputStream = new FileInputStream(fileH);
                            } catch (IOException unused2) {
                                map = null;
                            }
                            try {
                                FileChannel channel = fileInputStream.getChannel();
                                map = channel.map(FileChannel.MapMode.READ_ONLY, 0L, channel.size());
                                fileInputStream.close();
                                if (map == null) {
                                    if (x(objNewInstance, map, fVar.f26154e, fVar.f26151b, fVar.f26152c)) {
                                    }
                                }
                            } finally {
                            }
                        }
                    } finally {
                        fileH.delete();
                    }
                }
                map = null;
                if (map == null) {
                }
            }
            return y(objNewInstance);
        }
        return null;
    }

    @Override // n2.AbstractC2612c
    public final Typeface g(Context context, l[] lVarArr, int i6) {
        Object objNewInstance;
        int i7 = 0;
        try {
            objNewInstance = f26285d.newInstance(new Object[0]);
        } catch (IllegalAccessException | InstantiationException | InvocationTargetException unused) {
            objNewInstance = null;
        }
        if (objNewInstance != null) {
            d0 d0Var = new d0(0);
            int length = lVarArr.length;
            while (true) {
                if (i7 >= length) {
                    Typeface typefaceY = y(objNewInstance);
                    if (typefaceY != null) {
                        return Typeface.create(typefaceY, i6);
                    }
                } else {
                    l lVar = lVarArr[i7];
                    Uri uri = lVar.f692a;
                    ByteBuffer byteBufferJ = (ByteBuffer) d0Var.get(uri);
                    if (byteBufferJ == null) {
                        byteBufferJ = AbstractC2613d.j(context, uri);
                        d0Var.put(uri, byteBufferJ);
                    }
                    if (byteBufferJ == null) {
                        break;
                    }
                    if (!x(objNewInstance, byteBufferJ, lVar.f693b, lVar.f694c, lVar.f695d)) {
                        break;
                    }
                    i7++;
                }
            }
        }
        return null;
    }
}
