package com.parse;

import android.os.Parcel;
import android.os.Parcelable;
import com.parse.ParseFile;
import com.parse.ParseRESTFileCommand;
import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.HashSet;
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 ParseFile implements Parcelable {
    public static final Parcelable.Creator<ParseFile> CREATOR = new Parcelable.Creator<ParseFile>() { // from class: com.parse.ParseFile.1
        @Override // android.os.Parcelable.Creator
        public ParseFile createFromParcel(Parcel parcel) {
            return new ParseFile(parcel, ParseParcelDecoder.INSTANCE);
        }

        @Override // android.os.Parcelable.Creator
        public ParseFile[] newArray(int i) {
            return new ParseFile[i];
        }
    };
    public Set<C2754<?>> currentTasks;
    public byte[] data;
    public File file;
    public State state;
    public final TaskQueue taskQueue;

    /* JADX INFO: renamed from: com.parse.ParseFile$6, reason: invalid class name */
    public class AnonymousClass6 implements InterfaceC2741<Void, C2742<Void>> {
        public final /* synthetic */ C2742 val$cancellationToken;
        public final /* synthetic */ String val$sessionToken;
        public final /* synthetic */ ProgressCallback val$uploadProgressCallback;

        public AnonymousClass6(String str, ProgressCallback progressCallback, C2742 c2742) {
            this.val$sessionToken = str;
            this.val$uploadProgressCallback = progressCallback;
            this.val$cancellationToken = c2742;
        }

        @Override // p137.InterfaceC2741
        public C2742<Void> then(C2742<Void> c2742) {
            final ParseFile parseFile = ParseFile.this;
            final String str = this.val$sessionToken;
            final ProgressCallback progressCallback = this.val$uploadProgressCallback;
            final C2742 c27422 = this.val$cancellationToken;
            return !parseFile.isDirty() ? C2742.m4805((Object) null) : (c27422 == null || !c27422.m4818()) ? c2742.m4815(new InterfaceC2741<Void, C2742<Void>>() { // from class: com.parse.ParseFile.3
                @Override // p137.InterfaceC2741
                public C2742<Void> then(C2742<Void> c27423) {
                    C2742 c2742M4814;
                    if (!ParseFile.this.isDirty()) {
                        return C2742.m4805((Object) null);
                    }
                    C2742 c27424 = c27422;
                    if (c27424 != null && c27424.m4818()) {
                        return C2742.f8460;
                    }
                    if (ParseFile.this.data != null) {
                        final ParseFileController fileController = ParseFile.getFileController();
                        ParseFile parseFile2 = ParseFile.this;
                        final State state = parseFile2.state;
                        final byte[] bArr = parseFile2.data;
                        String str2 = str;
                        final ProgressCallback progressCallback2 = progressCallback;
                        ProgressCallback progressCallback3 = progressCallback2 == null ? null : new ProgressCallback() { // from class: com.parse.ParseFile.2
                            @Override // com.parse.ProgressCallback
                            public void done(final Integer num) {
                                C2742.m4802(new Callable<Void>() { // from class: com.parse.ParseFile.2.1
                                    @Override // java.util.concurrent.Callable
                                    public Void call() {
                                        progressCallback2.done(num);
                                        return null;
                                    }
                                }, ParseExecutors.main());
                            }
                        };
                        C2742<Void> c27425 = c27422;
                        if (fileController == null) {
                            throw null;
                        }
                        if (state.url != null) {
                            c2742M4814 = C2742.m4805(state);
                        } else if (c27425 == null || !c27425.m4818()) {
                            ParseRESTFileCommand.Builder builder = new ParseRESTFileCommand.Builder();
                            builder.httpPath = String.format("files/%s", state.name);
                            builder.data = bArr;
                            builder.contentType = state.contentType;
                            builder.sessionToken = str2;
                            c2742M4814 = new ParseRESTFileCommand(builder).executeAsync(fileController.restClient, progressCallback3, null, c27425).m4814(new InterfaceC2741<JSONObject, State>() { // from class: com.parse.ParseFileController.1
                                @Override // p137.InterfaceC2741
                                public ParseFile.State then(C2742<JSONObject> c27426) throws Throwable {
                                    ParseFileController parseFileController;
                                    JSONObject jSONObjectM4812 = c27426.m4812();
                                    ParseFile.State.Builder builder2 = new ParseFile.State.Builder(state);
                                    builder2.name = jSONObjectM4812.getString("name");
                                    builder2.url = jSONObjectM4812.getString("url");
                                    ParseFile.State stateBuild = builder2.build();
                                    try {
                                        parseFileController = ParseFileController.this;
                                    } catch (IOException unused) {
                                    }
                                    if (parseFileController == null) {
                                        throw null;
                                    }
                                    C4303.writeByteArrayToFile(new File(parseFileController.cachePath, stateBuild.name), bArr);
                                    return stateBuild;
                                }
                            }, ParseExecutors.io());
                        } else {
                            c2742M4814 = C2742.f8460;
                        }
                    } else {
                        final ParseFileController fileController2 = ParseFile.getFileController();
                        ParseFile parseFile3 = ParseFile.this;
                        final State state2 = parseFile3.state;
                        final File file = parseFile3.file;
                        String str3 = str;
                        final ProgressCallback progressCallback4 = progressCallback;
                        ProgressCallback progressCallback5 = progressCallback4 == null ? null : new ProgressCallback() { // from class: com.parse.ParseFile.2
                            @Override // com.parse.ProgressCallback
                            public void done(final Integer num) {
                                C2742.m4802(new Callable<Void>() { // from class: com.parse.ParseFile.2.1
                                    @Override // java.util.concurrent.Callable
                                    public Void call() {
                                        progressCallback4.done(num);
                                        return null;
                                    }
                                }, ParseExecutors.main());
                            }
                        };
                        C2742<Void> c27426 = c27422;
                        if (fileController2 == null) {
                            throw null;
                        }
                        if (state2.url != null) {
                            c2742M4814 = C2742.m4805(state2);
                        } else if (c27426 == null || !c27426.m4818()) {
                            ParseRESTFileCommand.Builder builder2 = new ParseRESTFileCommand.Builder();
                            builder2.httpPath = String.format("files/%s", state2.name);
                            builder2.file = file;
                            builder2.contentType = state2.contentType;
                            builder2.sessionToken = str3;
                            c2742M4814 = new ParseRESTFileCommand(builder2).executeAsync(fileController2.restClient, progressCallback5, null, c27426).m4814(new InterfaceC2741<JSONObject, State>() { // from class: com.parse.ParseFileController.2
                                @Override // p137.InterfaceC2741
                                public ParseFile.State then(C2742<JSONObject> c27427) throws Throwable {
                                    File file2;
                                    ParseFileController parseFileController;
                                    JSONObject jSONObjectM4812 = c27427.m4812();
                                    ParseFile.State.Builder builder3 = new ParseFile.State.Builder(state2);
                                    builder3.name = jSONObjectM4812.getString("name");
                                    builder3.url = jSONObjectM4812.getString("url");
                                    ParseFile.State stateBuild = builder3.build();
                                    try {
                                        file2 = file;
                                        parseFileController = ParseFileController.this;
                                    } catch (IOException unused) {
                                    }
                                    if (parseFileController == null) {
                                        throw null;
                                    }
                                    C4303.copyFile(file2, new File(parseFileController.cachePath, stateBuild.name));
                                    return stateBuild;
                                }
                            }, ParseExecutors.io());
                        } else {
                            c2742M4814 = C2742.f8460;
                        }
                    }
                    return c2742M4814.m4817(new InterfaceC2741<State, C2742<Void>>() { // from class: com.parse.ParseFile.3.1
                        @Override // p137.InterfaceC2741
                        public C2742<Void> then(C2742<State> c27427) {
                            ParseFile.this.state = c27427.m4812();
                            ParseFile parseFile4 = ParseFile.this;
                            parseFile4.data = null;
                            parseFile4.file = null;
                            return c27427.m4821();
                        }
                    }, C2742.f8455);
                }
            }, C2742.f8455, null) : C2742.f8460;
        }
    }

    public static class State {
        public final String contentType;
        public final String name;
        public final String url;

        public static class Builder {
            public String mimeType;
            public String name;
            public String url;

            public Builder() {
            }

            public Builder(State state) {
                this.name = state.name;
                this.mimeType = state.contentType;
                this.url = state.url;
            }

            public State build() {
                return new State(this, null);
            }
        }

        public /* synthetic */ State(Builder builder, AnonymousClass1 anonymousClass1) {
            String str = builder.name;
            this.name = str == null ? "file" : str;
            this.contentType = builder.mimeType;
            this.url = builder.url;
        }
    }

    public ParseFile(Parcel parcel, ParseParcelDecoder parseParcelDecoder) {
        State.Builder builder = new State.Builder();
        builder.url = parcel.readString();
        builder.name = parcel.readString();
        builder.mimeType = parcel.readByte() == 1 ? parcel.readString() : null;
        this(builder.build());
    }

    public ParseFile(State state) {
        this.taskQueue = new TaskQueue();
        this.currentTasks = Collections.synchronizedSet(new HashSet());
        this.state = state;
    }

    public ParseFile(File file) {
        State.Builder builder = new State.Builder();
        builder.name = file.getName();
        builder.mimeType = null;
        this(builder.build());
        this.file = file;
    }

    public ParseFile(JSONObject jSONObject) {
        State.Builder builder = new State.Builder();
        builder.name = jSONObject.optString("name");
        builder.url = jSONObject.optString("url");
        this(builder.build());
    }

    public static ParseFileController getFileController() {
        ParseCorePlugins parseCorePlugins = ParseCorePlugins.INSTANCE;
        if (parseCorePlugins.fileController.get() == null) {
            parseCorePlugins.fileController.compareAndSet(null, new ParseFileController(ParsePlugins.get().restClient(), Parse.getParseCacheDir("files")));
        }
        return parseCorePlugins.fileController.get();
    }

    @Override // android.os.Parcelable
    public int describeContents() {
        return 0;
    }

    public JSONObject encode() throws JSONException {
        JSONObject jSONObject = new JSONObject();
        jSONObject.put("__type", "File");
        jSONObject.put("name", this.state.name);
        String str = this.state.url;
        if (str == null) {
            throw new IllegalStateException("Unable to encode an unsaved ParseFile.");
        }
        jSONObject.put("url", str);
        return jSONObject;
    }

    public boolean isDirty() {
        return this.state.url == null;
    }

    public void save() throws ParseException {
        final C2754<?> c2754 = new C2754<>();
        this.currentTasks.add(c2754);
        final ProgressCallback progressCallback = null;
        C4303.wait(ParseUser.getCurrentUserController().getCurrentSessionTokenAsync().m4817(new InterfaceC2741<String, C2742<Void>>() { // from class: com.parse.ParseFile.5
            @Override // p137.InterfaceC2741
            public C2742<Void> then(C2742<String> c2742) {
                String strM4812 = c2742.m4812();
                ParseFile parseFile = ParseFile.this;
                return parseFile.taskQueue.enqueue(parseFile.new AnonymousClass6(strM4812, progressCallback, c2754.f8503));
            }
        }, C2742.f8455).m4815(new InterfaceC2741<Void, C2742<Void>>() { // from class: com.parse.ParseFile.4
            @Override // p137.InterfaceC2741
            public C2742<Void> then(C2742<Void> c2742) {
                c2754.m4828(null);
                ParseFile.this.currentTasks.remove(c2754);
                return c2742;
            }
        }, C2742.f8455, null));
    }

    public void writeToParcel(Parcel parcel) {
        if (isDirty()) {
            throw new RuntimeException("Unable to parcel an unsaved ParseFile.");
        }
        parcel.writeString(this.state.url);
        parcel.writeString(this.state.name);
        String str = this.state.contentType;
        parcel.writeByte(str != null ? (byte) 1 : (byte) 0);
        if (str != null) {
            parcel.writeString(str);
        }
    }

    @Override // android.os.Parcelable
    public void writeToParcel(Parcel parcel, int i) {
        ParseParcelEncoder parseParcelEncoder = ParseParcelEncoder.INSTANCE;
        writeToParcel(parcel);
    }
}
