package com.parse;

import com.parse.ParseObject;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import org.json.JSONException;
import org.json.JSONObject;
import p137.C2742;
import p137.C2754;
import p137.InterfaceC2741;
import p138.p176.p177.p242.p246.p251.C4303;

/* JADX INFO: loaded from: classes.dex */
public class ParseQuery<T extends ParseObject> {
    public final State.Builder<T> builder;
    public Set<C2754<?>> currentTasks;

    public enum CachePolicy {
        IGNORE_CACHE,
        CACHE_ONLY,
        NETWORK_ONLY,
        CACHE_ELSE_NETWORK,
        NETWORK_ELSE_CACHE,
        CACHE_THEN_NETWORK
    }

    public static class KeyConstraints extends HashMap<String, Object> {
    }

    public static class QueryConstraints extends HashMap<String, Object> {
        public QueryConstraints() {
        }

        public QueryConstraints(Map<? extends String, ?> map) {
            super(map);
        }
    }

    public static class RelationConstraint {
    }

    public static class State<T extends ParseObject> {
        public final CachePolicy cachePolicy;
        public final String className;
        public final Map<String, Object> extraOptions;
        public final boolean ignoreACLs;
        public final Set<String> include;
        public final boolean isFromLocalDatastore;
        public final int limit;
        public final long maxCacheAge;
        public final List<String> order;
        public final String pinName;
        public final Set<String> selectedKeys;
        public final int skip;
        public final boolean trace;
        public final QueryConstraints where;

        public static class Builder<T extends ParseObject> {
            public final String className;
            public boolean ignoreACLs;
            public String pinName;
            public Set<String> selectedKeys;
            public boolean trace;
            public final QueryConstraints where = new QueryConstraints();
            public final Set<String> includes = new HashSet();
            public final Map<String, Object> extraOptions = new HashMap();
            public int limit = -1;
            public int skip = 0;
            public List<String> order = new ArrayList();
            public CachePolicy cachePolicy = CachePolicy.IGNORE_CACHE;
            public long maxCacheAge = Long.MAX_VALUE;
            public boolean isFromLocalDatastore = false;

            public Builder(String str) {
                this.className = str;
            }

            public State<T> build() {
                if (this.isFromLocalDatastore || !this.ignoreACLs) {
                    return new State<>(this, null);
                }
                throw new IllegalStateException("`ignoreACLs` cannot be combined with network queries");
            }
        }

        public /* synthetic */ State(Builder builder, AnonymousClass1 anonymousClass1) {
            this.className = builder.className;
            this.where = new QueryConstraints(builder.where);
            this.include = Collections.unmodifiableSet(new HashSet(builder.includes));
            this.selectedKeys = builder.selectedKeys != null ? Collections.unmodifiableSet(new HashSet(builder.selectedKeys)) : null;
            this.limit = builder.limit;
            this.skip = builder.skip;
            this.order = Collections.unmodifiableList(new ArrayList(builder.order));
            this.extraOptions = Collections.unmodifiableMap(new HashMap(builder.extraOptions));
            this.trace = builder.trace;
            this.cachePolicy = builder.cachePolicy;
            this.maxCacheAge = builder.maxCacheAge;
            this.isFromLocalDatastore = builder.isFromLocalDatastore;
            this.pinName = builder.pinName;
            this.ignoreACLs = builder.ignoreACLs;
        }

        public JSONObject toJSON(ParseEncoder parseEncoder) {
            JSONObject jSONObject = new JSONObject();
            try {
                jSONObject.put("className", this.className);
                jSONObject.put("where", parseEncoder.encode(this.where));
                if (this.limit >= 0) {
                    jSONObject.put("limit", this.limit);
                }
                if (this.skip > 0) {
                    jSONObject.put("skip", this.skip);
                }
                if (!this.order.isEmpty()) {
                    jSONObject.put("order", C4303.join(",", this.order));
                }
                if (!this.include.isEmpty()) {
                    jSONObject.put("include", C4303.join(",", this.include));
                }
                if (this.selectedKeys != null) {
                    jSONObject.put("fields", C4303.join(",", this.selectedKeys));
                }
                if (this.trace) {
                    jSONObject.put("trace", 1);
                }
                for (String str : this.extraOptions.keySet()) {
                    jSONObject.put(str, parseEncoder.encode(this.extraOptions.get(str)));
                }
                return jSONObject;
            } catch (JSONException e) {
                throw new RuntimeException(e);
            }
        }

