package ai.advance.liveness.lib;

import ai.advance.liveness.lib.http.entity.ResultEntity;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.hardware.Camera;
import android.os.Handler;
import android.os.Looper;
import com.facebook.devicerequests.internal.DeviceRequestsHelper;
import java.io.File;
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.TimeUnit;
import org.json.JSONArray;
import org.json.JSONObject;
import ඓ.K.K.K.C1293;
import K.K.K.ʬ.C2369;
import K.K.㸾.K.C2383;
import K.K.㸾.K.C2385;
import K.K.㸾.K.C2386;
import K.K.㸾.K.C2387;
import K.K.㸾.K.C2388;
import K.K.㸾.K.C2390;
import K.K.㸾.K.C2394;
import K.K.㸾.K.C2395;

public final class Detector {

    /* renamed from: ʬ  reason: contains not printable characters */
    public AbstractC0012 f49;

    /* renamed from: Ԕ  reason: contains not printable characters */
    public long f50;

    /* renamed from: ؿ  reason: contains not printable characters */
    public ResultEntity f51;

    /* renamed from: ඓ  reason: contains not printable characters */
    public long f52;

    /* renamed from: ᕣ  reason: contains not printable characters */
    public C2386 f53;

    /* renamed from: ᨨ  reason: contains not printable characters */
    public Map<String, C2385> f54;

    /* renamed from: ῲ  reason: contains not printable characters */
    public DetectionType f55;

    /* renamed from: ⁱ  reason: contains not printable characters */
    public C0013 f56;

    /* renamed from: K  reason: contains not printable characters */
    public long f57;

    /* renamed from: 㑃  reason: contains not printable characters */
    public int f58 = 90;

    /* renamed from: 㗗  reason: contains not printable characters */
    public boolean f59;

    /* renamed from: 㳳  reason: contains not printable characters */
    public AbstractC0023 f60;

    /* renamed from: 㴹  reason: contains not printable characters */
    public BlockingQueue<C2385> f61;

    /* renamed from: 㸾  reason: contains not printable characters */
    public long f62;

    /* renamed from: 㿎  reason: contains not printable characters */
    public Handler f63;

    /* renamed from: 䁗  reason: contains not printable characters */
    public Context f64;

    public enum ActionStatus {
        NOFACE,
        FACECHECKSIZE,
        FACESIZEREADY,
        FACECENTERREADY,
        FACEFRONTALREADY,
        FACECAPTUREREADY,
        FACEMOTIONREADY,
        FACECHECKOCCLUSION,
        FACENODEFINE;

        public static ActionStatus valueOf(int i) {
            switch (i) {
                case 1:
                    return NOFACE;
                case 2:
                    return FACECHECKSIZE;
                case 3:
                    return FACESIZEREADY;
                case 4:
                    return FACECENTERREADY;
                case 5:
                    return FACEFRONTALREADY;
                case 6:
                    return FACECAPTUREREADY;
                case 7:
                    return FACEMOTIONREADY;
                case 8:
                    return FACECHECKOCCLUSION;
                default:
                    return FACENODEFINE;
            }
        }

        public boolean isFaceNotReady() {
            return this != FACEMOTIONREADY;
        }
    }

    public enum DetectionFailedType {
        TIMEOUT,
        WEAKLIGHT,
        STRONGLIGHT,
        FACEMISSING,
        MULTIPLEFACE,
        MUCHMOTION,
        UNAUTHORIZED,
        UNSUPPORT_DEVICE
    }

    public enum DetectionType {
        NONE(0),
        BLINK(1),
        MOUTH(2),
        POS_YAW(3),
        DONE(6),
        AIMLESS(5);
        
        public int mInterValue;

        /* access modifiers changed from: public */
        DetectionType(int i) {
            this.mInterValue = i;
        }
    }

