package com.seewo.factoryaging.opengl3d.i;

import android.util.Log;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.StringTokenizer;

/* JADX INFO: compiled from: WavefrontLoader.java */
/* JADX INFO: loaded from: classes.dex */
public class d {
    private c c;
    private e e;
    private FloatBuffer n;
    private FloatBuffer o;
    private boolean a = false;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    int f352g = 0;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    int f353h = 0;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    int f354i = 0;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    int f355j = 0;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    int f356k = 0;

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    int f357l = 0;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    int f358m = 0;
    private ArrayList<g> b = new ArrayList<>();
    private b d = new b();
    private f f = new f();

    /* JADX INFO: compiled from: WavefrontLoader.java */
    public static class b {
        private HashMap<Integer, String> a = new HashMap<>();
        private HashMap<String, Integer> b = new HashMap<>();

        public void a(int i2, String str) {
            if (this.a.containsKey(Integer.valueOf(i2))) {
                System.out.println("Face index " + i2 + " changed to use material " + str);
            }
            this.a.put(Integer.valueOf(i2), str);
            if (!this.b.containsKey(str)) {
                this.b.put(str, 1);
            } else {
                this.b.put(str, Integer.valueOf(this.b.get(str).intValue() + 1));
            }
        }

        public String b(int i2) {
            return this.a.get(Integer.valueOf(i2));
        }

        public boolean c() {
            return this.a.isEmpty() || this.b.isEmpty();
        }

        public void d() {
            System.out.println("No. of materials used: " + this.a.size());
            for (String str : this.b.keySet()) {
                int iIntValue = this.b.get(str).intValue();
                System.out.print(str + ": " + iIntValue);
                System.out.println();
            }
        }
    }

    /* JADX INFO: renamed from: com.seewo.factoryaging.opengl3d.i.d$d, reason: collision with other inner class name */
    /* JADX INFO: compiled from: WavefrontLoader.java */
    public static class C0012d {
        private String a;
        private float f = 1.0f;
        private float e = 0.0f;
        private g b = null;
        private g c = null;
        private g d = null;

        /* JADX INFO: renamed from: g, reason: collision with root package name */
        private String f363g = null;

        /* JADX INFO: renamed from: h, reason: collision with root package name */
        private String f364h = null;

        public C0012d(String str) {
            this.a = str;
        }

        public float a() {
            return this.f;
        }

        public float[] b() {
            g gVar = this.c;
            if (gVar == null) {
                return null;
            }
            return new float[]{gVar.a(), this.c.b(), this.c.c(), a()};
        }

        String c() {
            return this.a;
        }

        public String d() {
            return this.f364h;
        }

        public void e(float f) {
            this.f = f;
        }

        public void f(g gVar) {
            this.b = gVar;
        }

        public void g(g gVar) {
            this.c = gVar;
        }

        public void h(g gVar) {
            this.d = gVar;
        }

        public void i(float f) {
            this.e = f;
        }

        public void j(String str) {
            this.f364h = str;
        }

        public void k() {
            System.out.println(this.a);
            if (this.b != null) {
                System.out.println("  Ka: " + this.b.toString());
            }
            if (this.c != null) {
                System.out.println("  Kd: " + this.c.toString());
            }
            if (this.d != null) {
                System.out.println("  Ks: " + this.d.toString());
            }
            if (this.e != 0.0f) {
                System.out.println("  Ns: " + this.e);
            }
            if (this.f != 1.0f) {
                System.out.println("  d: " + this.f);
            }
            if (this.f363g != null) {
                System.out.println("  Texture file: " + this.f363g);
            }
        }
    }

    /* JADX INFO: compiled from: WavefrontLoader.java */
    public static class e {
        public Map<String, C0012d> a;
        public String b;

        private g c(String str) {
            StringTokenizer stringTokenizer = new StringTokenizer(str, " ");
            stringTokenizer.nextToken();
            try {
                return new g(Float.parseFloat(stringTokenizer.nextToken()), Float.parseFloat(stringTokenizer.nextToken()), Float.parseFloat(stringTokenizer.nextToken()));
            } catch (NumberFormatException e) {
                System.out.println(e.getMessage());
                return null;
            }
        }

        public C0012d a(String str) {
            return this.a.get(str);
        }

