package com.parse;

import android.content.Context;
import android.os.Build;
import com.parse.Parse;
import java.io.File;
import java.util.LinkedHashMap;
import java.util.Map;
import p138.p139.p143.p144.C2803;
import p402.p403.C6560;
import p402.p411.p413.C6635;
import p447.AbstractC6930;
import p447.C6905;
import p447.C6908;
import p447.C6921;
import p447.C6926;
import p447.C6933;
import p447.InterfaceC6912;
import p447.p448.C6943;

/* JADX INFO: loaded from: classes.dex */
public class ParsePlugins {
    public static final Object LOCK = new Object();
    public static ParsePlugins instance;
    public Context applicationContext;
    public File cacheDir;
    public final Parse.Configuration configuration;
    public InstallationId installationId;
    public final Object lock = new Object();
    public File parseDir;
    public ParseHttpClient restClient;

    public ParsePlugins(Context context, Parse.Configuration configuration) {
        if (context != null) {
            this.applicationContext = context.getApplicationContext();
        }
        this.configuration = configuration;
    }

    public static ParsePlugins get() {
        ParsePlugins parsePlugins;
        synchronized (LOCK) {
            parsePlugins = instance;
        }
        return parsePlugins;
    }

    public static void set(ParsePlugins parsePlugins) {
        synchronized (LOCK) {
            if (instance != null) {
                throw new IllegalStateException("ParsePlugins is already initialized");
            }
            instance = parsePlugins;
        }
    }

    public File getCacheDir() {
        File file;
        synchronized (this.lock) {
            if (this.cacheDir == null) {
                this.cacheDir = new File(this.applicationContext.getCacheDir(), "com.parse");
            }
            file = this.cacheDir;
            if (!file.exists()) {
                file.mkdirs();
            }
        }
        return file;
    }

    @Deprecated
    public File getParseDir() {
        File file;
        synchronized (this.lock) {
            if (this.parseDir == null) {
                this.parseDir = this.applicationContext.getDir("Parse", 0);
            }
            file = this.parseDir;
            if (!file.exists()) {
                file.mkdirs();
            }
        }
        return file;
    }

    public InstallationId installationId() {
        InstallationId installationId;
        synchronized (this.lock) {
            if (this.installationId == null) {
                this.installationId = new InstallationId(new File(getParseDir(), "installationId"));
            }
            installationId = this.installationId;
        }
        return installationId;
    }

    public ParseHttpClient restClient() {
        ParseHttpClient parseHttpClient;
        synchronized (this.lock) {
            if (this.restClient == null) {
                C6921.C6922 c6922 = this.configuration.clientBuilder;
                if (c6922 == null) {
                    c6922 = new C6921.C6922();
                }
                c6922.f20090.add(0, new InterfaceC6912() { // from class: com.parse.ParsePlugins.1
                    @Override // p447.InterfaceC6912
                    public C6933 intercept(InterfaceC6912.InterfaceC6913 interfaceC6913) {
                        C6926 c6926Mo10102 = interfaceC6913.mo10102();
                        C6905.C6906 c6906M10069 = c6926Mo10102.f20130.m10069();
                        c6906M10069.m10075("X-Parse-Application-Id", ParsePlugins.this.configuration.applicationId);
                        c6906M10069.m10075("X-Parse-App-Build-Version", String.valueOf(ManifestInfo.getVersionCode()));
                        c6906M10069.m10075("X-Parse-App-Display-Version", ManifestInfo.getVersionName());
                        c6906M10069.m10075("X-Parse-OS-Version", Build.VERSION.RELEASE);
                        if (ParsePlugins.this == null) {
                            throw null;
                        }
                        StringBuilder sbM4889 = C2803.m4889("Parse Android SDK API Level ");
                        sbM4889.append(Build.VERSION.SDK_INT);
                        c6906M10069.m10075("User-Agent", sbM4889.toString());
                        if (c6926Mo10102.m10115("X-Parse-Installation-Id") == null) {
                            c6906M10069.m10075("X-Parse-Installation-Id", ParsePlugins.this.installationId().get());
                        }
                        String str = ParsePlugins.this.configuration.clientKey;
                        if ("xxx" != 0) {
                            c6906M10069.m10075("X-Parse-Client-Key", "xxx");
                        }
                        if (c6926Mo10102 == null) {
                            C6635.m9824("request");
                            throw null;
                        }
                        new LinkedHashMap();
                        C6908 c6908 = c6926Mo10102.f20128;
                        String str2 = c6926Mo10102.f20129;
                        AbstractC6930 abstractC6930 = c6926Mo10102.f20131;
                        Map linkedHashMap = c6926Mo10102.f20132.isEmpty() ? new LinkedHashMap() : C6560.m9783(c6926Mo10102.f20132);
                        c6926Mo10102.f20130.m10069();
                        C6905 c6905M10073 = c6906M10069.m10073();
                        if (c6905M10073 == null) {
                            C6635.m9824("headers");
                            throw null;
                        }
                        C6905.C6906 c6906M100692 = c6905M10073.m10069();
                        if (c6908 != null) {
                            return interfaceC6913.mo10101(new C6926(c6908, str2, c6906M100692.m10073(), abstractC6930, C6943.m10161(linkedHashMap)));
                        }
                        throw new IllegalStateException("url == null".toString());
                    }
                });
                this.restClient = new ParseHttpClient(c6922);
            }
            parseHttpClient = this.restClient;
        }
        return parseHttpClient;
    }
}