    public enum WarnCode {
        FACEMISSING,
        FACELARGE,
        FACESMALL,
        FACENOTCENTER,
        FACENOTFRONTAL,
        FACENOTSTILL,
        WARN_MULTIPLEFACES,
        WARN_EYE_OCCLUSION,
        WARN_MOUTH_OCCLUSION,
        FACECAPTURE,
        FACEINACTION,
        OK_ACTIONDONE,
        ERROR_MULTIPLEFACES,
        ERROR_FACEMISSING,
        ERROR_MUCHMOTION,
        OK_COUNTING,
        OK_DEFAULT,
        WARN_MOTION,
        WARN_LARGE_YAW;

        public static WarnCode valueOf(int i) {
            switch (i) {
                case 1:
                    return FACEMISSING;
                case 2:
                    return FACELARGE;
                case 3:
                    return FACESMALL;
                case 4:
                    return FACENOTCENTER;
                case 5:
                    return FACENOTFRONTAL;
                case 6:
                    return FACENOTSTILL;
                case 7:
                    return WARN_MULTIPLEFACES;
                case 8:
                    return WARN_EYE_OCCLUSION;
                case 9:
                    return WARN_MOUTH_OCCLUSION;
                case 10:
                    return FACECAPTURE;
                case 11:
                    return FACEINACTION;
                case 12:
                    return OK_ACTIONDONE;
                case 13:
                    return ERROR_MULTIPLEFACES;
                case 14:
                    return ERROR_FACEMISSING;
                case 15:
                    return ERROR_MUCHMOTION;
                case 16:
                    return OK_COUNTING;
                case 17:
                    return WARN_MOTION;
                case 18:
                    return WARN_LARGE_YAW;
                default:
                    return OK_DEFAULT;
            }
        }
    }

    /* renamed from: ai.advance.liveness.lib.Detector$ʬ  reason: contains not printable characters */
    public interface AbstractC0012 {
        /* renamed from: ῲ  reason: contains not printable characters */
        void m70(long j);

        /* renamed from: ῲ  reason: contains not printable characters */
        void m71(C2385 v);

        /* renamed from: K  reason: contains not printable characters */
        DetectionType m72(C2385 v);

        /* renamed from: K  reason: contains not printable characters */
        void m73();

        /* renamed from: K  reason: contains not printable characters */
        void m74(DetectionFailedType detectionFailedType);
    }

    /* renamed from: ai.advance.liveness.lib.Detector$ඓ  reason: contains not printable characters */
    public class C0013 extends Thread {

        /* renamed from: ʬ  reason: contains not printable characters */
        public volatile boolean f69 = true;

        /* renamed from: ඓ  reason: contains not printable characters */
        public volatile boolean f70 = false;

        /* renamed from: ῲ  reason: contains not printable characters */
        public float f71 = 0.0f;

        /* renamed from: 㸾  reason: contains not printable characters */
        public ActionStatus f73 = ActionStatus.FACENODEFINE;

        /* renamed from: ai.advance.liveness.lib.Detector$ඓ$ʬ  reason: contains not printable characters */
        public class RunnableC0014 implements Runnable {
            public RunnableC0014() {
            }

            public void run() {
                Detector detector = Detector.this;
                AbstractC0012 r1 = detector.f49;
                if (r1 != null) {
                    r1.m70((detector.f62 + detector.f52) - System.currentTimeMillis());
                }
            }
        }

        /* renamed from: ai.advance.liveness.lib.Detector$ඓ$ඓ  reason: contains not printable characters */
        public class RunnableC0015 implements Runnable {

            /* renamed from: ῲ  reason: contains not printable characters */
            public final /* synthetic */ C2385 f76;

            public RunnableC0015(C2385 r2) {
                this.f76 = r2;
            }

            public void run() {
                AbstractC0012 r0 = Detector.this.f49;
                if (r0 != null) {
                    r0.m71(this.f76);
                }
            }
        }

        /* renamed from: ai.advance.liveness.lib.Detector$ඓ$ῲ  reason: contains not printable characters */
        public class RunnableC0016 implements Runnable {

