package p138.p176.p291.p323;

import android.text.TextUtils;
import android.util.Log;
import com.google.firebase.installations.FirebaseInstallationsException;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import p000.p001.p002.p003.p004.C0567;
import p138.p176.p177.p242.p270.AbstractC5062;
import p138.p176.p177.p242.p270.C5063;
import p138.p176.p291.C5559;
import p138.p176.p291.p319.InterfaceC5902;
import p138.p176.p291.p323.p324.AbstractC5987;
import p138.p176.p291.p323.p324.C5981;
import p138.p176.p291.p323.p324.C5984;
import p138.p176.p291.p323.p324.C5985;
import p138.p176.p291.p323.p325.AbstractC5995;
import p138.p176.p291.p323.p325.AbstractC5997;
import p138.p176.p291.p323.p325.C5989;
import p138.p176.p291.p323.p325.C5991;
import p138.p176.p291.p323.p325.C5994;
import p138.p176.p291.p327.InterfaceC6006;

/* JADX INFO: renamed from: ދ.ހ.ށ.ފ.ބ, reason: contains not printable characters */
/* JADX INFO: loaded from: classes.dex */
public class C5971 implements InterfaceC5973 {

    /* JADX INFO: renamed from: ވ, reason: contains not printable characters */
    public static final Object f16803 = new Object();

    /* JADX INFO: renamed from: މ, reason: contains not printable characters */
    public static final ThreadFactory f16804 = new ThreadFactoryC5972();

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public final C5559 f16805;

    /* JADX INFO: renamed from: ؠ, reason: contains not printable characters */
    public final C5994 f16806;

    /* JADX INFO: renamed from: ހ, reason: contains not printable characters */
    public final C5985 f16807;

    /* JADX INFO: renamed from: ށ, reason: contains not printable characters */
    public final C5980 f16808;

    /* JADX INFO: renamed from: ނ, reason: contains not printable characters */
    public final C5984 f16809;

    /* JADX INFO: renamed from: ރ, reason: contains not printable characters */
    public final C5978 f16810;

    /* JADX INFO: renamed from: ބ, reason: contains not printable characters */
    public final Object f16811;

    /* JADX INFO: renamed from: ޅ, reason: contains not printable characters */
    public final ExecutorService f16812;

    /* JADX INFO: renamed from: ކ, reason: contains not printable characters */
    public final ExecutorService f16813;

    /* JADX INFO: renamed from: އ, reason: contains not printable characters */
    public final List<InterfaceC5979> f16814;

    /* JADX INFO: renamed from: ދ.ހ.ށ.ފ.ބ$֏, reason: contains not printable characters */
    public class ThreadFactoryC5972 implements ThreadFactory {

        /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
        public final AtomicInteger f16815 = new AtomicInteger(1);

        @Override // java.util.concurrent.ThreadFactory
        public Thread newThread(Runnable runnable) {
            return new Thread(runnable, String.format("firebase-installations-executor-%d", Integer.valueOf(this.f16815.getAndIncrement())));
        }
    }

