package com.parse;

import android.content.Context;
import android.content.Intent;
import com.parse.ConnectivityNotifier;
import com.parse.OfflineStore;
import com.parse.ParseObject;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import org.json.JSONException;
import org.json.JSONObject;
import p137.C2742;
import p137.C2754;
import p137.InterfaceC2741;

/* JADX INFO: loaded from: classes.dex */
public class ParsePinningEventuallyQueue extends ParseEventuallyQueue {
    public C2754<Void> connectionTaskCompletionSource;
    public final ParseHttpClient httpClient;
    public ConnectivityNotifier notifier;
    public final Object connectionLock = new Object();
    public final Object taskQueueSyncLock = new Object();
    public HashMap<String, C2754<JSONObject>> pendingOperationSetUUIDTasks = new HashMap<>();
    public TaskQueue taskQueue = new TaskQueue();
    public TaskQueue operationSetTaskQueue = new TaskQueue();
    public ArrayList<String> eventuallyPinUUIDQueue = new ArrayList<>();
    public ConnectivityNotifier.ConnectivityListener listener = new ConnectivityNotifier.ConnectivityListener() { // from class: com.parse.ParsePinningEventuallyQueue.1
        @Override // com.parse.ConnectivityNotifier.ConnectivityListener
        public void networkConnectivityStatusChanged(Context context, Intent intent) {
            if (intent.getBooleanExtra("noConnectivity", false)) {
                ParsePinningEventuallyQueue.this.setConnected(false);
            } else {
                ParsePinningEventuallyQueue.this.setConnected(ConnectivityNotifier.isConnected(context));
            }
        }
    };
    public HashMap<String, C2754<JSONObject>> pendingEventuallyTasks = new HashMap<>();
    public HashMap<String, ParseOperationSet> uuidToOperationSet = new HashMap<>();
    public HashMap<String, EventuallyPin> uuidToEventuallyPin = new HashMap<>();

    /* JADX INFO: renamed from: com.parse.ParsePinningEventuallyQueue$13, reason: invalid class name */
    public class AnonymousClass13 implements InterfaceC2741<Void, C2742<JSONObject>> {
        public final /* synthetic */ EventuallyPin val$eventuallyPin;
        public final /* synthetic */ ParseOperationSet val$operationSet;

        public AnonymousClass13(EventuallyPin eventuallyPin, ParseOperationSet parseOperationSet) {
            this.val$eventuallyPin = eventuallyPin;
            this.val$operationSet = parseOperationSet;
        }