        public void b(BufferedReader bufferedReader) {
            Log.v("materials", "Reading material...");
            C0012d c0012d = null;
            while (true) {
                try {
                    String line = bufferedReader.readLine();
                    if (line == null) {
                        break;
                    }
                    String strTrim = line.trim();
                    if (strTrim.length() != 0) {
                        if (strTrim.startsWith("newmtl ")) {
                            if (c0012d != null) {
                                this.a.put(c0012d.c(), c0012d);
                            }
                            String strSubstring = strTrim.substring(7);
                            Log.d("Loader", "New material found: " + strSubstring);
                            c0012d = new C0012d(strSubstring);
                        } else if (strTrim.startsWith("map_Kd ")) {
                            String strSubstring2 = strTrim.substring(7);
                            Log.d("Loader", "New texture found: " + strSubstring2);
                            c0012d.j(strSubstring2);
                        } else if (strTrim.startsWith("Ka ")) {
                            c0012d.f(c(strTrim));
                        } else if (strTrim.startsWith("Kd ")) {
                            c0012d.g(c(strTrim));
                        } else if (strTrim.startsWith("Ks ")) {
                            c0012d.h(c(strTrim));
                        } else if (strTrim.startsWith("Ns ")) {
                            c0012d.i(Float.valueOf(strTrim.substring(3)).floatValue());
                        } else if (strTrim.charAt(0) == 'd') {
                            c0012d.e(Float.valueOf(strTrim.substring(2)).floatValue());
                        } else if (strTrim.startsWith("Tr ")) {
                            c0012d.e(1.0f - Float.valueOf(strTrim.substring(3)).floatValue());
                        } else if (!strTrim.startsWith("illum ") && strTrim.charAt(0) != '#') {
                            System.out.println("Ignoring MTL line: " + strTrim);
                        }
                    }
                } catch (Exception e) {
                    Log.e("materials", e.getMessage(), e);
                    return;
                }
            }
            if (c0012d != null) {
                this.a.put(c0012d.c(), c0012d);
            }
        }

        public void d() {
            Map<String, C0012d> map = this.a;
            if (map == null || map.isEmpty()) {
                Log.i("WavefrontLoader", "No materials available");
                return;
            }
            Log.i("WavefrontLoader", "No. of materials: " + this.a.size());
            Iterator<C0012d> it = this.a.values().iterator();
            while (it.hasNext()) {
                it.next().k();
            }
        }

        private e(String str) {
            this.a = new LinkedHashMap();
            this.b = str;
        }
    }

    /* JADX INFO: compiled from: WavefrontLoader.java */
    public static class f {

        /* JADX INFO: renamed from: g, reason: collision with root package name */
        private DecimalFormat f365g = new DecimalFormat("0.##");
        public float a = 0.0f;
        public float b = 0.0f;
        public float c = 0.0f;
        public float d = 0.0f;
        public float e = 0.0f;
        public float f = 0.0f;

        public g a() {
            return new g((this.b + this.a) / 2.0f, (this.c + this.d) / 2.0f, (this.f + this.e) / 2.0f);
        }

        public float b() {
            return this.f - this.e;
        }

        public float c() {
            return this.c - this.d;
        }

        public float d() {
            float fC = c();
            float fB = b();
            float fE = e();
            if (fC <= fE) {
                fC = fE;
            }
            return fB > fC ? fB : fC;
        }

        public float e() {
            return this.b - this.a;
        }

        public void f() {
            g gVarA = a();
            System.out.println("x Coords: " + this.f365g.format(this.a) + " to " + this.f365g.format(this.b));
            System.out.println("  Mid: " + this.f365g.format((double) gVarA.a()) + "; Width: " + this.f365g.format(e()));
            System.out.println("y Coords: " + this.f365g.format((double) this.d) + " to " + this.f365g.format(this.c));
            System.out.println("  Mid: " + this.f365g.format((double) gVarA.b()) + "; Height: " + this.f365g.format(c()));
            System.out.println("z Coords: " + this.f365g.format((double) this.f) + " to " + this.f365g.format(this.e));
            System.out.println("  Mid: " + this.f365g.format((double) gVarA.c()) + "; Depth: " + this.f365g.format(b()));
        }

        public void g(float f, float f2, float f3) {
            this.b = f;
            this.a = f;
            this.c = f2;
            this.d = f2;
            this.f = f3;
            this.e = f3;
        }