            /* renamed from: ῲ  reason: contains not printable characters */
            public final /* synthetic */ C2385 f78;

            public RunnableC0016(C2385 r2) {
                this.f78 = r2;
            }

            public void run() {
                AbstractC0012 r0 = Detector.this.f49;
                if (r0 != null) {
                    r0.m71(this.f78);
                }
            }
        }

        /* renamed from: ai.advance.liveness.lib.Detector$ඓ$K  reason: contains not printable characters */
        public class RunnableC0017 implements Runnable {

            /* renamed from: ῲ  reason: contains not printable characters */
            public final /* synthetic */ C2385 f80;

            public RunnableC0017(C2385 r2) {
                this.f80 = r2;
            }

            public void run() {
                AbstractC0012 r0 = Detector.this.f49;
                if (r0 != null) {
                    r0.m71(this.f80);
                    Detector detector = Detector.this;
                    detector.f54.put(detector.f55.name(), this.f80);
                    Detector detector2 = Detector.this;
                    detector2.f55 = detector2.f49.m72(this.f80);
                    C0013 r02 = C0013.this;
                    r02.f70 = false;
                    C2386 r03 = Detector.this.m63();
                    DetectionType detectionType = Detector.this.f55;
                    r03.m6402();
                    r03.f8268 = 0;
                    if (r03.f8261 == null) {
                        r03.f8261 = new JSONArray();
                    }
                    r03.f8261.put(r03.m6401());
                    r03.f8266 = detectionType;
                    List<JSONObject> list = r03.f8270;
                    if (list != null) {
                        list.clear();
                    }
                    C2394.m6436();
                    if (detectionType == DetectionType.DONE) {
                        r03.m6183("sdk_detection_success", true);
                    }
                    Detector detector3 = Detector.this;
                    DetectionType detectionType2 = detector3.f55;
                    if (detectionType2 == DetectionType.DONE) {
                        detector3.m63().f8262 = true;
                        C0013.this.f69 = false;
                        return;
                    }
                    detector3.m65(detectionType2);
                }
            }
        }

        /* renamed from: ai.advance.liveness.lib.Detector$ඓ$㴹  reason: contains not printable characters */
        public class RunnableC0018 implements Runnable {

            /* renamed from: ῲ  reason: contains not printable characters */
            public final /* synthetic */ C2385 f82;

            public RunnableC0018(C2385 r2) {
                this.f82 = r2;
            }

            public void run() {
                AbstractC0012 r0 = Detector.this.f49;
                if (r0 != null) {
                    r0.m71(this.f82);
                }
            }
        }

        /* renamed from: ai.advance.liveness.lib.Detector$ඓ$㸾  reason: contains not printable characters */
        public class RunnableC0019 implements Runnable {

            /* renamed from: ῲ  reason: contains not printable characters */
            public final /* synthetic */ C2385 f84;

            public RunnableC0019(C2385 r2) {
                this.f84 = r2;
            }

            public void run() {
                AbstractC0012 r0 = Detector.this.f49;
                if (r0 != null) {
                    r0.m71(this.f84);
                }
            }
        }

        public C0013() {
            super("liveness_worker");
        }

