package M4;

import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.util.Log;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Random;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.json.JSONObject;

/* JADX INFO: loaded from: classes.dex */
public final class m {

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    public static final int[] f4262s = {2, 4, 8, 16, 32, 64, 128, 256};

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    public static final Pattern f4263t = Pattern.compile("^[^:]+:([0-9]+):(android|ios|web):([0-9a-f]+)");

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final LinkedHashSet f4264a;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public int f4266c;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public HttpURLConnection f4269f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public L1.b f4270g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public final ScheduledExecutorService f4271h;
    public final i i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public final W3.f f4272j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public final E4.e f4273k;

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    public final d f4274l;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public final Context f4275m;

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    public final o f4279q;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public boolean f4265b = false;

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    public final Random f4277o = new Random();

    /* JADX INFO: renamed from: p, reason: collision with root package name */
    public final T2.a f4278p = T2.a.f6551a;

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    public final String f4276n = "firebase";

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public boolean f4267d = false;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public boolean f4268e = false;

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    public final Object f4280r = new Object();

    public m(W3.f fVar, E4.e eVar, i iVar, d dVar, Context context, LinkedHashSet linkedHashSet, o oVar, ScheduledExecutorService scheduledExecutorService) {
        this.f4264a = linkedHashSet;
        this.f4271h = scheduledExecutorService;
        this.f4266c = Math.max(8 - oVar.c().f4281a, 1);
        this.f4272j = fVar;
        this.i = iVar;
        this.f4273k = eVar;
        this.f4274l = dVar;
        this.f4275m = context;
        this.f4279q = oVar;
    }

    public static boolean d(int i) {
        return i == 408 || i == 429 || i == 502 || i == 503 || i == 504;
    }

    public static String f(InputStream inputStream) {
        StringBuilder sb = new StringBuilder();
        try {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            while (true) {
                String line = bufferedReader.readLine();
                if (line == null) {
                    break;
                }
                sb.append(line);
            }
        } catch (IOException unused) {
            if (sb.length() == 0) {
                return "Unable to connect to the server, access is forbidden. HTTP status code: 403";
            }
        }
        return sb.toString();
    }