        public void h(float f, float f2, float f3) {
            if (f > this.b) {
                this.b = f;
            }
            if (f < this.a) {
                this.a = f;
            }
            if (f2 > this.c) {
                this.c = f2;
            }
            if (f2 < this.d) {
                this.d = f2;
            }
            if (f3 > this.f) {
                this.f = f3;
            }
            if (f3 < this.e) {
                this.e = f3;
            }
        }
    }

    /* JADX INFO: compiled from: WavefrontLoader.java */
    public static class g {
        private float a;
        private float b;
        private float c;

        public g(float f, float f2, float f3) {
            this.a = f;
            this.b = f2;
            this.c = f3;
        }

        public float a() {
            return this.a;
        }

        public float b() {
            return this.b;
        }

        public float c() {
            return this.c;
        }

        public String toString() {
            return "( " + this.a + ", " + this.b + ", " + this.c + " )";
        }
    }

    public d(String str) {
    }

    private boolean a(String str, boolean z) {
        if (z) {
            this.a = e(str);
            System.out.println("Using 3D tex coords: " + this.a);
        }
        g gVarP = p(str);
        if (gVarP == null) {
            return false;
        }
        this.b.add(gVarP);
        return true;
    }

    private boolean b(FloatBuffer floatBuffer, int i2, String str, boolean z, f fVar) throws Throwable {
        float f2;
        float f3;
        String[] strArrSplit;
        float f4;
        float f5 = 0.0f;
        try {
            strArrSplit = str.contains("  ") ? str.split(" +") : str.split(" ");
            f4 = Float.parseFloat(strArrSplit[1]);
            try {
                f3 = Float.parseFloat(strArrSplit[2]);
            } catch (NumberFormatException e2) {
                e = e2;
                f2 = 0.0f;
                f3 = 0.0f;
            } catch (Throwable th) {
                th = th;
                f2 = 0.0f;
                f3 = 0.0f;
            }
        } catch (NumberFormatException e3) {
            e = e3;
            f2 = 0.0f;
            f3 = 0.0f;
        } catch (Throwable th2) {
            th = th2;
            f2 = 0.0f;
            f3 = 0.0f;
        }
        try {
            float f6 = Float.parseFloat(strArrSplit[3]);
            if (fVar != null) {
                if (z) {
                    this.f.g(f4, f3, f6);
                } else {
                    this.f.h(f4, f3, f6);
                }
            }
            floatBuffer.put(i2, f4).put(i2 + 1, f3).put(i2 + 2, f6);
            return true;
        } catch (NumberFormatException e4) {
            e = e4;
            f2 = 0.0f;
            f5 = f4;
            try {
                Log.e("WavefrontLoader", e.getMessage());
                floatBuffer.put(i2, f5).put(i2 + 1, f3).put(i2 + 2, f2);
                return false;
            } catch (Throwable th3) {
                th = th3;
                floatBuffer.put(i2, f5).put(i2 + 1, f3).put(i2 + 2, f2);
                throw th;
            }
        } catch (Throwable th4) {
            th = th4;
            f2 = 0.0f;
            f5 = f4;
            floatBuffer.put(i2, f5).put(i2 + 1, f3).put(i2 + 2, f2);
            throw th;
        }
    }

    private boolean e(String str) {
        return str.split("\\s+").length == 4;
    }

    private static ByteBuffer f(int i2) {
        ByteBuffer byteBufferAllocateDirect = ByteBuffer.allocateDirect(i2);
        byteBufferAllocateDirect.order(ByteOrder.nativeOrder());
        return byteBufferAllocateDirect;
    }