        public void run() {
            Runnable runnable;
            Handler handler;
            Detector detector;
            DetectionFailedType detectionFailedType;
            Detector.this.m69();
            System.currentTimeMillis();
            Detector.this.m63().m6396();
            while (this.f69) {
                try {
                    if (this.f70) {
                        Thread.sleep(10);
                    } else if (Detector.this.f55 != DetectionType.DONE) {
                        C2385 poll = Detector.this.f61.poll(300, TimeUnit.MILLISECONDS);
                        if (poll == null) {
                            continue;
                        } else if (poll.f8253 == Detector.this.f55) {
                            if (Math.abs(System.currentTimeMillis() - Detector.this.f52) < Detector.this.f62 || Detector.this.f55 == DetectionType.AIMLESS) {
                                long currentTimeMillis = System.currentTimeMillis();
                                long j = Detector.this.f57;
                                Bitmap r4 = poll.m6386(300, 80);
                                poll.f8250 = r4.getHeight();
                                poll.f8257 = r4.getWidth();
                                ByteBuffer allocate = ByteBuffer.allocate(r4.getByteCount());
                                r4.copyPixelsToBuffer(allocate);
                                byte[] array = allocate.array();
                                r4.recycle();
                                String r42 = C2387.m6412(j, array, poll.f8257, poll.f8250, Detector.this.f55.mInterValue);
                                int parseInt = Integer.parseInt(String.valueOf(System.currentTimeMillis() - currentTimeMillis));
                                JSONObject jSONObject = new JSONObject(r42);
                                poll.m6389(jSONObject);
                                if (this.f73 == ActionStatus.FACEMOTIONREADY) {
                                    Detector.this.f49.m73();
                                    Detector.this.m63().m6403();
                                }
                                this.f73 = poll.f8251;
                                Detector.this.m63().m6399(jSONObject, poll, this.f73, parseInt);
                                int ordinal = WarnCode.valueOf(jSONObject.optInt("code")).ordinal();
                                if (ordinal != 0) {
                                    if (!(ordinal == 17 || ordinal == 18)) {
                                        switch (ordinal) {
                                            case 7:
                                            case 10:
                                                break;
                                            case 8:
                                                this.f71 = 0.0f;
                                                Detector.this.f54.clear();
                                                Detector.this.m69();
                                                handler = Detector.this.f63;
                                                runnable = new RunnableC0018(poll);
                                                handler.post(runnable);
                                                break;
                                            case 9:
                                                float f = poll.f8256.f8278;
                                                C2385 r3 = Detector.this.f54.get("bestImage");
                                                if (f > this.f71) {
                                                    this.f71 = f;
                                                    Detector.this.f54.put("bestImage", poll);
                                                }
                                                if (r3 != null) {
                                                    Detector.this.f54.put("anotherCaptureImage", r3);
                                                }
                                                Detector.this.m69();
                                                handler = Detector.this.f63;
                                                runnable = new RunnableC0015(poll);
                                                handler.post(runnable);
                                                break;
                                            case 11:
                                                this.f70 = true;
                                                handler = Detector.this.f63;
                                                runnable = new RunnableC0017(poll);
                                                handler.post(runnable);
                                                break;
                                            case 12:
                                                detector = Detector.this;
                                                detectionFailedType = DetectionFailedType.MULTIPLEFACE;
                                                detector.m64(detectionFailedType);
                                                this.f69 = false;
                                                break;
                                            case 13:
                                                detector = Detector.this;
                                                detectionFailedType = DetectionFailedType.FACEMISSING;
                                                detector.m64(detectionFailedType);
                                                this.f69 = false;
                                                break;
                                            case 14:
                                                detector = Detector.this;
                                                detectionFailedType = DetectionFailedType.MUCHMOTION;
                                                detector.m64(detectionFailedType);
                                                this.f69 = false;
                                                break;
                                            default:
                                                Detector.this.m69();
                                                handler = Detector.this.f63;
                                                runnable = new RunnableC0018(poll);
                                                handler.post(runnable);
                                                break;
                                        }
                                    }
                                } else if (this.f73.isFaceNotReady()) {
                                    Detector.this.m69();
                                    handler = Detector.this.f63;
                                    runnable = new RunnableC0016(poll);
                                    handler.post(runnable);
                                }
                                Detector.this.f63.post(new RunnableC0014());
                                handler = Detector.this.f63;
                                runnable = new RunnableC0019(poll);
                                handler.post(runnable);
                            } else {
                                Detector.this.m64(DetectionFailedType.TIMEOUT);
                                this.f69 = false;
                                return;
                            }
                        }
                    } else {
                        return;
                    }
                } catch (Exception unused) {
                }
            }
        }
    }