        @Override // p137.InterfaceC2741
        public C2742<JSONObject> then(C2742<Void> c2742) throws JSONException {
            ParseRESTCommand parseRESTCommandFromJSONObject;
            C2742 c2742ExecuteAsync;
            Number number = this.val$eventuallyPin.getNumber("type");
            final int iIntValue = number == null ? 0 : number.intValue();
            Object obj = this.val$eventuallyPin.get("object");
            final ParseObject parseObject = !(obj instanceof ParseObject) ? null : (ParseObject) obj;
            String string = this.val$eventuallyPin.getString("sessionToken");
            if (iIntValue == 1) {
                ParseHttpClient parseHttpClient = ParsePinningEventuallyQueue.this.httpClient;
                ParseOperationSet parseOperationSet = this.val$operationSet;
                if (parseObject == null) {
                    throw null;
                }
                PointerEncoder pointerEncoder = PointerEncoder.INSTANCE;
                ParseObject.State state = parseObject.getState();
                JSONObject jSONObject = new JSONObject();
                try {
                    for (String str : parseOperationSet.keySet()) {
                        jSONObject.put(str, pointerEncoder.encode((ParseFieldOperation) parseOperationSet.get(str)));
                    }
                    if (state.objectId != null) {
                        jSONObject.put("objectId", state.objectId);
                    }
                    c2742ExecuteAsync = ParseRESTObjectCommand.saveObjectCommand(state, jSONObject, string).executeAsync(parseHttpClient, null, null, null);
                } catch (JSONException unused) {
                    throw new RuntimeException("could not serialize object to JSON");
                }
            } else if (iIntValue != 2) {
                JSONObject jSONObject2 = this.val$eventuallyPin.getJSONObject("command");
                if (ParseRESTCommand.isValidCommandJSONObject(jSONObject2)) {
                    parseRESTCommandFromJSONObject = ParseRESTCommand.fromJSONObject(jSONObject2);
                } else {
                    if (!jSONObject2.has("op")) {
                        throw new JSONException("Failed to load command from JSON.");
                    }
                    parseRESTCommandFromJSONObject = null;
                }
                if (parseRESTCommandFromJSONObject == null) {
                    c2742ExecuteAsync = C2742.m4805((Object) null);
                    if (ParsePinningEventuallyQueue.this == null) {
                        throw null;
                    }
                } else {
                    c2742ExecuteAsync = parseRESTCommandFromJSONObject.executeAsync(ParsePinningEventuallyQueue.this.httpClient);
                }
            } else if (parseObject == null || (c2742ExecuteAsync = ParseObject.getObjectController().deleteAsync(parseObject.getState(), string)) == null) {
                throw null;
            }
            return c2742ExecuteAsync.m4815(new InterfaceC2741<JSONObject, C2742<JSONObject>>() { // from class: com.parse.ParsePinningEventuallyQueue.13.1
                @Override // p137.InterfaceC2741
                public C2742<JSONObject> then(final C2742<JSONObject> c27422) {
                    Exception excM4806 = c27422.m4806();
                    if (excM4806 != null && (excM4806 instanceof ParseException) && ((ParseException) excM4806).code == 100) {
                        ParsePinningEventuallyQueue.this.setConnected(false);
                        AnonymousClass13 anonymousClass13 = AnonymousClass13.this;
                        ParsePinningEventuallyQueue parsePinningEventuallyQueue = ParsePinningEventuallyQueue.this;
                        if (parsePinningEventuallyQueue != null) {
                            return parsePinningEventuallyQueue.process(anonymousClass13.val$eventuallyPin, anonymousClass13.val$operationSet);
                        }
                        throw null;
                    }
                    EventuallyPin eventuallyPin = AnonymousClass13.this.val$eventuallyPin;
                    if (eventuallyPin == null) {
                        throw null;
                    }
                    final List listSingletonList = Collections.singletonList(eventuallyPin);
                    if (!Parse.isLocalDatastoreEnabled) {
                        throw new IllegalStateException("Method requires Local Datastore. Please refer to `Parse#enableLocalDatastore(Context)`.");
                    }
                    final OfflineStore offlineStore = Parse.offlineStore;
                    if (offlineStore == null) {
                        throw null;
                    }
                    final String str2 = "_eventuallyPin";
                    return offlineStore.runWithManagedTransaction(new OfflineStore.SQLiteDatabaseCallable<C2742<Void>>() { // from class: com.parse.OfflineStore.39
                        public final /* synthetic */ String val$name;
                        public final /* synthetic */ List val$objects;

                        public AnonymousClass39(final String str22, final List listSingletonList2) {
                            str = str22;
                            list = listSingletonList2;
                        }

                        @Override // com.parse.OfflineStore.SQLiteDatabaseCallable
                        public C2742<Void> call(ParseSQLiteDatabase parseSQLiteDatabase) {
                            OfflineStore offlineStore2 = OfflineStore.this;
                            String str3 = str;
                            List list = list;
                            if (offlineStore2 != null) {
                                return (list == null || list.size() == 0) ? C2742.m4805((Object) null) : offlineStore2.getParsePin(str3, parseSQLiteDatabase).m4817(new InterfaceC2741<ParsePin, C2742<Void>>() { // from class: com.parse.OfflineStore.40
                                    public final /* synthetic */ ParseSQLiteDatabase val$db;
                                    public final /* synthetic */ List val$objects;

                                    public AnonymousClass40(List list2, ParseSQLiteDatabase parseSQLiteDatabase2) {
                                        list = list2;
                                        parseSQLiteDatabase = parseSQLiteDatabase2;
                                    }

                                    @Override // p137.InterfaceC2741
                                    public C2742<Void> then(C2742<ParsePin> c27423) {
                                        ParsePin parsePinM4812 = c27423.m4812();
                                        List<ParseObject> objects = parsePinM4812.getObjects();
                                        if (objects == null) {
                                            return C2742.m4805((Object) null);
                                        }
                                        objects.removeAll(list);
                                        if (objects.size() == 0) {
                                            return OfflineStore.access$1800(OfflineStore.this, parsePinM4812, parseSQLiteDatabase);
                                        }
                                        parsePinM4812.put("_objects", objects);
                                        return OfflineStore.access$1300(OfflineStore.this, parsePinM4812, true, parseSQLiteDatabase);
                                    }
                                }, C2742.f8455);
                            }
                            throw null;
                        }
                    }).m4815(new InterfaceC2741<Void, C2742<Void>>() { // from class: com.parse.ParsePinningEventuallyQueue.13.1.2
                        @Override // p137.InterfaceC2741
                        public C2742<Void> then(C2742<Void> c27423) {
                            JSONObject jSONObject3 = (JSONObject) c27422.m4812();
                            AnonymousClass1 anonymousClass1 = AnonymousClass1.this;
                            int i = iIntValue;
                            if (i != 1) {
                                return (i != 2 || c27422.m4820()) ? c27423 : parseObject.handleDeleteEventuallyResultAsync();
                            }
                            ParseObject parseObject2 = parseObject;
                            return parseObject2.handleSaveResultAsync(jSONObject3, AnonymousClass13.this.val$operationSet).m4817(new InterfaceC2741<Void, C2742<Void>>(parseObject2, jSONObject3 != null) { // from class: com.parse.ParseObject.35
                                public final /* synthetic */ boolean val$success;

                                public AnonymousClass35(ParseObject parseObject22, boolean z) {
                                    this.val$success = z;
                                }

                                @Override // p137.InterfaceC2741
                                public C2742<Void> then(C2742<Void> c27424) {
                                    if (this.val$success && Parse.getEventuallyQueue() == null) {
                                        throw null;
                                    }
                                    return c27424;
                                }
                            }, C2742.f8455);
                        }
                    }, C2742.f8455, null).m4815(new InterfaceC2741<Void, C2742<JSONObject>>(this) { // from class: com.parse.ParsePinningEventuallyQueue.13.1.1
                        @Override // p137.InterfaceC2741
                        public C2742<JSONObject> then(C2742<Void> c27423) {
                            return c27422;
                        }
                    }, C2742.f8455, null);
                }
            }, C2742.f8455, null);
        }
    }

