package b.j;

import b.j.d;
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 */
public final class a {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public static a f1277a = new a();

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

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

    /* JADX INFO: renamed from: b.j.a$a, reason: collision with other inner class name */
    public static class C0022a {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        public final Map<d.a, List<b>> f1280a = new HashMap();

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

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

        public static void a(List<b> list, g gVar, d.a aVar, Object obj) {
            if (list != null) {
                for (int size = list.size() - 1; size >= 0; size--) {
                    b bVar = list.get(size);
                    Objects.requireNonNull(bVar);
                    try {
                        int i = bVar.f1282a;
                        if (i == 0) {
                            bVar.f1283b.invoke(obj, new Object[0]);
                        } else if (i == 1) {
                            bVar.f1283b.invoke(obj, gVar);
                        } else if (i == 2) {
                            bVar.f1283b.invoke(obj, gVar, aVar);
                        }
                    } catch (IllegalAccessException e2) {
                        throw new RuntimeException(e2);
                    } catch (InvocationTargetException e3) {
                        throw new RuntimeException("Failed to call observer method", e3.getCause());
                    }
                }
            }
        }
    }

    public static final class b {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        public final int f1282a;

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

        public b(int i, Method method) {
            this.f1282a = i;
            this.f1283b = method;
            method.setAccessible(true);
        }

        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (!(obj instanceof b)) {
                return false;
            }
            b bVar = (b) obj;
            return this.f1282a == bVar.f1282a && this.f1283b.getName().equals(bVar.f1283b.getName());
        }

        public int hashCode() {
            return this.f1283b.getName().hashCode() + (this.f1282a * 31);
        }
    }

    public final C0022a a(Class<?> cls, Method[] methodArr) {
        int i;
        C0022a c0022aB;
        Class<?> superclass = cls.getSuperclass();
        HashMap map = new HashMap();
        if (superclass != null && (c0022aB = b(superclass)) != null) {
            map.putAll(c0022aB.f1281b);
        }
        for (Class<?> cls2 : cls.getInterfaces()) {
            for (Map.Entry<b, d.a> entry : b(cls2).f1281b.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 z = false;
        for (Method method : methodArr) {
            n nVar = (n) method.getAnnotation(n.class);
            if (nVar != null) {
                Class<?>[] parameterTypes = method.getParameterTypes();
                if (parameterTypes.length <= 0) {
                    i = 0;
                } else {
                    if (!parameterTypes[0].isAssignableFrom(g.class)) {
                        throw new IllegalArgumentException("invalid parameter type. Must be one and instanceof LifecycleOwner");
                    }
                    i = 1;
                }
                d.a aVarValue = nVar.value();
                if (parameterTypes.length > 1) {
                    if (!parameterTypes[1].isAssignableFrom(d.a.class)) {
                        throw new IllegalArgumentException("invalid parameter type. second arg must be an event");
                    }
                    if (aVarValue != d.a.ON_ANY) {
                        throw new IllegalArgumentException("Second arg is supported only for ON_ANY value");
                    }
                    i = 2;
                }
                if (parameterTypes.length > 2) {
                    throw new IllegalArgumentException("cannot have more than 2 params");
                }
                c(map, new b(i, method), aVarValue, cls);
                z = true;
            }
        }
        C0022a c0022a = new C0022a(map);
        this.f1278b.put(cls, c0022a);
        this.f1279c.put(cls, Boolean.valueOf(z));
        return c0022a;
    }

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

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