        public String toString() {
            return String.format(Locale.US, "%s[className=%s, where=%s, include=%s, selectedKeys=%s, limit=%s, skip=%s, order=%s, extraOptions=%s, cachePolicy=%s, maxCacheAge=%s, trace=%s]", State.class.getName(), this.className, this.where, this.include, this.selectedKeys, Integer.valueOf(this.limit), Integer.valueOf(this.skip), this.order, this.extraOptions, this.cachePolicy, Long.valueOf(this.maxCacheAge), Boolean.valueOf(this.trace));
        }
    }

    public ParseQuery(Class<T> cls) {
        State.Builder<T> builder = new State.Builder<>(ParseCorePlugins.INSTANCE.getSubclassingController().getClassName(cls));
        this.currentTasks = Collections.synchronizedSet(new HashSet());
        this.builder = builder;
    }

    public ParseQuery(String str) {
        State.Builder<T> builder = new State.Builder<>(str);
        this.currentTasks = Collections.synchronizedSet(new HashSet());
        this.builder = builder;
    }

    public C2742<List<T>> findInBackground() {
        C2742 c2742M4804;
        final State<T> stateBuild = this.builder.build();
        final C2754<?> c2754 = new C2754<>();
        Callable<C2742<List<T>>> callable = new Callable<C2742<List<T>>>() { // from class: com.parse.ParseQuery.3
            @Override // java.util.concurrent.Callable
            public Object call() {
                ParseQuery parseQuery = ParseQuery.this;
                State state = stateBuild;
                if (parseQuery != null) {
                    return (state.ignoreACLs ? C2742.m4805((Object) null) : ParseUser.getCurrentUserAsync()).m4817(new InterfaceC2741<ParseUser, C2742<List<T>>>() { // from class: com.parse.ParseQuery.3.1
                        @Override // p137.InterfaceC2741
                        public Object then(C2742<ParseUser> c2742) {
                            ParseUser parseUserM4812 = c2742.m4812();
                            AnonymousClass3 anonymousClass3 = AnonymousClass3.this;
                            ParseQuery parseQuery2 = ParseQuery.this;
                            State<T> state2 = stateBuild;
                            C2742<TResult> c27422 = c2754.f8503;
                            if (parseQuery2 == null) {
                                throw null;
                            }
                            ParseCorePlugins parseCorePlugins = ParseCorePlugins.INSTANCE;
                            if (parseCorePlugins.queryController.get() == null) {
                                NetworkQueryController networkQueryController = new NetworkQueryController(ParsePlugins.get().restClient());
                                parseCorePlugins.queryController.compareAndSet(null, Parse.isLocalDatastoreEnabled ? new OfflineQueryController(Parse.offlineStore, networkQueryController) : new CacheQueryController(networkQueryController));
                            }
                            return parseCorePlugins.queryController.get().findAsync(state2, parseUserM4812, c27422);
                        }
                    }, C2742.f8455);
                }
                throw null;
            }
        };
        this.currentTasks.add(c2754);
        try {
            c2742M4804 = (C2742) callable.call();
        } catch (Exception e) {
            c2742M4804 = C2742.m4804(e);
        }
        return c2742M4804.m4815(new InterfaceC2741<TResult, C2742<TResult>>() { // from class: com.parse.ParseQuery.1
            @Override // p137.InterfaceC2741
            public Object then(C2742 c2742) {
                c2754.m4828(null);
                ParseQuery.this.currentTasks.remove(c2754);
                return c2742;
            }
        }, C2742.f8455, null);
    }

    public ParseQuery<T> fromPin(String str) {
        State.Builder<T> builder = this.builder;
        if (builder == null) {
            throw null;
        }
        if (!Parse.isLocalDatastoreEnabled) {
            throw new IllegalStateException("Method requires Local Datastore. Please refer to `Parse#enableLocalDatastore(Context)`.");
        }
        builder.isFromLocalDatastore = true;
        builder.pinName = str;
        return this;
    }

    public ParseQuery<T> ignoreACLs() {
        State.Builder<T> builder = this.builder;
        if (builder == null) {
            throw null;
        }
        if (!Parse.isLocalDatastoreEnabled) {
            throw new IllegalStateException("Method requires Local Datastore. Please refer to `Parse#enableLocalDatastore(Context)`.");
        }
        builder.ignoreACLs = true;
        return this;
    }
}
