package xyz.stream.api;

import android.os.Handler;
import android.os.Looper;
import clientgosdk.Clientgosdk;
import java.io.File;
import java.util.HashMap;
import java.util.UUID;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicReference;
import xyz.stream.api.entity.CheckUpdateEntity;
import xyz.stream.utils.entity.InstallEntity;
import xyz.stream.utils.f0;

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

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public static volatile z f36035d;

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public int f36036a = 0;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final Handler f36037b = new Handler(Looper.getMainLooper());

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

    public static z b() {
        if (f36035d == null) {
            synchronized (z.class) {
                try {
                    if (f36035d == null) {
                        f36035d = new z();
                    }
                } finally {
                }
            }
        }
        return f36035d;
    }

    public final CheckUpdateEntity a(boolean z10) {
        try {
            CheckUpdateEntity checkUpdateEntityC = c(z10);
            this.f36038c = checkUpdateEntityC;
            return checkUpdateEntityC;
        } catch (Exception e10) {
            u.a.g(e10, new StringBuilder("checkVersionAppUpdate failed: "));
            return null;
        }
    }

    public final CheckUpdateEntity c(boolean z10) {
        CountDownLatch countDownLatch = new CountDownLatch(1);
        AtomicReference atomicReference = new AtomicReference();
        o oVarF = o.f();
        c cVar = new c(this, atomicReference, countDownLatch, 2);
        oVarF.getClass();
        HashMap map = new HashMap();
        map.put("channel", xyz.stream.utils.e.d().f36123l);
        map.put("version", xyz.stream.utils.e.d().f36113b);
        map.put("refresh", String.valueOf(z10));
        o.g("app/latest", oVarF.n(), map, new m(oVarF, map, cVar));
        try {
            countDownLatch.await();
            return (CheckUpdateEntity) atomicReference.get();
        } catch (InterruptedException e10) {
            xyz.stream.utils.h.c(e10.getMessage());
            return null;
        }
    }

    public final boolean d(g gVar) {
        try {
            CheckUpdateEntity checkUpdateEntity = this.f36038c;
            if (checkUpdateEntity != null && checkUpdateEntity.getFile() != null) {
                InstallEntity installEntity = (InstallEntity) f0.o().k(InstallEntity.class, "sp_install_entity");
                if (installEntity != null) {
                    String versionName = installEntity.getVersionName();
                    String sourcePath = installEntity.getSourcePath();
                    String fileUrl = installEntity.getFileUrl();
                    if (this.f36038c.getVersion().equals(versionName) && new File(sourcePath).exists() && fileUrl.equals(this.f36038c.getFile())) {
                        gVar.a(sourcePath);
                        return true;
                    }
                }
                this.f36036a = 0;
                String string = UUID.randomUUID().toString();
                q qVarG = q.g();
                String file = this.f36038c.getFile();
                qVarG.getClass();
                String downloadUrl = Clientgosdk.getDownloadUrl(string, file);
                File file2 = new File(xyz.stream.utils.e.d().f36112a.getFilesDir().getAbsolutePath() + "/local");
                if (!file2.exists() && !file2.mkdirs()) {
                    gVar.b("updateVersionApp create local dir failed");
                    return false;
                }
                String str = file2.getAbsolutePath() + File.separator;
                String str2 = xyz.stream.utils.e.d().f36123l + "_" + this.f36038c.getVersion() + ".apk";
                v6.j jVar = new v6.j();
                jVar.f33702c = this;
                jVar.f33701b = downloadUrl;
                jVar.f33700a = str;
                jVar.f33704e = str2;
                jVar.f33703d = gVar;
                xyz.stream.utils.z.d(downloadUrl, str2, str, jVar);
                return true;
            }
            return false;
        } catch (Exception e10) {
            xyz.stream.utils.h.b("updateVersionApp failed: " + e10.getMessage());
            gVar.b("updateVersionApp failed: " + e10.getMessage());
            return false;
        }
    }
}
