package G0;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.SystemClock;
import android.util.Log;
import com.seewo.sdk.eventbus.EventBusRuntimeException;
import com.seewo.sdk.eventbus.OnEventOfOS;
import com.seewo.sdk.eventbus.ThreadMode;
import com.seewo.sdk.internal.model.LibMcuCallback;
import com.seewo.sdk.internal.model.SDKResponse;
import d.AbstractC0421a;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CopyOnWriteArrayList;

/* JADX INFO: loaded from: classes.dex */
public class e {

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    private static e f777f;

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    private final Map f778a = new HashMap();

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private Map f779b = new HashMap();

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private final Map f780c = new HashMap();

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private c f781d = new c(Looper.getMainLooper(), 10);

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    private c f782e = new c(G0.a.b().getLooper(), 10);

    class a extends LibMcuCallback.Stub {
        a() {
        }

        @Override // com.seewo.sdk.internal.model.LibMcuCallback
        public void onCall(String str) {
            SDKResponse sDKResponse = (SDKResponse) Q0.c.f(str, SDKResponse.class);
            try {
                e.this.g(AbstractC0421a.i(sDKResponse.getParamsJson(), Class.forName(sDKResponse.getResponseName())));
            } catch (ClassNotFoundException e2) {
                Log.e("OsEventBus", "Error: ", e2);
            }
        }
    }

    static /* synthetic */ class b {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        static final /* synthetic */ int[] f784a;

        static {
            int[] iArr = new int[ThreadMode.values().length];
            f784a = iArr;
            try {
                iArr[ThreadMode.MAIN.ordinal()] = 1;
            } catch (NoSuchFieldError unused) {
            }
            try {
                f784a[ThreadMode.BACKGROUND.ordinal()] = 2;
            } catch (NoSuchFieldError unused2) {
            }
        }
    }

    static final class c extends Handler {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        private final ConcurrentLinkedQueue f785a;

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        private final int f786b;

        /* JADX INFO: renamed from: c, reason: collision with root package name */
        private boolean f787c;

        c(Looper looper, int i2) {
            super(looper);
            this.f785a = new ConcurrentLinkedQueue();
            this.f786b = i2;
        }

        private d b() {
            d dVar = (d) this.f785a.poll();
            if (dVar == null) {
                synchronized (this) {
                    try {
                        dVar = (d) this.f785a.poll();
                        if (dVar == null) {
                            this.f787c = false;
                            return null;
                        }
                    } finally {
                    }
                }
            }
            return dVar;
        }

        void a(f fVar, Object obj) {
            d dVarA = d.a(fVar, obj);
            synchronized (this) {
                try {
                    this.f785a.add(dVarA);
                    if (!this.f787c) {
                        this.f787c = true;
                        if (!sendMessage(obtainMessage())) {
                            throw new EventBusRuntimeException("Could not send handler message");
                        }
                    }
                } finally {
                }
            }
        }

        @Override // android.os.Handler
        public void handleMessage(Message message) {
            try {
                long jUptimeMillis = SystemClock.uptimeMillis();
                do {
                    d dVarB = b();
                    if (dVarB == null) {
                        this.f787c = false;
                        return;
                    }
                    Object obj = dVarB.f789a;
                    f fVar = dVarB.f790b;
                    d.b(dVarB);
                    if (fVar != null && fVar.f798c) {
                        e.d().e(fVar, obj);
                    }
                } while (SystemClock.uptimeMillis() - jUptimeMillis < this.f786b);
                if (!sendMessage(obtainMessage())) {
                    throw new EventBusRuntimeException("Could not send handler message");
                }
                this.f787c = true;
            } catch (Throwable th) {
                this.f787c = false;
                throw th;
            }
        }
    }

    static class d {

        /* JADX INFO: renamed from: d, reason: collision with root package name */
        private static final List f788d = new ArrayList();

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        Object f789a;

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        f f790b;

        /* JADX INFO: renamed from: c, reason: collision with root package name */
        d f791c;