    /* renamed from: ai.advance.liveness.lib.Detector$ῲ  reason: contains not printable characters */
    public class RunnableC0020 implements Runnable {

        /* renamed from: ῲ  reason: contains not printable characters */
        public final /* synthetic */ DetectionFailedType f86;

        public RunnableC0020(DetectionFailedType detectionFailedType) {
            this.f86 = detectionFailedType;
        }

        public void run() {
            AbstractC0012 r0 = Detector.this.f49;
            if (r0 != null) {
                r0.m74(this.f86);
            }
        }
    }

    /* renamed from: ai.advance.liveness.lib.Detector$K  reason: contains not printable characters */
    public class C0021 extends Thread {

        /* renamed from: ʬ  reason: contains not printable characters */
        public final /* synthetic */ DetectionType f87;

        /* renamed from: ῲ  reason: contains not printable characters */
        public final /* synthetic */ AbstractC0023 f88;

        public C0021(AbstractC0023 r2, DetectionType detectionType) {
            this.f88 = r2;
            this.f87 = detectionType;
        }

        public void run() {
            boolean z;
            String str;
            String str2;
            Detector detector;
            Detector detector2 = Detector.this;
            detector2.f60 = this.f88;
            Context context = detector2.f64;
            try {
                if (!"10".equals(C2369.m6354(context, "liveness_mv"))) {
                    File[] listFiles = new File(C2369.m6313(context)).listFiles();
                    if (listFiles != null) {
                        for (File file : listFiles) {
                            file.delete();
                        }
                    }
                    String[] list = context.getAssets().list(DeviceRequestsHelper.DEVICE_INFO_MODEL);
                    if (list != null) {
                        for (String str3 : list) {
                            C2369.m6335(str3, context);
                        }
                    }
                    C2369.m6327(context, "liveness_mv", "10");
                }
                z = true;
            } catch (Exception e) {
                C2369.m6274(e.getMessage());
                z = false;
            }
            if (z) {
                detector = Detector.this;
                if (detector.f56 != null) {
                    str2 = e.ALREADY_INIT.toString();
                    str = "already init";
                } else {
                    detector.f62 = C2388.f8277;
                    detector.f54 = new HashMap();
                    Detector.this.f61 = new LinkedBlockingDeque(2);
                    C2386 r0 = Detector.this.m63();
                    DetectionType detectionType = this.f87;
                    r0.f8266 = detectionType;
                    Detector.this.m65(detectionType);
                    Detector detector3 = Detector.this;
                    C2386 r1 = detector3.m63();
                    r1.f8171 = true;
                    r1.f8169 = System.currentTimeMillis();
                    detector3.m63().m6181(detector3.f58);
                    C2369.m6355("auth checking");
                    AbstractC0023 r12 = detector3.f60;
                    if (r12 != null) {
                        r12.m75();
                    }
                    new C2395(new C2390(detector3)).start();
                    return;
                }
            } else {
                detector = Detector.this;
                str2 = e.MODEL_ERROR.toString();
                str = "model error";
            }
            detector.m67(false, str2, str);
        }
    }

    /* renamed from: ai.advance.liveness.lib.Detector$㴹  reason: contains not printable characters */
    public interface AbstractC0022 {
    }

    /* renamed from: ai.advance.liveness.lib.Detector$㸾  reason: contains not printable characters */
    public interface AbstractC0023 {
        /* renamed from: ῲ  reason: contains not printable characters */
        void m75();

        /* renamed from: K  reason: contains not printable characters */
        void m76(boolean z, String str, String str2);
    }

    public Detector(Activity activity) {
        this.f64 = activity;
        this.f58 = C2369.m6295(C2388.f8274, activity);
    }

    /* renamed from: ʬ  reason: contains not printable characters */
    public final synchronized void m59() {
        if (this.f56 == null) {
            this.f56 = new C0013();
            this.f56.start();
        }
    }

