package com.github.mustachejava.reflect;

import com.github.mustachejava.Binding;
import com.github.mustachejava.Code;
import com.github.mustachejava.ObjectHandler;
import com.github.mustachejava.TemplateContext;
import com.github.mustachejava.reflect.guards.ClassGuard;
import com.github.mustachejava.reflect.guards.DepthGuard;
import com.github.mustachejava.reflect.guards.DotGuard;
import com.github.mustachejava.reflect.guards.MapGuard;
import com.github.mustachejava.reflect.guards.NullGuard;
import com.github.mustachejava.reflect.guards.WrappedGuard;
import com.github.mustachejava.util.GuardException;
import com.github.mustachejava.util.Wrapper;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/* JADX INFO: loaded from: classes.dex */
public class ReflectionObjectHandler extends BaseObjectHandler {
    protected static final Method MAP_METHOD;
    private static final Wrapper[] EMPTY_WRAPPERS = new Wrapper[0];
    private static final Guard[] EMPTY_GUARDS = new Guard[0];

    protected boolean areMethodsAccessible(Map<?, ?> map) {
        return false;
    }

    static {
        try {
            MAP_METHOD = Map.class.getMethod("get", Object.class);
        } catch (NoSuchMethodException e) {
            throw new AssertionError(e);
        }
    }

    public static Object unwrap(ObjectHandler objectHandler, int i, Wrapper[] wrapperArr, List<Object> list) throws GuardException {
        Object objCoerce = objectHandler.coerce(list.get(i));
        if (wrapperArr != null) {
            for (Wrapper wrapper : wrapperArr) {
                objCoerce = objectHandler.coerce(wrapper.call(ObjectHandler.makeList(objCoerce)));
            }
        }
        return objCoerce;
    }

    @Override // com.github.mustachejava.ObjectHandler
    public Wrapper find(String str, List<Object> list) {
        String strSubstring;
        int size = list.size();
        ArrayList arrayList = new ArrayList(size);
        arrayList.add(createDepthGuard(size));
        int i = 1;
        int i2 = size - 1;
        Wrapper wrapperFindWrapper = null;
        while (true) {
            if (i2 < 0) {
                break;
            }
            Object objCoerce = list.get(i2);
            if (objCoerce != null) {
                arrayList.add(createClassGuard(i2, objCoerce));
                wrapperFindWrapper = findWrapper(i2, null, arrayList, objCoerce, str);
                if (wrapperFindWrapper != null) {
                    break;
                }
                String str2 = str;
                ArrayList arrayList2 = null;
                while (true) {
                    int iIndexOf = str2.indexOf(46);
                    if (iIndexOf == -1) {
                        strSubstring = str2;
                        break;
                    }
                    String strSubstring2 = str2.substring(0, iIndexOf);
                    strSubstring = str2.substring(iIndexOf + 1);
                    arrayList.add(createDotGuard(i2, objCoerce, strSubstring2));
                    ArrayList arrayList3 = new ArrayList(i);
                    arrayList3.add(createClassGuard(0, coerce(objCoerce)));
                    wrapperFindWrapper = findWrapper(0, null, arrayList3, objCoerce, strSubstring2);
                    if (arrayList2 == null) {
                        arrayList2 = new ArrayList();
                    }
                    if (wrapperFindWrapper != null) {
                        arrayList2.add(wrapperFindWrapper);
                        try {
                            objCoerce = coerce(wrapperFindWrapper.call(ObjectHandler.makeList(coerce(objCoerce))));
                            if (objCoerce == null) {
                                arrayList.add(createWrappedGuard(i2, arrayList2, Collections.singletonList(createNullGuard())));
                                break;
                            }
                            str2 = strSubstring;
                            i = 1;
                        } catch (GuardException e) {
                            throw new AssertionError(e);
                        }
                    } else {
                        arrayList.add(createWrappedGuard(i2, arrayList2, arrayList3));
                        break;
                    }
                }
                Object obj = objCoerce;
                if (arrayList2 != null) {
                    arrayList.add(createWrappedGuard(i2, arrayList2, Collections.singletonList(new ClassGuard(0, obj))));
                }
                wrapperFindWrapper = findWrapper(i2, arrayList2 == null ? null : (Wrapper[]) arrayList2.toArray(EMPTY_WRAPPERS), arrayList, obj, strSubstring);
                if (wrapperFindWrapper != null) {
                    break;
                }
                if (arrayList2 != null) {
                    wrapperFindWrapper = createMissingWrapper(strSubstring, arrayList);
                    break;
                }
            }
            i2--;
            i = 1;
        }
        return wrapperFindWrapper == null ? createMissingWrapper(str, arrayList) : wrapperFindWrapper;
    }

    protected Wrapper findWrapper(int i, Wrapper[] wrapperArr, List<Guard> list, Object obj, String str) {
        Object objCoerce = coerce(obj);
        if (objCoerce == null) {
            return null;
        }
        if (objCoerce instanceof Map) {
            Map<?, ?> map = (Map) objCoerce;
            if (map.containsKey(str)) {
                list.add(createMapGuard(i, wrapperArr, str, true));
                return createWrapper(i, wrapperArr, list, MAP_METHOD, new Object[]{str});
            }
            list.add(createMapGuard(i, wrapperArr, str, false));
            if (!areMethodsAccessible(map)) {
                return null;
            }
        }
        AccessibleObject accessibleObjectFindMember = findMember(objCoerce.getClass(), str);
        if (accessibleObjectFindMember == null) {
            return null;
        }
        return createWrapper(i, wrapperArr, list, accessibleObjectFindMember, new Object[0]);
    }

    protected MissingWrapper createMissingWrapper(String str, List<Guard> list) {
        return new MissingWrapper(str, (Guard[]) list.toArray(EMPTY_GUARDS));
    }

    protected DotGuard createDotGuard(int i, Object obj, String str) {
        return new DotGuard(str, i, obj);
    }

    protected WrappedGuard createWrappedGuard(int i, List<Wrapper> list, List<Guard> list2) {
        return new WrappedGuard(this, i, list, list2);
    }

    protected NullGuard createNullGuard() {
        return new NullGuard();
    }

    protected DepthGuard createDepthGuard(int i) {
        return new DepthGuard(i);
    }

    protected ClassGuard createClassGuard(int i, Object obj) {
        return new ClassGuard(i, obj);
    }

    protected MapGuard createMapGuard(int i, Wrapper[] wrapperArr, String str, boolean z) {
        return new MapGuard(this, i, str, z, wrapperArr);
    }

    protected Wrapper createWrapper(int i, Wrapper[] wrapperArr, List<? extends Guard> list, AccessibleObject accessibleObject, Object[] objArr) {
        return new ReflectionWrapper(i, wrapperArr, (Guard[]) list.toArray(EMPTY_GUARDS), accessibleObject, objArr, this);
    }

    @Override // com.github.mustachejava.reflect.BaseObjectHandler, com.github.mustachejava.ObjectHandler
    public Binding createBinding(String str, TemplateContext templateContext, Code code) {
        return new GuardedBinding(this, str, templateContext, code);
    }
}