        private d(Object obj, f fVar) {
            this.f789a = obj;
            this.f790b = fVar;
        }

        static d a(f fVar, Object obj) {
            List list = f788d;
            synchronized (list) {
                try {
                    int size = list.size();
                    if (size <= 0) {
                        return new d(obj, fVar);
                    }
                    d dVar = (d) list.remove(size - 1);
                    dVar.f789a = obj;
                    dVar.f790b = fVar;
                    dVar.f791c = null;
                    return dVar;
                } catch (Throwable th) {
                    throw th;
                }
            }
        }

        static void b(d dVar) {
            dVar.f789a = null;
            dVar.f790b = null;
            dVar.f791c = null;
            List list = f788d;
            synchronized (list) {
                try {
                    if (list.size() < 10000) {
                        list.add(dVar);
                    }
                } catch (Throwable th) {
                    throw th;
                }
            }
        }
    }

    /* JADX INFO: renamed from: G0.e$e, reason: collision with other inner class name */
    class C0007e {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        Class f792a;

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        Method f793b;

        /* JADX INFO: renamed from: c, reason: collision with root package name */
        ThreadMode f794c;

        C0007e(Class cls, Method method, ThreadMode threadMode) {
            this.f792a = cls;
            this.f793b = method;
            this.f794c = threadMode;
        }

        public void a(Object obj, Object obj2) throws IllegalAccessException, InvocationTargetException {
            this.f793b.getClass();
            this.f793b.invoke(obj, obj2);
        }

        public boolean equals(Object obj) {
            return obj == this || ((obj instanceof C0007e) && toString().equals(obj.toString()));
        }

        public int hashCode() {
            this.f793b.getClass();
            return this.f793b.hashCode();
        }

        public String toString() {
            if (this.f793b != null) {
                return this.f793b.getDeclaringClass().getName() + '.' + this.f793b.getName() + '(' + this.f792a.getName() + ") in " + this.f794c.name();
            }
            return "SubscriberMethod{eventType=" + this.f792a.getName() + ", threadMode=" + this.f794c.name() + ", eventCallback=" + ((Object) null) + '}';
        }
    }

    final class f {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        final Object f796a;

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        final C0007e f797b;

        /* JADX INFO: renamed from: c, reason: collision with root package name */
        volatile boolean f798c = true;

        f(Object obj, C0007e c0007e) {
            this.f796a = obj;
            this.f797b = c0007e;
        }

        public boolean equals(Object obj) {
            if (!(obj instanceof f)) {
                return false;
            }
            f fVar = (f) obj;
            return this.f796a == fVar.f796a && this.f797b.equals(fVar.f797b);
        }

        public int hashCode() {
            return this.f796a.hashCode() + this.f797b.hashCode();
        }
    }

    private e() {
    }

    private void b(Method method) {
        Class<?>[] parameterTypes = method.getParameterTypes();
        if (parameterTypes.length != 1) {
            throw new EventBusRuntimeException("@Subscribe method " + method.getName() + "must have exactly 1 parameter but has " + parameterTypes.length);
        }
        int modifiers = method.getModifiers();
        if ((modifiers & 1) == 0 || (modifiers & 1096) > 0) {
            throw new EventBusRuntimeException(method.getName() + " is a illegal @Subscribe method: must be public, non-static, and non-abstract");
        }
    }

    private void c(Object obj, List list) {
        Iterator it = list.iterator();
        while (it.hasNext()) {
            Class cls = (Class) it.next();
            LibMcuCallback.Stub stub = (LibMcuCallback.Stub) this.f779b.remove(cls);
            if (stub != null) {
                G0.d.o().B(cls, stub);
            }
            m(obj, cls);
        }
        this.f780c.remove(obj);
    }

