package e.c.d;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.RemoteException;
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 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: compiled from: OsEventBus.java */
/* JADX INFO: loaded from: classes.dex */
public class d {

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    private static d f774f;
    private final Map<Class, CopyOnWriteArrayList<g>> a = new HashMap();
    private Map<Class, LibMcuCallback.Stub> b = new HashMap();

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private final Map<Object, List<Class<?>>> f775c = new HashMap();

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

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

    /* JADX INFO: compiled from: OsEventBus.java */
    class a extends LibMcuCallback.Stub {
        a() {
        }

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

    /* JADX INFO: compiled from: OsEventBus.java */
    static /* synthetic */ class b {
        static final /* synthetic */ int[] a;

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

    /* JADX INFO: compiled from: OsEventBus.java */
    static final class c extends Handler {
        private final ConcurrentLinkedQueue<e> a;
        private final int b;

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

        c(Looper looper, int i2) {
            super(looper);
            this.a = new ConcurrentLinkedQueue<>();
            this.b = i2;
        }

        private e b() {
            e eVarPoll = this.a.poll();
            if (eVarPoll == null) {
                synchronized (this) {
                    eVarPoll = this.a.poll();
                    if (eVarPoll == null) {
                        this.f778c = false;
                        return null;
                    }
                }
            }
            return eVarPoll;
        }

        void a(g gVar, Object obj) {
            e eVarA = e.a(gVar, obj);
            synchronized (this) {
                this.a.add(eVarA);
                if (!this.f778c) {
                    this.f778c = true;
                    if (!sendMessage(obtainMessage())) {
                        throw new EventBusRuntimeException("Could not send handler message");
                    }
                }
            }
        }

        @Override // android.os.Handler
        public void handleMessage(Message message) {
            try {
                long jUptimeMillis = SystemClock.uptimeMillis();
                do {
                    e eVarB = b();
                    if (eVarB == null) {
                        return;
                    }
                    Object obj = eVarB.a;
                    g gVar = eVarB.b;
                    e.b(eVarB);
                    if (gVar != null && gVar.f783c) {
                        d.d().e(gVar, obj);
                    }
                } while (SystemClock.uptimeMillis() - jUptimeMillis < this.b);
                if (!sendMessage(obtainMessage())) {
                    throw new EventBusRuntimeException("Could not send handler message");
                }
                this.f778c = true;
            } finally {
                this.f778c = false;
            }
        }
    }

    /* JADX INFO: renamed from: e.c.d.d$d, reason: collision with other inner class name */
    /* JADX INFO: compiled from: OsEventBus.java */
    public interface InterfaceC0032d<T> {
        void onEvent(T t);
    }

    /* JADX INFO: compiled from: OsEventBus.java */
    static class e {

        /* JADX INFO: renamed from: d, reason: collision with root package name */
        private static final List<e> f779d = new ArrayList();
        Object a;
        g b;

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

        private e(Object obj, g gVar) {
            this.a = obj;
            this.b = gVar;
        }

        static e a(g gVar, Object obj) {
            synchronized (f779d) {
                int size = f779d.size();
                if (size <= 0) {
                    return new e(obj, gVar);
                }
                e eVarRemove = f779d.remove(size - 1);
                eVarRemove.a = obj;
                eVarRemove.b = gVar;
                eVarRemove.f780c = null;
                return eVarRemove;
            }
        }

        static void b(e eVar) {
            eVar.a = null;
            eVar.b = null;
            eVar.f780c = null;
            synchronized (f779d) {
                if (f779d.size() < 10000) {
                    f779d.add(eVar);
                }
            }
        }
    }

    /* JADX INFO: compiled from: OsEventBus.java */
    class f {
        Class<?> a;
        Method b;

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

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

        f(d dVar, Class<?> cls, Method method, ThreadMode threadMode) {
            this.a = cls;
            this.b = method;
            this.f781c = threadMode;
        }

        public void a(Object obj, Object obj2) throws IllegalAccessException, InvocationTargetException {
            Method method = this.b;
            if (method != null) {
                method.invoke(obj, obj2);
            } else {
                this.f782d.onEvent(obj2);
            }
        }

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

        public int hashCode() {
            Method method = this.b;
            return method != null ? method.hashCode() : this.f782d.hashCode();
        }

        public String toString() {
            if (this.b != null) {
                return this.b.getDeclaringClass().getName() + '.' + this.b.getName() + '(' + this.a.getName() + ") in " + this.f781c.name();
            }
            return "SubscriberMethod{eventType=" + this.a.getName() + ", threadMode=" + this.f781c.name() + ", eventCallback=" + this.f782d + '}';
        }
    }

    /* JADX INFO: compiled from: OsEventBus.java */
    final class g {
        final Object a;
        final f b;

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

        g(d dVar, Object obj, f fVar) {
            this.a = obj;
            this.b = fVar;
        }

        public boolean equals(Object obj) {
            if (!(obj instanceof g)) {
                return false;
            }
            g gVar = (g) obj;
            return this.a == gVar.a && this.b.equals(gVar.b);
        }

        public int hashCode() {
            return this.a.hashCode() + this.b.hashCode();
        }
    }

    private d() {
    }

    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<Class<?>> list) {
        for (Class<?> cls : list) {
            LibMcuCallback.Stub stubRemove = this.b.remove(cls);
            if (stubRemove != null) {
                e.c.d.c.p().C(cls, stubRemove);
            }
            m(obj, cls);
        }
        this.f775c.remove(obj);
    }