    private void o(BufferedReader bufferedReader) {
        int i2;
        boolean z = true;
        boolean z2 = true;
        boolean z3 = true;
        int i3 = 0;
        int i4 = 0;
        int i5 = 0;
        int i6 = 0;
        while (true) {
            try {
                String line = bufferedReader.readLine();
                if (line == null) {
                    break;
                }
                int i7 = i3 + 1;
                String strTrim = line.trim();
                if (strTrim.length() <= 0) {
                    i2 = i7;
                } else if (strTrim.startsWith("v ")) {
                    int i8 = i4 + 1;
                    z = b(this.n, i4 * 3, strTrim, z2, this.f) && z;
                    i3 = i7;
                    if (z2) {
                        z2 = false;
                    }
                    i4 = i8;
                } else {
                    i2 = i7;
                    if (strTrim.startsWith("vt")) {
                        z = a(strTrim, z3) && z;
                        if (z3) {
                            i3 = i2;
                            z3 = false;
                        }
                    } else if (strTrim.startsWith("vn")) {
                        int i9 = i5 + 1;
                        z = b(this.o, i5 * 3, strTrim, z2, null) && z;
                        i3 = i2;
                        i5 = i9;
                    } else if (strTrim.startsWith("f ")) {
                        z = this.c.a(strTrim) && z;
                        i6++;
                    } else if (!strTrim.startsWith("mtllib ")) {
                        if (strTrim.startsWith("usemtl ")) {
                            this.d.a(i6, strTrim.substring(7));
                        } else if (strTrim.charAt(0) != 'g' && strTrim.charAt(0) != 's' && strTrim.charAt(0) != '#' && strTrim.charAt(0) != 'o') {
                            System.out.println("Ignoring line " + i2 + " : " + strTrim);
                        }
                    }
                }
                i3 = i2;
            } catch (IOException e2) {
                Log.e("WavefrontLoader", e2.getMessage(), e2);
                throw new RuntimeException(e2);
            }
        }
        if (z) {
            return;
        }
        Log.e("WavefrontLoader", "Error loading model");
    }

    private g p(String str) {
        StringTokenizer stringTokenizer = new StringTokenizer(str, " ");
        stringTokenizer.nextToken();
        try {
            return new g(Float.parseFloat(stringTokenizer.nextToken()), Float.parseFloat(stringTokenizer.nextToken()), this.a ? Float.parseFloat(stringTokenizer.nextToken()) : -5.0f);
        } catch (NumberFormatException e2) {
            System.out.println(e2.getMessage());
            return null;
        }
    }