    public ParsePinningEventuallyQueue(Context context, ParseHttpClient parseHttpClient) {
        this.connectionTaskCompletionSource = new C2754<>();
        setConnected(ConnectivityNotifier.isConnected(context));
        this.httpClient = parseHttpClient;
        ConnectivityNotifier.singleton.tryToRegisterForNetworkStatusNotifications(context);
        ConnectivityNotifier connectivityNotifier = ConnectivityNotifier.singleton;
        this.notifier = connectivityNotifier;
        connectivityNotifier.addListener(this.listener);
        if (this.isConnected) {
            this.connectionTaskCompletionSource.m4828(null);
            C2754<Void> c2754 = new C2754<>();
            this.connectionTaskCompletionSource = c2754;
            c2754.m4828(null);
        } else {
            this.connectionTaskCompletionSource = new C2754<>();
        }
        this.taskQueue.enqueue(new InterfaceC2741<Void, C2742<Void>>() { // from class: com.parse.ParsePinningEventuallyQueue.6
            @Override // p137.InterfaceC2741
            public C2742<Void> then(C2742<Void> c2742) {
                final ParsePinningEventuallyQueue parsePinningEventuallyQueue = ParsePinningEventuallyQueue.this;
                if (parsePinningEventuallyQueue != null) {
                    return c2742.m4815(new InterfaceC2741<Void, C2742<List<EventuallyPin>>>() { // from class: com.parse.ParsePinningEventuallyQueue.8
                        @Override // p137.InterfaceC2741
                        public C2742<List<EventuallyPin>> then(C2742<Void> c27422) {
                            return EventuallyPin.findAllPinned(ParsePinningEventuallyQueue.this.eventuallyPinUUIDQueue);
                        }
                    }, C2742.f8455, null).m4817(new InterfaceC2741<List<EventuallyPin>, C2742<Void>>() { // from class: com.parse.ParsePinningEventuallyQueue.7
                        @Override // p137.InterfaceC2741
                        public C2742<Void> then(C2742<List<EventuallyPin>> c27422) {
                            for (final EventuallyPin eventuallyPin : c27422.m4812()) {
                                final ParsePinningEventuallyQueue parsePinningEventuallyQueue2 = ParsePinningEventuallyQueue.this;
                                if (parsePinningEventuallyQueue2 == null) {
                                    throw null;
                                }
                                final String string = eventuallyPin.getString("uuid");
                                if (parsePinningEventuallyQueue2.eventuallyPinUUIDQueue.contains(string)) {
                                    C2742.m4805((Object) null);
                                } else {
                                    parsePinningEventuallyQueue2.eventuallyPinUUIDQueue.add(string);
                                    parsePinningEventuallyQueue2.operationSetTaskQueue.enqueue(new InterfaceC2741<Void, C2742<Void>>() { // from class: com.parse.ParsePinningEventuallyQueue.9
                                        @Override // p137.InterfaceC2741
                                        public C2742<Void> then(C2742<Void> c27423) {
                                            final ParsePinningEventuallyQueue parsePinningEventuallyQueue3 = ParsePinningEventuallyQueue.this;
                                            final EventuallyPin eventuallyPin2 = eventuallyPin;
                                            if (parsePinningEventuallyQueue3 != null) {
                                                return c27423.m4815(new InterfaceC2741<Void, C2742<Void>>() { // from class: com.parse.ParsePinningEventuallyQueue.11
                                                    @Override // p137.InterfaceC2741
                                                    public C2742<Void> then(C2742<Void> c27424) {
                                                        return ParsePinningEventuallyQueue.this.waitForConnectionAsync();
                                                    }
                                                }, C2742.f8455, null).m4817(new InterfaceC2741<Void, C2742<Void>>() { // from class: com.parse.ParsePinningEventuallyQueue.10
                                                    @Override // p137.InterfaceC2741
                                                    public C2742<Void> then(C2742<Void> c27424) {
                                                        return ParsePinningEventuallyQueue.this.waitForOperationSetAndEventuallyPin(null, eventuallyPin2).m4815(new InterfaceC2741<JSONObject, C2742<Void>>() { // from class: com.parse.ParsePinningEventuallyQueue.10.1
                                                            @Override // p137.InterfaceC2741
                                                            public C2742<Void> then(C2742<JSONObject> c27425) {
                                                                Exception excM4806 = c27425.m4806();
                                                                if (excM4806 != null) {
                                                                    Parse.getLogLevel();
                                                                    if (ParsePinningEventuallyQueue.this == null) {
                                                                        throw null;
                                                                    }
                                                                } else if (ParsePinningEventuallyQueue.this == null) {
                                                                    throw null;
                                                                }
                                                                AnonymousClass10 anonymousClass10 = AnonymousClass10.this;
                                                                C2754<JSONObject> c2754Remove = ParsePinningEventuallyQueue.this.pendingOperationSetUUIDTasks.remove(eventuallyPin2.getString("uuid"));
                                                                if (c2754Remove != null) {
                                                                    if (excM4806 != null) {
                                                                        c2754Remove.m4826(excM4806);
                                                                    } else {
                                                                        c2754Remove.m4827(c27425.m4812());
                                                                    }
                                                                }
                                                                return c27425.m4821();
                                                            }
                                                        }, C2742.f8455, null);
                                                    }
                                                }, C2742.f8455).m4815(new InterfaceC2741<Void, C2742<Void>>() { // from class: com.parse.ParsePinningEventuallyQueue.9.1
                                                    @Override // p137.InterfaceC2741
                                                    public C2742<Void> then(C2742<Void> c27424) {
                                                        AnonymousClass9 anonymousClass9 = AnonymousClass9.this;
                                                        ParsePinningEventuallyQueue.this.eventuallyPinUUIDQueue.remove(string);
                                                        return c27424;
                                                    }
                                                }, C2742.f8455, null);
                                            }
                                            throw null;
                                        }
                                    });
                                    C2742.m4805((Object) null);
                                }
                            }
                            return c27422.m4821();
                        }
                    }, C2742.f8455);
                }
                throw null;
            }
        });
    }