    public static d d() {
        if (f774f == null) {
            synchronized (d.class) {
                if (f774f == null) {
                    f774f = new d();
                }
            }
        }
        return f774f;
    }

    /* 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<g> copyOnWriteArrayList;
        Class<?> cls = obj.getClass();
        synchronized (this) {
            copyOnWriteArrayList = this.a.get(cls);
        }
        if (copyOnWriteArrayList == null || copyOnWriteArrayList.isEmpty()) {
            return false;
        }
        Iterator<g> it = copyOnWriteArrayList.iterator();
        while (it.hasNext()) {
            i(it.next(), obj);
        }
        return true;
    }

    private void i(g gVar, Object obj) {
        ThreadMode threadMode = gVar.b.f781c;
        int i2 = b.a[threadMode.ordinal()];
        if (i2 == 1) {
            this.f776d.a(gVar, obj);
        } else {
            if (i2 == 2) {
                this.f777e.a(gVar, obj);
                return;
            }
            throw new IllegalStateException("Unknown thread mode: " + threadMode);
        }
    }

    private void k(Object obj, f fVar) {
        Class<?> cls = fVar.a;
        g gVar = new g(this, obj, fVar);
        CopyOnWriteArrayList<g> copyOnWriteArrayList = this.a.get(cls);
        if (copyOnWriteArrayList == null) {
            copyOnWriteArrayList = new CopyOnWriteArrayList<>();
            this.a.put(cls, copyOnWriteArrayList);
        } else if (copyOnWriteArrayList.contains(gVar)) {
            throw new EventBusRuntimeException("Subscriber " + obj.getClass() + " already registered to event " + cls);
        }
        List<Class<?>> arrayList = this.f775c.get(obj);
        if (arrayList == null) {
            arrayList = new ArrayList<>();
            this.f775c.put(obj, arrayList);
        }
        arrayList.add(cls);
        copyOnWriteArrayList.add(gVar);
    }

    private void m(Object obj, Class<?> cls) {
        CopyOnWriteArrayList<g> copyOnWriteArrayList = this.a.get(cls);
        if (copyOnWriteArrayList != null) {
            for (g gVar : copyOnWriteArrayList) {
                if (gVar.a == obj) {
                    gVar.f783c = false;
                    copyOnWriteArrayList.remove(gVar);
                }
            }
        }
    }

    void e(g gVar, Object obj) {
        try {
            gVar.b.a(gVar.a, 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.f775c.containsKey(obj);
    }

    public synchronized void j(Object obj) {
        if (f(obj)) {
            e.c.d.y.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 f(this, cls, method, onEventOfOS.threadMode()));
                a aVar = new a();
                this.b.put(cls, aVar);
                e.c.d.c.p().y(cls, aVar);
            }
        }
        if (!f(obj)) {
            e.c.d.y.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)) {
            e.c.d.y.e.b("OsEventBus", obj.getClass().getSimpleName() + " has not been registered. It can not be unregistered");
            return;
        }
        List<Class<?>> list = this.f775c.get(obj);
        if (list != null) {
            c(obj, list);
        } else {
            Log.w("OsEventBus", "Subscriber to unregister was not registered before: " + obj.getClass());
        }
    }
}