    public void c() {
        this.n = f(this.f352g * 3 * 4).asFloatBuffer();
        int i2 = this.f354i;
        if (i2 > 0) {
            this.o = f(i2 * 3 * 4).asFloatBuffer();
        }
        f(this.f353h * 3 * 4).asFloatBuffer();
        int i3 = this.f355j;
        if (i3 > 0) {
            this.c = new c(this.f355j, f(i3 * 3 * 4).asIntBuffer(), this.n, this.o, this.b);
        }
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:76:0x0192 A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public void d(java.io.InputStream r10) throws java.lang.Throwable {
        /*
            Method dump skipped, instruction units count: 415
            To view this dump add '--comments-level debug' option
        */
        throw new UnsupportedOperationException("Method not decompiled: com.seewo.factoryaging.opengl3d.i.d.d(java.io.InputStream):void");
    }

    public f g() {
        return this.f;
    }

    public b h() {
        return this.d;
    }

    public c i() {
        return this.c;
    }

    public e j() {
        return this.e;
    }

    public FloatBuffer k() {
        return this.o;
    }

    public ArrayList<g> l() {
        return this.b;
    }

    public FloatBuffer m() {
        return this.n;
    }

    public void n(InputStream inputStream) throws Throwable {
        BufferedReader bufferedReader = null;
        try {
            BufferedReader bufferedReader2 = new BufferedReader(new InputStreamReader(inputStream));
            try {
                o(bufferedReader2);
                try {
                    bufferedReader2.close();
                } catch (IOException e2) {
                    e2.printStackTrace();
                }
            } catch (Throwable th) {
                th = th;
                bufferedReader = bufferedReader2;
                if (bufferedReader != null) {
                    try {
                        bufferedReader.close();
                    } catch (IOException e3) {
                        e3.printStackTrace();
                    }
                }
                throw th;
            }
        } catch (Throwable th2) {
            th = th2;
        }
    }

    public void q() {
        Log.i("WavefrontLoader", "No. of vertices: " + (this.n.capacity() / 3));
        Log.i("WavefrontLoader", "No. of normal coords: " + this.f354i);
        Log.i("WavefrontLoader", "No. of tex coords: " + this.f353h);
        Log.i("WavefrontLoader", "No. of faces: " + this.f355j);
        this.f.f();
        e eVar = this.e;
        if (eVar != null) {
            eVar.d();
        }
        this.d.d();
    }

    /* JADX INFO: compiled from: WavefrontLoader.java */
    public static class c {
        public final int a;
        public IntBuffer b;
        public ArrayList<int[]> c;
        public ArrayList<int[]> d;
        private FloatBuffer e;
        private FloatBuffer f;

        /* JADX INFO: renamed from: g, reason: collision with root package name */
        private ArrayList<g> f359g;

        /* JADX INFO: renamed from: h, reason: collision with root package name */
        private int f360h;

        /* JADX INFO: renamed from: i, reason: collision with root package name */
        private int f361i;

        /* JADX INFO: renamed from: j, reason: collision with root package name */
        private int f362j;

        public c(int i2) {
            this.f361i = 0;
            this.a = i2;
            this.f360h = i2;
        }

        public boolean a(String str) {
            int iCapacity;
            try {
                String strSubstring = str.substring(2);
                String[] strArrSplit = strSubstring.contains("  ") ? strSubstring.split(" +") : strSubstring.split(" ");
                int length = strArrSplit.length;
                int i2 = 0;
                int i3 = 0;
                int[] iArr = null;
                int[] iArr2 = null;
                while (i2 < length) {
                    if (i3 > 2) {
                        this.f360h++;
                        this.f362j += 3;
                        if (iArr != null) {
                            this.c.add(iArr);
                        }
                        if (iArr2 != null) {
                            this.d.add(iArr2);
                        }
                        i2 -= 2;
                        i3 = 0;
                        iArr = null;
                        iArr2 = null;
                    }
                    String[] strArrSplit2 = (i3 == 0 ? strArrSplit[0] : strArrSplit[i2]).split("/");
                    int length2 = strArrSplit2.length;
                    int i4 = Integer.parseInt(strArrSplit2[0]);
                    if (length2 > 1) {
                        if (iArr == null) {
                            iArr = new int[3];
                        }
                        try {
                            iArr[i3] = Integer.parseInt(strArrSplit2[1]);
                        } catch (NumberFormatException unused) {
                            iArr[i3] = 0;
                        }
                    }
                    if (length2 > 2) {
                        if (iArr2 == null) {
                            iArr2 = new int[3];
                        }
                        try {
                            iArr2[i3] = Integer.parseInt(strArrSplit2[2]);
                        } catch (NumberFormatException unused2) {
                            iArr2[i3] = 0;
                        }
                    }
                    if (i4 < 0) {
                        iCapacity = (this.e.capacity() / 3) + i4;
                        if (iArr != null) {
                            iArr[i3] = this.f359g.size() + iArr[i3];
                        }
                        if (iArr2 != null) {
                            iArr2[i3] = (this.f.capacity() / 3) + iArr2[i3];
                        }
                    } else {
                        int i5 = i4 - 1;
                        if (iArr != null) {
                            iArr[i3] = iArr[i3] - 1;
                        }
                        if (iArr2 != null) {
                            iArr2[i3] = iArr2[i3] - 1;
                        }
                        iCapacity = i5;
                    }
                    IntBuffer intBuffer = this.b;
                    int i6 = this.f361i;
                    this.f361i = i6 + 1;
                    intBuffer.put(i6, iCapacity);
                    i2++;
                    i3++;
                }
                if (iArr != null) {
                    this.c.add(iArr);
                }
                if (iArr2 != null) {
                    this.d.add(iArr2);
                }
                this.f360h++;
                this.f362j += 3;
                return true;
            } catch (NumberFormatException e) {
                Log.e("WavefrontLoader", e.getMessage(), e);
                return false;
            }
        }

        public IntBuffer b() {
            return this.b;
        }

        public int c() {
            return this.a;
        }

        public int d() {
            return c() * 3;
        }

        public boolean e() {
            return this.f360h == this.a;
        }

        c(int i2, IntBuffer intBuffer, FloatBuffer floatBuffer, FloatBuffer floatBuffer2, ArrayList<g> arrayList) {
            this.f361i = 0;
            this.a = i2;
            this.e = floatBuffer;
            this.f = floatBuffer2;
            this.f359g = arrayList;
            this.b = intBuffer;
            this.c = new ArrayList<>();
            this.d = new ArrayList<>();
        }
    }
}