    public static e d() {
        if (f777f == null) {
            synchronized (e.class) {
                try {
                    if (f777f == null) {
                        f777f = new e();
                    }
                } finally {
                }
            }
        }
        return f777f;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public synchronized void g(Object obj) {
        if (!h(obj)) {
            Log.e("OsEventBus", "No subscribers registered for event " + obj.getClass());
        }
    }

    private boolean h(Object obj) {
        CopyOnWriteArrayList copyOnWriteArrayList;
        Class<?> cls = obj.getClass();
        synchronized (this) {
            copyOnWriteArrayList = (CopyOnWriteArrayList) this.f778a.get(cls);
        }
        if (copyOnWriteArrayList == null || copyOnWriteArrayList.isEmpty()) {
            return false;
        }
        Iterator it = copyOnWriteArrayList.iterator();
        while (it.hasNext()) {
            i((f) it.next(), obj);
        }
        return true;
    }

    private void i(f fVar, Object obj) {
        ThreadMode threadMode = fVar.f797b.f794c;
        int i2 = b.f784a[threadMode.ordinal()];
        if (i2 == 1) {
            this.f781d.a(fVar, obj);
        } else {
            if (i2 == 2) {
                this.f782e.a(fVar, obj);
                return;
            }
            throw new IllegalStateException("Unknown thread mode: " + threadMode);
        }
    }

    private void k(Object obj, C0007e c0007e) {
        Class cls = c0007e.f792a;
        f fVar = new f(obj, c0007e);
        CopyOnWriteArrayList copyOnWriteArrayList = (CopyOnWriteArrayList) this.f778a.get(cls);
        if (copyOnWriteArrayList == null) {
            copyOnWriteArrayList = new CopyOnWriteArrayList();
            this.f778a.put(cls, copyOnWriteArrayList);
        } else if (copyOnWriteArrayList.contains(fVar)) {
            throw new EventBusRuntimeException("Subscriber " + obj.getClass() + " already registered to event " + cls);
        }
        List arrayList = (List) this.f780c.get(obj);
        if (arrayList == null) {
            arrayList = new ArrayList();
            this.f780c.put(obj, arrayList);
        }
        arrayList.add(cls);
        copyOnWriteArrayList.add(fVar);
    }

    private void m(Object obj, Class cls) {
        List<f> list = (List) this.f778a.get(cls);
        if (list != null) {
            for (f fVar : list) {
                if (fVar.f796a == obj) {
                    fVar.f798c = false;
                    list.remove(fVar);
                }
            }
        }
    }

    void e(f fVar, Object obj) {
        try {
            fVar.f797b.a(fVar.f796a, obj);
        } catch (IllegalAccessException e2) {
            throw new IllegalStateException("Unexpected exception", e2);
        } catch (InvocationTargetException e3) {
            throw new EventBusRuntimeException(e3);
        }
    }

    public synchronized boolean f(Object obj) {
        return this.f780c.containsKey(obj);
    }

    public synchronized void j(Object obj) {
        if (f(obj)) {
            Q0.e.b("OsEventBus", obj.getClass().getSimpleName() + " has been registered. It can not be registered again");
            return;
        }
        for (Method method : obj.getClass().getDeclaredMethods()) {
            OnEventOfOS onEventOfOS = (OnEventOfOS) method.getAnnotation(OnEventOfOS.class);
            if (onEventOfOS != null) {
                b(method);
                Class<?> cls = method.getParameterTypes()[0];
                k(obj, new C0007e(cls, method, onEventOfOS.threadMode()));
                a aVar = new a();
                this.f779b.put(cls, aVar);
                G0.d.o().x(cls, aVar);
            }
        }
        if (!f(obj)) {
            Q0.e.b("OsEventBus", obj.getClass().getSimpleName() + " has no OnEventOfOS method(s) to been registered! Register failed!");
        }
    }

    public synchronized void l(Object obj) {
        if (!f(obj)) {
            Q0.e.b("OsEventBus", obj.getClass().getSimpleName() + " has not been registered. It can not be unregistered");
            return;
        }
        List list = (List) this.f780c.get(obj);
        if (list != null) {
            c(obj, list);
        } else {
            Log.w("OsEventBus", "Subscriber to unregister was not registered before: " + obj.getClass());
        }
    }
}
