package com.google.firebase.remoteconfig.internal;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import com.google.firebase.remoteconfig.FirebaseRemoteConfigClientException;
import com.google.firebase.remoteconfig.FirebaseRemoteConfigException;
import com.google.firebase.remoteconfig.FirebaseRemoteConfigServerException;
import defpackage.AbstractC0562;
import defpackage.AbstractC3481;
import defpackage.C3025;
import defpackage.C5235;
import defpackage.C5266;
import j$.util.DesugarTimeZone;
import j$.util.Objects;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/* JADX INFO: loaded from: classes3.dex */
public class ConfigFetchHttpClient {

    /* JADX INFO: renamed from: ˊᵫᵗ, reason: contains not printable characters */
    public static final Pattern f1129 = Pattern.compile("^[^:]+:([0-9]+):(android|ios|web):([0-9a-f]+)");

    /* JADX INFO: renamed from: ʽˈˋ, reason: contains not printable characters */
    public final String f1130;

    /* JADX INFO: renamed from: ːˏᵟᵯ, reason: contains not printable characters */
    public final String f1131;

    /* JADX INFO: renamed from: ᵨᵣ, reason: contains not printable characters */
    public final String f1132;

    /* JADX INFO: renamed from: ᵲᵶᵳˈ, reason: contains not printable characters */
    public final Context f1133;

    /* JADX INFO: renamed from: ᵵᵽ, reason: contains not printable characters */
    public final long f1134;

    /* JADX INFO: renamed from: ᵷᵺʾᵖ, reason: contains not printable characters */
    public final String f1135;

    /* JADX INFO: renamed from: ᵻˈᵦʼ, reason: contains not printable characters */
    public final long f1136;

    public ConfigFetchHttpClient(Context context, String str, String str2, long j, long j2) {
        this.f1133 = context;
        this.f1130 = str;
        this.f1131 = str2;
        Matcher matcher = f1129.matcher(str);
        this.f1135 = matcher.matches() ? matcher.group(1) : null;
        this.f1132 = "firebase";
        this.f1136 = j;
        this.f1134 = j2;
    }

