package d.m;

import d.m.e;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/* JADX INFO: loaded from: classes.dex */
@Deprecated
public final class b {
    public static b a = new b();

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final Map<Class<?>, a> f3180b = new HashMap();

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final Map<Class<?>, Boolean> f3181c = new HashMap();

    @Deprecated
    public static class a {
        public final Map<e.a, List<C0041b>> a = new HashMap();

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        public final Map<C0041b, e.a> f3182b;

        public a(Map<C0041b, e.a> map) {
            this.f3182b = map;
            for (Map.Entry<C0041b, e.a> entry : map.entrySet()) {
                e.a value = entry.getValue();
                List<C0041b> arrayList = this.a.get(value);
                if (arrayList == null) {
                    arrayList = new ArrayList<>();
                    this.a.put(value, arrayList);
                }
                arrayList.add(entry.getKey());
            }
        }

        public static void a(List<C0041b> list, h hVar, e.a aVar, Object obj) {
            if (list != null) {
                for (int size = list.size() - 1; size >= 0; size--) {
                    C0041b c0041b = list.get(size);
                    Objects.requireNonNull(c0041b);
                    try {
                        int i2 = c0041b.a;
                        if (i2 == 0) {
                            c0041b.f3183b.invoke(obj, new Object[0]);
                        } else if (i2 == 1) {
                            c0041b.f3183b.invoke(obj, hVar);
                        } else if (i2 == 2) {
                            c0041b.f3183b.invoke(obj, hVar, aVar);
                        }
                    } catch (IllegalAccessException e2) {
                        throw new RuntimeException(e2);
                    } catch (InvocationTargetException e3) {
                        throw new RuntimeException("Failed to call observer method", e3.getCause());
                    }
                }
            }
        }
    }

    /* JADX INFO: renamed from: d.m.b$b, reason: collision with other inner class name */
    @Deprecated
    public static final class C0041b {
        public final int a;

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

        public C0041b(int i2, Method method) {
            this.a = i2;
            this.f3183b = method;
            method.setAccessible(true);
        }

        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (!(obj instanceof C0041b)) {
                return false;
            }
            C0041b c0041b = (C0041b) obj;
            return this.a == c0041b.a && this.f3183b.getName().equals(c0041b.f3183b.getName());
        }

        public int hashCode() {
            return this.f3183b.getName().hashCode() + (this.a * 31);
        }
    }

    public final a a(Class<?> cls, Method[] methodArr) {
        int i2;
        a aVarB;
        Class<?> superclass = cls.getSuperclass();
        HashMap map = new HashMap();
        if (superclass != null && (aVarB = b(superclass)) != null) {
            map.putAll(aVarB.f3182b);
        }
        for (Class<?> cls2 : cls.getInterfaces()) {
            for (Map.Entry<C0041b, e.a> entry : b(cls2).f3182b.entrySet()) {
                c(map, entry.getKey(), entry.getValue(), cls);
            }
        }
        if (methodArr == null) {
            try {
                methodArr = cls.getDeclaredMethods();
            } catch (NoClassDefFoundError e2) {
                throw new IllegalArgumentException("The observer class has some methods that use newer APIs which are not available in the current OS version. Lifecycles cannot access even other methods so you should make sure that your observer classes only access framework classes that are available in your min API level OR use lifecycle:compiler annotation processor.", e2);
            }
        }
        boolean z2 = false;
        for (Method method : methodArr) {
            o oVar = (o) method.getAnnotation(o.class);
            if (oVar != null) {
                Class<?>[] parameterTypes = method.getParameterTypes();
                if (parameterTypes.length <= 0) {
                    i2 = 0;
                } else {
                    if (!parameterTypes[0].isAssignableFrom(h.class)) {
                        throw new IllegalArgumentException("invalid parameter type. Must be one and instanceof LifecycleOwner");
                    }
                    i2 = 1;
                }
                e.a aVarValue = oVar.value();
                if (parameterTypes.length > 1) {
                    if (!parameterTypes[1].isAssignableFrom(e.a.class)) {
                        throw new IllegalArgumentException("invalid parameter type. second arg must be an event");
                    }
                    if (aVarValue != e.a.ON_ANY) {
                        throw new IllegalArgumentException("Second arg is supported only for ON_ANY value");
                    }
                    i2 = 2;
                }
                if (parameterTypes.length > 2) {
                    throw new IllegalArgumentException("cannot have more than 2 params");
                }
                c(map, new C0041b(i2, method), aVarValue, cls);
                z2 = true;
            }
        }
        a aVar = new a(map);
        this.f3180b.put(cls, aVar);
        this.f3181c.put(cls, Boolean.valueOf(z2));
        return aVar;
    }

    public a b(Class<?> cls) {
        a aVar = this.f3180b.get(cls);
        return aVar != null ? aVar : a(cls, null);
    }

    public final void c(Map<C0041b, e.a> map, C0041b c0041b, e.a aVar, Class<?> cls) {
        e.a aVar2 = map.get(c0041b);
        if (aVar2 == null || aVar == aVar2) {
            if (aVar2 == null) {
                map.put(c0041b, aVar);
                return;
            }
            return;
        }
        Method method = c0041b.f3183b;
        StringBuilder sbF = g.a.a.a.a.F("Method ");
        sbF.append(method.getName());
        sbF.append(" in ");
        sbF.append(cls.getName());
        sbF.append(" already declared with different @OnLifecycleEvent value: previous value ");
        sbF.append(aVar2);
        sbF.append(", new value ");
        sbF.append(aVar);
        throw new IllegalArgumentException(sbF.toString());
    }
}