    /* JADX WARN: Removed duplicated region for block: B:14:0x0019  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final synchronized boolean a() {
        boolean z8;
        if (this.f4264a.isEmpty() || this.f4265b || this.f4267d) {
            z8 = false;
        } else if (!this.f4268e) {
            z8 = true;
        }
        return z8;
    }

    public final void b(InputStream inputStream, InputStream inputStream2) {
        HttpURLConnection httpURLConnection = this.f4269f;
        if (httpURLConnection != null && !this.f4268e) {
            httpURLConnection.disconnect();
        }
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e8) {
                Log.d("FirebaseRemoteConfig", "Error closing connection stream.", e8);
            }
        }
        if (inputStream2 != null) {
            try {
                inputStream2.close();
            } catch (IOException e9) {
                Log.d("FirebaseRemoteConfig", "Error closing connection stream.", e9);
            }
        }
    }

    public final String c(String str) {
        W3.f fVar = this.f4272j;
        fVar.a();
        Matcher matcher = f4263t.matcher(fVar.f7115c.f7128b);
        return "https://firebaseremoteconfigrealtime.googleapis.com/v1/projects/" + (matcher.matches() ? matcher.group(1) : null) + "/namespaces/" + str + ":streamFetchInvalidations";
    }

    public final synchronized void e(long j8) {
        try {
            if (a()) {
                int i = this.f4266c;
                if (i > 0) {
                    this.f4266c = i - 1;
                    this.f4271h.schedule(new k(0, this), j8, TimeUnit.MILLISECONDS);
                } else if (!this.f4268e) {
                    new L4.d("Unable to connect to the server. Check your connection and try again.");
                    g();
                }
            }
        } catch (Throwable th) {
            throw th;
        }
    }

    public final synchronized void g() {
        Iterator it = this.f4264a.iterator();
        while (it.hasNext()) {
            ((l) it.next()).a();
        }
    }

    public final synchronized void h() {
        this.f4266c = 8;
    }

    public final synchronized void i() {
        this.f4278p.getClass();
        e(Math.max(0L, this.f4279q.c().f4282b.getTime() - new Date(System.currentTimeMillis()).getTime()));
    }

    public final synchronized void j(boolean z8) {
        this.f4265b = z8;
    }

    public final void k(boolean z8) {
        HttpURLConnection httpURLConnection;
        synchronized (this.f4280r) {
            try {
                this.f4268e = z8;
                L1.b bVar = this.f4270g;
                if (bVar != null) {
                    bVar.f3938a = z8;
                }
                if (Build.VERSION.SDK_INT >= 26 && z8 && (httpURLConnection = this.f4269f) != null) {
                    httpURLConnection.disconnect();
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:12:0x0097  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void l(HttpURLConnection httpURLConnection, String str, String str2) throws IOException {
        String strA;
        byte[] bArrC;
        httpURLConnection.setRequestMethod("POST");
        httpURLConnection.setRequestProperty("X-Goog-Firebase-Installations-Auth", str2);
        W3.f fVar = this.f4272j;
        fVar.a();
        W3.h hVar = fVar.f7115c;
        httpURLConnection.setRequestProperty("X-Goog-Api-Key", hVar.f7127a);
        Context context = this.f4275m;
        httpURLConnection.setRequestProperty("X-Android-Package", context.getPackageName());
        try {
            bArrC = T2.b.c(context, context.getPackageName());
        } catch (PackageManager.NameNotFoundException unused) {
            Log.i("FirebaseRemoteConfig", "No such package: " + context.getPackageName());
        }
        if (bArrC != null) {
            strA = T2.b.a(bArrC);
            httpURLConnection.setRequestProperty("X-Android-Cert", strA);
            httpURLConnection.setRequestProperty("X-Google-GFE-Can-Retry", "yes");
            httpURLConnection.setRequestProperty("X-Accept-Response-Streaming", "true");
            httpURLConnection.setRequestProperty("Content-Type", "application/json");
            httpURLConnection.setRequestProperty("Accept", "application/json");
            HashMap map = new HashMap();
            fVar.a();
            Matcher matcher = f4263t.matcher(hVar.f7128b);
            map.put("project", matcher.matches() ? matcher.group(1) : null);
            map.put("namespace", this.f4276n);
            map.put("lastKnownVersionNumber", Long.toString(this.i.f4253g.f4285a.getLong("last_template_version", 0L)));
            fVar.a();
            map.put("appId", hVar.f7128b);
            map.put("sdkVersion", "22.1.2");
            map.put("appInstanceId", str);
            byte[] bytes = new JSONObject(map).toString().getBytes("utf-8");
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(httpURLConnection.getOutputStream());
            bufferedOutputStream.write(bytes);
            bufferedOutputStream.flush();
            bufferedOutputStream.close();
        }
        Log.e("FirebaseRemoteConfig", "Could not get fingerprint hash for package: " + context.getPackageName());
        strA = null;
        httpURLConnection.setRequestProperty("X-Android-Cert", strA);
        httpURLConnection.setRequestProperty("X-Google-GFE-Can-Retry", "yes");
        httpURLConnection.setRequestProperty("X-Accept-Response-Streaming", "true");
        httpURLConnection.setRequestProperty("Content-Type", "application/json");
        httpURLConnection.setRequestProperty("Accept", "application/json");
        HashMap map2 = new HashMap();
        fVar.a();
        Matcher matcher2 = f4263t.matcher(hVar.f7128b);
        map2.put("project", matcher2.matches() ? matcher2.group(1) : null);
        map2.put("namespace", this.f4276n);
        map2.put("lastKnownVersionNumber", Long.toString(this.i.f4253g.f4285a.getLong("last_template_version", 0L)));
        fVar.a();
        map2.put("appId", hVar.f7128b);
        map2.put("sdkVersion", "22.1.2");
        map2.put("appInstanceId", str);
        byte[] bytes2 = new JSONObject(map2).toString().getBytes("utf-8");
        BufferedOutputStream bufferedOutputStream2 = new BufferedOutputStream(httpURLConnection.getOutputStream());
        bufferedOutputStream2.write(bytes2);
        bufferedOutputStream2.flush();
        bufferedOutputStream2.close();
    }

    public final synchronized L1.b m(HttpURLConnection httpURLConnection) {
        return new L1.b(httpURLConnection, this.i, this.f4274l, this.f4264a, new l(this), this.f4271h);
    }

    public final void n(Date date) {
        o oVar = this.f4279q;
        int i = oVar.c().f4281a + 1;
        long millis = TimeUnit.MINUTES.toMillis(f4262s[(i < 8 ? i : 8) - 1]);
        oVar.f(i, new Date(date.getTime() + (millis / 2) + ((long) this.f4277o.nextInt((int) millis))));
    }
}