    /* renamed from: ඓ  reason: contains not printable characters */
    public synchronized void m60() {
        if (!this.f59) {
            this.f59 = true;
            try {
                C2386 r0 = m63();
                C2383.m6377(r0.f8272 != 0 ? System.currentTimeMillis() - r0.f8272 : -1);
                r0.m6406();
                if (this.f56 != null) {
                    if (this.f56.f69) {
                        C2383.m6380(e.USER_GIVE_UP);
                        r0.m6405();
                    }
                    this.f56.f69 = false;
                    try {
                        this.f56.join();
                    } catch (InterruptedException unused) {
                    }
                    this.f56 = null;
                }
                if (this.f60 != null) {
                    this.f60 = null;
                }
                if (this.f57 != 0) {
                    C2387.m6418(this.f57);
                    this.f57 = 0;
                }
                this.f54.clear();
                this.f61 = null;
            } catch (Exception unused2) {
            }
            LService.m77(m63().m6394().toString());
            LivenessRiskService.m83();
        }
    }

    /* renamed from: ῲ  reason: contains not printable characters */
    public final void m61() {
        C2369.m6355("sdk auth success");
        this.f57 = C2387.m6411(this.f64);
        if (this.f57 == 0) {
            m67(false, e.MODEL_ERROR.toString(), "model error");
            return;
        }
        m67(true, "", "");
        long j = this.f50;
        if (j != 0) {
            try {
                Thread.sleep(j);
            } catch (Exception e) {
                C2369.m6274(e.getMessage());
            }
        }
        m59();
    }

    @Deprecated
    /* renamed from: ῲ  reason: contains not printable characters */
    public synchronized boolean m62(byte[] bArr, Camera.Size size) {
        if (this.f61 == null) {
            return false;
        }
        try {
            boolean offer = this.f61.offer(new C2385(bArr, this.f58, size.width, size.height, this.f55));
            m63().m6182(size);
            return offer;
        } catch (Exception unused) {
            return false;
        }
    }

    /* renamed from: K  reason: contains not printable characters */
    public C2386 m63() {
        if (this.f53 == null) {
            this.f53 = new C2386(this.f64);
        }
        return this.f53;
    }

    /* renamed from: K  reason: contains not printable characters */
    public final void m64(DetectionFailedType detectionFailedType) {
        StringBuilder r0 = C1293.m3416("liveness detection failed,reason:");
        r0.append(detectionFailedType.name());
        C2369.m6355(r0.toString());
        C2383.m6378(detectionFailedType);
        C2383.m6379(this.f55);
        m63().m6397(detectionFailedType);
        this.f63.post(new RunnableC0020(detectionFailedType));
    }

    /* renamed from: K  reason: contains not printable characters */
    public final void m65(DetectionType detectionType) {
        C2369.m6355("next action:" + detectionType);
        this.f55 = detectionType;
        this.f52 = System.currentTimeMillis();
        m63().f8265 = this.f52;
    }

    /* renamed from: K  reason: contains not printable characters */
    public synchronized void m66(DetectionType detectionType, AbstractC0023 r4) {
        this.f63 = new Handler(Looper.getMainLooper());
        C2394.m6436();
        new C0021(r4, detectionType).start();
    }

    /* renamed from: K  reason: contains not printable characters */
    public synchronized boolean m68(byte[] bArr, Camera.Size size) {
        return m62(bArr, size);
    }

    /* renamed from: 㸾  reason: contains not printable characters */
    public final void m69() {
        this.f52 = System.currentTimeMillis();
        m63().f8265 = this.f52;
    }

    /* renamed from: K  reason: contains not printable characters */
    public final void m67(boolean z, String str, String str2) {
        if (!z) {
            if ("NO_RESPONSE".equals(str)) {
                C2383.m6380(e.AUTH_BAD_NETWORK);
            } else {
                C2383.f8246 = "AUTH_" + str;
            }
            C2383.m6382(str2);
        }
        AbstractC0023 r0 = this.f60;
        if (r0 != null) {
            r0.m76(z, str, str2);
        }
    }
}
