package xyz.stream.api;

import android.net.Uri;
import com.bytedance.sdk.openadsdk.TTAdConstant;
import com.google.gson.Gson;
import com.google.gson.stream.JsonReader;
import java.io.File;
import java.io.StringReader;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicReference;

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

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

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public File f35767a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public File f35768b;

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

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public Map f35770d;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public Uri f35773g;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public Class f35775i;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final ConcurrentHashMap f35771e = new ConcurrentHashMap();

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final ConcurrentHashMap f35772f = new ConcurrentHashMap();

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public String f35774h = "";

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public final ExecutorService f35776j = Executors.newCachedThreadPool();

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

    public static d b() {
        if (f35766l == null) {
            f35766l = new d();
        }
        return f35766l;
    }

    public final void a() {
        if (f35766l == null) {
            return;
        }
        this.f35769c = null;
        this.f35770d = null;
        this.f35771e.clear();
        this.f35772f.clear();
    }

    public final List c(int i10, int i11, int i12) {
        CountDownLatch countDownLatch = new CountDownLatch(1);
        AtomicReference atomicReference = new AtomicReference();
        o oVarF = o.f();
        c cVar = new c(this, atomicReference, countDownLatch, 0);
        oVarF.getClass();
        HashMap map = new HashMap();
        map.put("tab", xyz.stream.utils.h.d(i10));
        map.put("page", String.valueOf(i11));
        map.put("page_size", String.valueOf(i12));
        o.g("collection/list", oVarF.n(), map, new j(oVarF, "collection/list", map, cVar, 20));
        try {
            countDownLatch.await();
            return (List) atomicReference.get();
        } catch (InterruptedException e10) {
            xyz.stream.utils.h.c("getCollectionFromApi interrupted: " + e10.getMessage());
            return null;
        }
    }

    public final ArrayList d(final int i10, final int i11, final int i12) {
        if (this.f35770d == null) {
            f();
        }
        String strD = xyz.stream.utils.h.d(i10);
        Map map = this.f35770d;
        if (map == null || !map.containsKey(strD)) {
            final List listC = c(i10, i11, i12);
            this.f35776j.execute(new Runnable() { // from class: xyz.stream.api.a
                @Override // java.lang.Runnable
                public final void run() {
                    this.f35751a.i(listC, i10, i11, i12);
                }
            });
            if (listC == null || listC.isEmpty()) {
                return null;
            }
            return t4.f.x(listC).e(new xyz.netfly.viewmodel.a(4)).E();
        }
        synchronized (this.f35777k) {
            try {
                List list = (List) this.f35770d.get(strD);
                if (list != null && !list.isEmpty()) {
                    int i13 = (i11 - 1) * i12;
                    if (i13 >= list.size()) {
                        return null;
                    }
                    ArrayList arrayList = new ArrayList(list.subList(i13, Math.min(i12 + i13, list.size())));
                    if (arrayList.isEmpty()) {
                        return null;
                    }
                    return t4.f.x(arrayList).e(new xyz.netfly.viewmodel.a(3)).E();
                }
                return null;
            } finally {
            }
        }
    }

    public final ArrayList e(int i10) {
        List list;
        if (this.f35769c == null) {
            g();
        }
        Map map = this.f35769c;
        if (map == null || !map.containsKey(String.valueOf(i10)) || (list = (List) this.f35769c.get(String.valueOf(i10))) == null || list.isEmpty()) {
            return null;
        }
        return t4.f.x(list).e(new xyz.netfly.viewmodel.a(5)).E();
    }

    public final void f() {
        try {
            if (this.f35768b.exists()) {
                int[] iArr = xyz.stream.utils.h.f36141b;
                for (int i10 = 0; i10 < 5; i10++) {
                    int i11 = iArr[i10];
                    this.f35772f.put(i11 + "_1_3", Long.valueOf(this.f35768b.lastModified()));
                }
            } else {
                kotlinx.coroutines.flow.h.c(xyz.stream.utils.e.d().f36112a, "cms_collections.json", this.f35768b.getAbsolutePath());
            }
            String strK = kotlinx.coroutines.flow.h.K(this.f35768b);
            if (strK.isEmpty()) {
                kotlinx.coroutines.flow.h.c(xyz.stream.utils.e.d().f36112a, "cms_collections.json", this.f35768b.getAbsolutePath());
            }
            if (strK.isEmpty() || strK.equals("null") || strK.equals("{}")) {
                return;
            }
            this.f35770d = (Map) xyz.stream.utils.m.f36162a.fromJson(strK, new ApiDataCacheHelper$2().getType());
        } catch (Exception e10) {
            u.a.g(e10, new StringBuilder("initCollectionData error: "));
        }
    }

    public final void g() {
        String strTrim = "";
        try {
            if (this.f35767a.exists()) {
                int[] iArr = xyz.stream.utils.h.f36140a;
                for (int i10 = 0; i10 < 3; i10++) {
                    this.f35771e.put(Integer.valueOf(iArr[i10]), Long.valueOf(this.f35767a.lastModified()));
                }
            } else {
                kotlinx.coroutines.flow.h.c(xyz.stream.utils.e.d().f36112a, "cms_top.json", this.f35767a.getAbsolutePath());
            }
            String strK = kotlinx.coroutines.flow.h.K(this.f35767a);
            if (strK.isEmpty()) {
                kotlinx.coroutines.flow.h.c(xyz.stream.utils.e.d().f36112a, "cms_top.json", this.f35767a.getAbsolutePath());
            }
            strTrim = strK.trim();
            if (strTrim.isEmpty() || strTrim.equals("null") || strTrim.equals("{}")) {
                return;
            }
            Type type = new ApiDataCacheHelper$1().getType();
            try {
                try {
                    this.f35769c = (Map) xyz.stream.utils.m.f36162a.fromJson(strTrim, type);
                } catch (Exception e10) {
                    xyz.stream.utils.h.c("initTopData parse error lenient: " + e10.getMessage());
                }
            } catch (Exception unused) {
                this.f35769c = (Map) new Gson().fromJson(new JsonReader(new StringReader(strTrim)), type);
            }
            List list = (List) this.f35769c.get(String.valueOf(2));
            List list2 = (List) this.f35769c.get(String.valueOf(3));
            if (!xyz.stream.utils.e.d().f36118g || list == null || list.isEmpty() || list2 == null || list2.isEmpty()) {
                return;
            }
            ArrayList arrayList = new ArrayList();
            arrayList.addAll(list);
            arrayList.addAll(list2);
            if (arrayList.isEmpty()) {
                return;
            }
            this.f35776j.execute(new ve.b(this, arrayList, 15));
        } catch (Exception e11) {
            xyz.stream.utils.h.c("initTopData error: " + e11.getMessage() + ", cmsTop: " + strTrim);
            File file = this.f35767a;
            if (file == null || !file.exists()) {
                return;
            }
            this.f35767a.delete();
        }
    }

    public final void h(int i10, int i11, int i12) {
        long jCurrentTimeMillis = System.currentTimeMillis();
        String str = i10 + "_" + i11 + "_" + i12;
        ConcurrentHashMap concurrentHashMap = this.f35772f;
        if (concurrentHashMap.containsKey(str)) {
            Long l10 = (Long) concurrentHashMap.get(str);
            if (l10 == null) {
                l10 = 0L;
            }
            if (jCurrentTimeMillis - l10.longValue() < TTAdConstant.AD_MAX_EVENT_TIME) {
                return;
            }
        }
        concurrentHashMap.put(str, Long.valueOf(jCurrentTimeMillis));
        this.f35776j.execute(new xyz.netfly.mobile.player.f(this, i10, i11, i12, 1));
    }

    public final void i(List list, int i10, int i11, int i12) {
        if (list == null || list.isEmpty() || this.f35770d == null) {
            return;
        }
        String strD = xyz.stream.utils.h.d(i10);
        synchronized (this.f35777k) {
            try {
                List arrayList = (List) this.f35770d.get(strD);
                if (arrayList == null) {
                    arrayList = new ArrayList(list);
                } else {
                    int i13 = (i11 - 1) * i12;
                    if (i13 >= arrayList.size()) {
                        arrayList.addAll(list);
                    } else {
                        arrayList.subList(i13, Math.min(i12 + i13, arrayList.size())).clear();
                        arrayList.addAll(i13, list);
                    }
                }
                this.f35770d.put(strD, arrayList);
                kotlinx.coroutines.flow.h.R(this.f35768b, xyz.stream.utils.m.a(this.f35770d));
            } finally {
            }
        }
    }

    public final void j(int i10) {
        long jCurrentTimeMillis = System.currentTimeMillis();
        ConcurrentHashMap concurrentHashMap = this.f35771e;
        if (concurrentHashMap.containsKey(Integer.valueOf(i10))) {
            Long l10 = (Long) concurrentHashMap.get(Integer.valueOf(i10));
            if (l10 == null) {
                l10 = 0L;
            }
            if (jCurrentTimeMillis - l10.longValue() < TTAdConstant.AD_MAX_EVENT_TIME) {
                return;
            }
        }
        concurrentHashMap.put(Integer.valueOf(i10), Long.valueOf(jCurrentTimeMillis));
        ExecutorService executorService = this.f35776j;
        if (i10 == 5) {
            executorService.execute(new androidx.activity.n(this, Arrays.asList("TV-Y", "TV-7", "TV-G", "G", "PG"), i10, 18));
        } else {
            executorService.execute(new i0.o(i10, 14, this));
        }
    }
}
