package c.i;

import c.i.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 f5373a = new a();

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

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

    /* JADX INFO: renamed from: c.i.a$a, reason: collision with other inner class name */
    public static class C0072a {

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

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

        public C0072a(Map<b, d.a> map) {
            this.f5377b = map;
            for (Map.Entry<b, d.a> entry : map.entrySet()) {
                d.a value = entry.getValue();
                List<b> arrayList = this.f5376a.get(value);
                if (arrayList == null) {
                    arrayList = new ArrayList<>();
                    this.f5376a.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 i2 = bVar.f5378a;
                        if (i2 == 0) {
                            bVar.f5379b.invoke(obj, new Object[0]);
                        } else if (i2 == 1) {
                            bVar.f5379b.invoke(obj, gVar);
                        } else if (i2 == 2) {
                            bVar.f5379b.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 f5378a;

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

        public b(int i2, Method method) {
            this.f5378a = i2;
            this.f5379b = 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.f5378a == bVar.f5378a && this.f5379b.getName().equals(bVar.f5379b.getName());
        }

        public int hashCode() {
            return this.f5379b.getName().hashCode() + (this.f5378a * 31);
        }
    }

    public final C0072a a(Class<?> cls, Method[] methodArr) {
        int i2;
        C0072a c0072aB;
        Class<?> superclass = cls.getSuperclass();
        HashMap map = new HashMap();
        if (superclass != null && (c0072aB = b(superclass)) != null) {
            map.putAll(c0072aB.f5377b);
        }
        for (Class<?> cls2 : cls.getInterfaces()) {
            for (Map.Entry<b, d.a> entry : b(cls2).f5377b.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) {
            m mVar = (m) method.getAnnotation(m.class);
            if (mVar != null) {
                Class<?>[] parameterTypes = method.getParameterTypes();
                if (parameterTypes.length <= 0) {
                    i2 = 0;
                } else {
                    if (!parameterTypes[0].isAssignableFrom(g.class)) {
                        throw new IllegalArgumentException("invalid parameter type. Must be one and instanceof LifecycleOwner");
                    }
                    i2 = 1;
                }
                d.a aVarValue = mVar.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");
                    }
                    i2 = 2;
                }
                if (parameterTypes.length > 2) {
                    throw new IllegalArgumentException("cannot have more than 2 params");
                }
                c(map, new b(i2, method), aVarValue, cls);
                z = true;
            }
        }
        C0072a c0072a = new C0072a(map);
        this.f5374b.put(cls, c0072a);
        this.f5375c.put(cls, Boolean.valueOf(z));
        return c0072a;
    }

    public C0072a b(Class<?> cls) {
        C0072a c0072a = this.f5374b.get(cls);
        return c0072a != null ? c0072a : 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.f5379b;
        StringBuilder sbC = a.b.c.a.a.C("Method ");
        sbC.append(method.getName());
        sbC.append(" in ");
        sbC.append(cls.getName());
        sbC.append(" already declared with different @OnLifecycleEvent value: previous value ");
        sbC.append(aVar2);
        sbC.append(", new value ");
        sbC.append(aVar);
        throw new IllegalArgumentException(sbC.toString());
    }
}