    public final C2742<JSONObject> process(EventuallyPin eventuallyPin, ParseOperationSet parseOperationSet) {
        return waitForConnectionAsync().m4817(new AnonymousClass13(eventuallyPin, parseOperationSet), C2742.f8455);
    }

    public void setConnected(boolean z) {
        synchronized (this.connectionLock) {
            if (this.isConnected != z) {
                this.isConnected = z;
                if (z) {
                    this.connectionTaskCompletionSource.m4828(null);
                    C2754<Void> c2754 = new C2754<>();
                    this.connectionTaskCompletionSource = c2754;
                    c2754.m4828(null);
                } else {
                    this.connectionTaskCompletionSource = new C2754<>();
                }
            }
        }
    }

    public final C2742<Void> waitForConnectionAsync() {
        C2742<Void> c2742;
        synchronized (this.connectionLock) {
            c2742 = this.connectionTaskCompletionSource.f8503;
        }
        return c2742;
    }

    @Override // com.parse.ParseEventuallyQueue
    public C2742<JSONObject> waitForOperationSetAndEventuallyPin(ParseOperationSet parseOperationSet, EventuallyPin eventuallyPin) {
        final String str;
        C2754<JSONObject> c2754;
        if (eventuallyPin != null) {
            Number number = eventuallyPin.getNumber("type");
            if ((number == null ? 0 : number.intValue()) != 1) {
                return process(eventuallyPin, null);
            }
        }
        synchronized (this.taskQueueSyncLock) {
            if (parseOperationSet != null && eventuallyPin == null) {
                str = parseOperationSet.uuid;
                this.uuidToOperationSet.put(str, parseOperationSet);
            } else {
                if (parseOperationSet != null || eventuallyPin == null) {
                    throw new IllegalStateException("Either operationSet or eventuallyPin must be set.");
                }
                String string = eventuallyPin.getString("operationSetUUID");
                this.uuidToEventuallyPin.put(string, eventuallyPin);
                str = string;
            }
            EventuallyPin eventuallyPin2 = this.uuidToEventuallyPin.get(str);
            ParseOperationSet parseOperationSet2 = this.uuidToOperationSet.get(str);
            if (eventuallyPin2 != null && parseOperationSet2 != null) {
                final C2754<JSONObject> c27542 = this.pendingEventuallyTasks.get(str);
                return process(eventuallyPin2, parseOperationSet2).m4815(new InterfaceC2741<JSONObject, C2742<JSONObject>>() { // from class: com.parse.ParsePinningEventuallyQueue.12
                    @Override // p137.InterfaceC2741
                    public C2742<JSONObject> then(C2742<JSONObject> c2742) {
                        synchronized (ParsePinningEventuallyQueue.this.taskQueueSyncLock) {
                            ParsePinningEventuallyQueue.this.pendingEventuallyTasks.remove(str);
                            ParsePinningEventuallyQueue.this.uuidToOperationSet.remove(str);
                            ParsePinningEventuallyQueue.this.uuidToEventuallyPin.remove(str);
                        }
                        Exception excM4806 = c2742.m4806();
                        if (excM4806 != null) {
                            c27542.f8503.m4810(excM4806);
                        } else if (c2742.m4818()) {
                            c27542.f8503.m4823();
                        } else {
                            c27542.m4828(c2742.m4812());
                        }
                        return c27542.f8503;
                    }
                }, C2742.f8455, null);
            }
            if (this.pendingEventuallyTasks.containsKey(str)) {
                c2754 = this.pendingEventuallyTasks.get(str);
            } else {
                c2754 = new C2754<>();
                this.pendingEventuallyTasks.put(str, c2754);
            }
            return c2754.f8503;
        }
    }
}