    public C5971(C5559 c5559, InterfaceC6006 interfaceC6006, InterfaceC5902 interfaceC5902) {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(0, 1, 30L, TimeUnit.SECONDS, new LinkedBlockingQueue(), f16804);
        c5559.m8928();
        C5994 c5994 = new C5994(c5559.f15975, interfaceC6006, interfaceC5902);
        C5985 c5985 = new C5985(c5559);
        C5980 c5980 = new C5980();
        C5984 c5984 = new C5984(c5559);
        C5978 c5978 = new C5978();
        this.f16811 = new Object();
        this.f16814 = new ArrayList();
        this.f16805 = c5559;
        this.f16806 = c5994;
        this.f16807 = c5985;
        this.f16808 = c5980;
        this.f16809 = c5984;
        this.f16810 = c5978;
        this.f16812 = threadPoolExecutor;
        this.f16813 = new ThreadPoolExecutor(0, 1, 30L, TimeUnit.SECONDS, new LinkedBlockingQueue(), f16804);
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public static /* synthetic */ void m9295(C5971 c5971, boolean z) {
        AbstractC5987 abstractC5987M9306;
        AbstractC5987 abstractC5987M9309 = c5971.m9309();
        try {
            if (abstractC5987M9309.m9329() || abstractC5987M9309.m9331()) {
                abstractC5987M9306 = c5971.m9306(abstractC5987M9309);
            } else if (!z && !c5971.f16808.m9316(abstractC5987M9309)) {
                return;
            } else {
                abstractC5987M9306 = c5971.m9300(abstractC5987M9309);
            }
            c5971.f16807.m9327(abstractC5987M9306);
            if (abstractC5987M9306.m9329()) {
                c5971.m9301(abstractC5987M9306, new FirebaseInstallationsException(FirebaseInstallationsException.EnumC0343.BAD_CONFIG));
            } else if (abstractC5987M9306.m9330()) {
                c5971.m9301(abstractC5987M9306, new IOException("cleared fid due to auth error"));
            } else {
                c5971.m9308(abstractC5987M9306);
            }
        } catch (IOException e) {
            c5971.m9301(abstractC5987M9309, e);
        }
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public final AbstractC5062<AbstractC5977> m9298() {
        C5063 c5063 = new C5063();
        C5975 c5975 = new C5975(this.f16808, c5063);
        synchronized (this.f16811) {
            this.f16814.add(c5975);
        }
        return c5063.f15214;
    }

    @Override // p138.p176.p291.p323.InterfaceC5973
    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public AbstractC5062<AbstractC5977> mo9299(boolean z) {
        m9311();
        AbstractC5062<AbstractC5977> abstractC5062M9298 = m9298();
        if (z) {
            this.f16812.execute(new Runnable(this) { // from class: ދ.ހ.ށ.ފ.ށ

                /* JADX INFO: renamed from: ރ, reason: contains not printable characters */
                public final C5971 f16799;

                {
                    this.f16799 = this;
                }

                @Override // java.lang.Runnable
                public void run() {
                    this.f16799.m9304(true);
                }
            });
        } else {
            this.f16812.execute(new Runnable(this) { // from class: ދ.ހ.ށ.ފ.ނ

                /* JADX INFO: renamed from: ރ, reason: contains not printable characters */
                public final C5971 f16800;

                {
                    this.f16800 = this;
                }

                @Override // java.lang.Runnable
                public void run() {
                    this.f16800.m9304(false);
                }
            });
        }
        return abstractC5062M9298;
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public final AbstractC5987 m9300(AbstractC5987 abstractC5987) throws IOException {
        AbstractC5997 abstractC5997M9340;
        C5994 c5994 = this.f16806;
        String strM9305 = m9305();
        C5981 c5981 = (C5981) abstractC5987;
        String str = c5981.f16823;
        String strM9310 = m9310();
        String str2 = c5981.f16826;
        if (c5994 == null) {
            throw null;
        }
        int i = 0;
        URL url = new URL(String.format("https://%s/%s/%s", "firebaseinstallations.googleapis.com", "v1", String.format("projects/%s/installations/%s/authTokens:generate", strM9310, str)));
        while (i <= 1) {
            HttpURLConnection httpURLConnectionM9337 = c5994.m9337(url, strM9305);
            try {
                httpURLConnectionM9337.setRequestMethod("POST");
                httpURLConnectionM9337.addRequestProperty("Authorization", "FIS_v2 " + str2);
                c5994.m9341(httpURLConnectionM9337);
                int responseCode = httpURLConnectionM9337.getResponseCode();
                if (responseCode == 200) {
                    abstractC5997M9340 = c5994.m9340(httpURLConnectionM9337);
                } else if (responseCode == 401 || responseCode == 404) {
                    C5991.C5993 c5993 = (C5991.C5993) AbstractC5997.m9342();
                    c5993.f16858 = AbstractC5997.EnumC5999.AUTH_ERROR;
                    abstractC5997M9340 = c5993.mo9333();
                } else {
                    if (responseCode != 429 && (responseCode < 500 || responseCode >= 600)) {
                        C5991.C5993 c59932 = (C5991.C5993) AbstractC5997.m9342();
                        c59932.f16858 = AbstractC5997.EnumC5999.BAD_CONFIG;
                        abstractC5997M9340 = c59932.mo9333();
                    }
                    i++;
                }
                httpURLConnectionM9337.disconnect();
                C5991 c5991 = (C5991) abstractC5997M9340;
                int iOrdinal = c5991.f16855.ordinal();
                if (iOrdinal == 0) {
                    String str3 = c5991.f16853;
                    long j = c5991.f16854;
                    long jM9315 = this.f16808.m9315();
                    C5981.C5983 c5983 = (C5981.C5983) abstractC5987.mo9317();
                    c5983.f16832 = str3;
                    c5983.f16834 = Long.valueOf(j);
                    c5983.f16835 = Long.valueOf(jM9315);
                    return c5983.mo9320();
                }
                if (iOrdinal == 1) {
                    C5981.C5983 c59832 = (C5981.C5983) abstractC5987.mo9317();
                    c59832.f16836 = "BAD CONFIG";
                    c59832.mo9319(C5985.EnumC5986.REGISTER_ERROR);
                    return c59832.mo9320();
                }
                if (iOrdinal != 2) {
                    throw new IOException();
                }
                AbstractC5987.AbstractC5988 abstractC5988Mo9317 = abstractC5987.mo9317();
                abstractC5988Mo9317.mo9319(C5985.EnumC5986.NOT_GENERATED);
                return abstractC5988Mo9317.mo9320();
            } finally {
                httpURLConnectionM9337.disconnect();
            }
        }
        throw new IOException();
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public final void m9301(AbstractC5987 abstractC5987, Exception exc) {
        synchronized (this.f16811) {
            Iterator<InterfaceC5979> it = this.f16814.iterator();
            while (it.hasNext()) {
                if (it.next().mo9313(abstractC5987, exc)) {
                    it.remove();
                }
            }
        }
    }

    /* JADX INFO: renamed from: ؠ, reason: contains not printable characters */
    public final String m9302(AbstractC5987 abstractC5987) {
        C5559 c5559 = this.f16805;
        c5559.m8928();
        if (c5559.f15976.equals("CHIME_ANDROID_SDK") || this.f16805.m8932()) {
            if (abstractC5987 == null) {
                throw null;
            }
            if (((C5981) abstractC5987).f16824 == C5985.EnumC5986.ATTEMPT_MIGRATION) {
                String strM9322 = this.f16809.m9322();
                return TextUtils.isEmpty(strM9322) ? this.f16810.m9314() : strM9322;
            }
        }
        return this.f16810.m9314();
    }

    /* JADX INFO: renamed from: ؠ, reason: contains not printable characters */
    public final AbstractC5062<String> m9303() {
        C5063 c5063 = new C5063();
        C5976 c5976 = new C5976(c5063);
        synchronized (this.f16811) {
            this.f16814.add(c5976);
        }
        return c5063.f15214;
    }

    /* JADX INFO: renamed from: ؠ, reason: contains not printable characters */
    public final void m9304(final boolean z) {
        AbstractC5987 abstractC5987M9309 = m9309();
        if (z) {
            C5981.C5983 c5983 = (C5981.C5983) abstractC5987M9309.mo9317();
            c5983.f16832 = null;
            abstractC5987M9309 = c5983.mo9320();
        }
        m9308(abstractC5987M9309);
        this.f16813.execute(new Runnable(this, z) { // from class: ދ.ހ.ށ.ފ.ރ

            /* JADX INFO: renamed from: ރ, reason: contains not printable characters */
            public final C5971 f16801;

            /* JADX INFO: renamed from: ބ, reason: contains not printable characters */
            public final boolean f16802;

            {
                this.f16801 = this;
                this.f16802 = z;
            }

            @Override // java.lang.Runnable
            public void run() {
                C5971.m9295(this.f16801, this.f16802);
            }
        });
    }

    /* JADX INFO: renamed from: ހ, reason: contains not printable characters */
    public String m9305() {
        C5559 c5559 = this.f16805;
        c5559.m8928();
        return c5559.f15977.f15987;
    }

    /* JADX INFO: renamed from: ހ, reason: contains not printable characters */
    public final AbstractC5987 m9306(AbstractC5987 abstractC5987) throws IOException {
        AbstractC5995 abstractC5995M9338;
        C5981 c5981 = (C5981) abstractC5987;
        String strM9325 = c5981.f16823.length() == 11 ? this.f16809.m9325() : null;
        C5994 c5994 = this.f16806;
        String strM9305 = m9305();
        String str = c5981.f16823;
        String strM9310 = m9310();
        C5559 c5559 = this.f16805;
        c5559.m8928();
        String str2 = c5559.f15977.f15988;
        if (c5994 == null) {
            throw null;
        }
        int i = 0;
        URL url = new URL(String.format("https://%s/%s/%s", "firebaseinstallations.googleapis.com", "v1", String.format("projects/%s/installations", strM9310)));
        while (i <= 1) {
            HttpURLConnection httpURLConnectionM9337 = c5994.m9337(url, strM9305);
            try {
                httpURLConnectionM9337.setRequestMethod("POST");
                httpURLConnectionM9337.setDoOutput(true);
                if (strM9325 != null) {
                    httpURLConnectionM9337.addRequestProperty("x-goog-fis-android-iid-migration-auth", strM9325);
                }
                c5994.m9339(httpURLConnectionM9337, str, str2);
                int responseCode = httpURLConnectionM9337.getResponseCode();
                if (responseCode == 200) {
                    abstractC5995M9338 = c5994.m9338(httpURLConnectionM9337);
                } else {
                    if (responseCode != 429 && (responseCode < 500 || responseCode >= 600)) {
                        abstractC5995M9338 = new C5989(null, null, null, null, AbstractC5995.EnumC5996.BAD_CONFIG, null);
                    }
                    i++;
                }
                httpURLConnectionM9337.disconnect();
                C5989 c5989 = (C5989) abstractC5995M9338;
                int iOrdinal = c5989.f16852.ordinal();
                if (iOrdinal != 0) {
                    if (iOrdinal != 1) {
                        throw new IOException();
                    }
                    C5981.C5983 c5983 = (C5981.C5983) abstractC5987.mo9317();
                    c5983.f16836 = "BAD CONFIG";
                    c5983.mo9319(C5985.EnumC5986.REGISTER_ERROR);
                    return c5983.mo9320();
                }
                String str3 = c5989.f16849;
                String str4 = c5989.f16850;
                long jM9315 = this.f16808.m9315();
                C5991 c5991 = (C5991) c5989.f16851;
                String str5 = c5991.f16853;
                long j = c5991.f16854;
                C5981.C5983 c59832 = (C5981.C5983) abstractC5987.mo9317();
                c59832.f16830 = str3;
                c59832.mo9319(C5985.EnumC5986.REGISTERED);
                c59832.f16832 = str5;
                c59832.f16833 = str4;
                c59832.f16834 = Long.valueOf(j);
                c59832.f16835 = Long.valueOf(jM9315);
                return c59832.mo9320();
            } finally {
                httpURLConnectionM9337.disconnect();
            }
        }
        throw new IOException();
    }

    @Override // p138.p176.p291.p323.InterfaceC5973
    /* JADX INFO: renamed from: ށ, reason: contains not printable characters */
    public AbstractC5062<String> mo9307() {
        m9311();
        AbstractC5062<String> abstractC5062M9303 = m9303();
        this.f16812.execute(new Runnable(this) { // from class: ދ.ހ.ށ.ފ.ހ

            /* JADX INFO: renamed from: ރ, reason: contains not printable characters */
            public final C5971 f16798;

            {
                this.f16798 = this;
            }

            @Override // java.lang.Runnable
            public void run() {
                this.f16798.m9304(false);
            }
        });
        return abstractC5062M9303;
    }

    /* JADX INFO: renamed from: ށ, reason: contains not printable characters */
    public final void m9308(AbstractC5987 abstractC5987) {
        synchronized (this.f16811) {
            Iterator<InterfaceC5979> it = this.f16814.iterator();
            while (it.hasNext()) {
                if (it.next().mo9312(abstractC5987)) {
                    it.remove();
                }
            }
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:26:0x0038 A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /* JADX INFO: renamed from: ނ, reason: contains not printable characters */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final AbstractC5987 m9309() {
        AbstractC5987 abstractC5987M9326;
        synchronized (f16803) {
            C5559 c5559 = this.f16805;
            c5559.m8928();
            C5966 c5966M9293 = C5966.m9293(c5559.f15975, "generatefid.lock");
            try {
                abstractC5987M9326 = this.f16807.m9326();
                if (abstractC5987M9326.m9330()) {
                    String strM9302 = m9302(abstractC5987M9326);
                    C5985 c5985 = this.f16807;
                    C5981.C5983 c5983 = (C5981.C5983) abstractC5987M9326.mo9317();
                    c5983.f16830 = strM9302;
                    c5983.mo9319(C5985.EnumC5986.UNREGISTERED);
                    abstractC5987M9326 = c5983.mo9320();
                    c5985.m9327(abstractC5987M9326);
                }
            } finally {
                if (c5966M9293 != null) {
                    try {
                        c5966M9293.f16797.release();
                        c5966M9293.f16796.close();
                    } catch (IOException e) {
                        Log.e("CrossProcessLock", "encountered error while releasing, ignoring", e);
                    }
                }
            }
        }
        return abstractC5987M9326;
    }

    /* JADX INFO: renamed from: ރ, reason: contains not printable characters */
    public String m9310() {
        C5559 c5559 = this.f16805;
        c5559.m8928();
        if (TextUtils.isEmpty(c5559.f15977.f15993)) {
            C5559 c55592 = this.f16805;
            c55592.m8928();
            return c55592.f15977.f15991;
        }
        C5559 c55593 = this.f16805;
        c55593.m8928();
        return c55593.f15977.f15993;
    }

    /* JADX INFO: renamed from: ބ, reason: contains not printable characters */
    public final void m9311() {
        C5559 c5559 = this.f16805;
        c5559.m8928();
        C0567.m1926(c5559.f15977.f15988);
        C0567.m1926(m9310());
        C0567.m1926(m9305());
    }
}