    /* JADX INFO: renamed from: ːˏᵟᵯ, reason: contains not printable characters */
    public static JSONObject m1001(HttpURLConnection httpURLConnection) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream(), "utf-8"));
        StringBuilder sb = new StringBuilder();
        while (true) {
            int i = bufferedReader.read();
            if (i == -1) {
                return new JSONObject(sb.toString());
            }
            sb.append((char) i);
        }
    }

    /* JADX INFO: renamed from: ᵷᵺʾᵖ, reason: contains not printable characters */
    public static void m1002(HttpURLConnection httpURLConnection, byte[] bArr) throws IOException {
        httpURLConnection.setFixedLengthStreamingMode(bArr.length);
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(httpURLConnection.getOutputStream());
        bufferedOutputStream.write(bArr);
        bufferedOutputStream.flush();
        bufferedOutputStream.close();
    }

    /* JADX WARN: Removed duplicated region for block: B:13:0x0075 A[LOOP:0: B:11:0x006f->B:13:0x0075, LOOP_END] */
    /* JADX WARN: Removed duplicated region for block: B:17:0x00af A[Catch: all -> 0x0161, IOException | JSONException -> 0x0164, IOException -> 0x0167, TRY_LEAVE, TryCatch #1 {all -> 0x0161, blocks: (B:15:0x0093, B:17:0x00af, B:66:0x0169, B:67:0x0172, B:68:0x0173, B:69:0x017a), top: B:81:0x0093 }] */
    /* JADX WARN: Removed duplicated region for block: B:66:0x0169 A[Catch: all -> 0x0161, IOException | JSONException -> 0x0164, IOException -> 0x0167, TRY_ENTER, TryCatch #1 {all -> 0x0161, blocks: (B:15:0x0093, B:17:0x00af, B:66:0x0169, B:67:0x0172, B:68:0x0173, B:69:0x017a), top: B:81:0x0093 }] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public C3025 fetch(HttpURLConnection httpURLConnection, String str, String str2, Map<String, String> map, String str3, Map<String, String> map2, Long l, Date date, Map<String, String> map3) {
        String strM7946;
        int responseCode;
        JSONObject jSONObject;
        JSONArray jSONArray;
        JSONObject jSONObject2;
        JSONArray jSONArray2;
        boolean z;
        byte[] bArr;
        httpURLConnection.setDoOutput(true);
        long j = this.f1136;
        TimeUnit timeUnit = TimeUnit.SECONDS;
        httpURLConnection.setConnectTimeout((int) timeUnit.toMillis(j));
        httpURLConnection.setReadTimeout((int) timeUnit.toMillis(this.f1134));
        httpURLConnection.setRequestProperty("If-None-Match", str3);
        httpURLConnection.setRequestProperty("X-Goog-Api-Key", this.f1131);
        Context context = this.f1133;
        httpURLConnection.setRequestProperty("X-Android-Package", context.getPackageName());
        try {
            bArr = ˀᵳˎᵹ.ᵝᵶ(context, context.getPackageName());
        } catch (PackageManager.NameNotFoundException unused) {
            context.getPackageName();
        }
        if (bArr == null) {
            context.getPackageName();
            strM7946 = null;
            httpURLConnection.setRequestProperty("X-Android-Cert", strM7946);
            httpURLConnection.setRequestProperty("X-Google-GFE-Can-Retry", "yes");
            httpURLConnection.setRequestProperty("X-Goog-Firebase-Installations-Auth", str2);
            httpURLConnection.setRequestProperty("Content-Type", "application/json");
            httpURLConnection.setRequestProperty("Accept", "application/json");
            for (Map.Entry<String, String> entry : map2.entrySet()) {
                httpURLConnection.setRequestProperty(entry.getKey(), entry.getValue());
            }
            try {
                try {
                    m1002(httpURLConnection, m1004(str, str2, map, l, map3).toString().getBytes("utf-8"));
                    httpURLConnection.connect();
                    responseCode = httpURLConnection.getResponseCode();
                    if (responseCode == 200) {
                        throw new FirebaseRemoteConfigServerException(responseCode, httpURLConnection.getResponseMessage());
                    }
                    String headerField = httpURLConnection.getHeaderField("ETag");
                    JSONObject jSONObjectM1001 = m1001(httpURLConnection);
                    httpURLConnection.disconnect();
                    try {
                        httpURLConnection.getInputStream().close();
                    } catch (IOException unused2) {
                    }
                    try {
                        C5266 c5266M10797 = C5235.m10797();
                        c5266M10797.f19903 = date;
                        try {
                            jSONObject = jSONObjectM1001.getJSONObject("entries");
                        } catch (JSONException unused3) {
                            jSONObject = null;
                        }
                        if (jSONObject != null) {
                            try {
                                c5266M10797.f19906 = new JSONObject(jSONObject.toString());
                            } catch (JSONException unused4) {
                            }
                        }
                        try {
                            jSONArray = jSONObjectM1001.getJSONArray("experimentDescriptions");
                        } catch (JSONException unused5) {
                            jSONArray = null;
                        }
                        if (jSONArray != null) {
                            try {
                                c5266M10797.f19904 = new JSONArray(jSONArray.toString());
                            } catch (JSONException unused6) {
                            }
                        }
                        try {
                            jSONObject2 = jSONObjectM1001.getJSONObject("personalizationMetadata");
                        } catch (JSONException unused7) {
                            jSONObject2 = null;
                        }
                        if (jSONObject2 != null) {
                            try {
                                c5266M10797.f19907 = new JSONObject(jSONObject2.toString());
                            } catch (JSONException unused8) {
                            }
                        }
                        String string = jSONObjectM1001.has("templateVersion") ? jSONObjectM1001.getString("templateVersion") : null;
                        if (string != null) {
                            c5266M10797.f19905 = Long.parseLong(string);
                        }
                        try {
                            jSONArray2 = jSONObjectM1001.getJSONArray("rolloutMetadata");
                        } catch (JSONException unused9) {
                            jSONArray2 = null;
                        }
                        if (jSONArray2 != null) {
                            try {
                                c5266M10797.f19908 = new JSONArray(jSONArray2.toString());
                            } catch (JSONException unused10) {
                            }
                        }
                        C5235 c5235M10862 = c5266M10797.m10862();
                        try {
                            z = !jSONObjectM1001.get("state").equals("NO_CHANGE");
                        } catch (JSONException unused11) {
                            z = true;
                        }
                        return !z ? new C3025(1, c5235M10862, null) : new C3025(0, c5235M10862, headerField);
                    } catch (JSONException e) {
                        throw new FirebaseRemoteConfigClientException("Fetch failed: fetch response could not be parsed.", e);
                    }
                } finally {
                }
            } catch (IOException | JSONException e2) {
                throw new FirebaseRemoteConfigClientException("The client had an error while calling the backend!", e2);
            }
        } else {
            strM7946 = AbstractC3481.m7946(bArr);
            httpURLConnection.setRequestProperty("X-Android-Cert", strM7946);
            httpURLConnection.setRequestProperty("X-Google-GFE-Can-Retry", "yes");
            httpURLConnection.setRequestProperty("X-Goog-Firebase-Installations-Auth", str2);
            httpURLConnection.setRequestProperty("Content-Type", "application/json");
            httpURLConnection.setRequestProperty("Accept", "application/json");
            while (r14.hasNext()) {
            }
            m1002(httpURLConnection, m1004(str, str2, map, l, map3).toString().getBytes("utf-8"));
            httpURLConnection.connect();
            responseCode = httpURLConnection.getResponseCode();
            if (responseCode == 200) {
            }
        }
    }

    /* JADX INFO: renamed from: ʽˈˋ, reason: contains not printable characters */
    public final HttpURLConnection m1003() {
        try {
            return (HttpURLConnection) new URL("https://firebaseremoteconfig.googleapis.com/v1/projects/" + this.f1135 + "/namespaces/" + this.f1132 + ":fetch").openConnection();
        } catch (IOException e) {
            throw new FirebaseRemoteConfigException(e.getMessage());
        }
    }

    /* JADX INFO: renamed from: ᵲᵶᵳˈ, reason: contains not printable characters */
    public final JSONObject m1004(String str, String str2, Map map, Long l, Map map2) throws FirebaseRemoteConfigClientException {
        HashMap map3 = new HashMap();
        if (str == null) {
            throw new FirebaseRemoteConfigClientException("Fetch failed: Firebase installation id is null.");
        }
        map3.put("appInstanceId", str);
        map3.put("appInstanceIdToken", str2);
        map3.put("appId", this.f1130);
        Context context = this.f1133;
        Locale locale = context.getResources().getConfiguration().locale;
        map3.put("countryCode", locale.getCountry());
        map3.put("languageCode", locale.toLanguageTag());
        int i = Build.VERSION.SDK_INT;
        map3.put("platformVersion", Integer.toString(i));
        map3.put("timeZone", TimeZone.getDefault().getID());
        try {
            PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
            if (packageInfo != null) {
                map3.put("appVersion", packageInfo.versionName);
                map3.put("appBuild", Long.toString(i >= 28 ? AbstractC0562.m2154(packageInfo) : packageInfo.versionCode));
            }
        } catch (PackageManager.NameNotFoundException unused) {
        }
        map3.put("packageName", context.getPackageName());
        map3.put("sdkVersion", "23.1.0");
        map3.put("analyticsUserProperties", new JSONObject(map));
        if (!map2.isEmpty()) {
            map3.put("customSignals", new JSONObject(map2));
            Objects.toString(map2.keySet());
        }
        if (l != null) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US);
            simpleDateFormat.setTimeZone(DesugarTimeZone.getTimeZone("UTC"));
            map3.put("firstOpenTime", simpleDateFormat.format(l));
        }
        return new JSONObject(map3);
    }
}
