package xyz.stream.api.live;

import android.os.Looper;
import fa.h;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicReference;
import xyz.stream.api.database.l;
import xyz.stream.api.entity.LiveCategoryEntity;
import xyz.stream.api.entity.LiveChannelEntity;
import xyz.stream.api.j;
import xyz.stream.api.o;
import xyz.stream.utils.f0;
import ze.i;

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

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public static volatile h f35897g;

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public volatile ArrayList f35898a = new ArrayList();

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final ExecutorService f35899b = Executors.newFixedThreadPool(3);

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final ArrayList f35900c = new ArrayList();

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

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

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public ArrayList f35901d = f0.o().m(Integer.class, "sp_selected_live_category");

    public static void a(LiveCategoryEntity liveCategoryEntity, LiveChannelEntity liveChannelEntity) {
        if (liveChannelEntity == null) {
            return;
        }
        List<LiveChannelEntity> channels = liveCategoryEntity.getChannels();
        if (channels == null) {
            channels = new ArrayList<>();
            liveCategoryEntity.setChannels(channels);
        }
        Integer id2 = liveChannelEntity.getId();
        if (id2 == null || !t4.f.x(channels).p(new androidx.core.app.e(id2, 0), 0)) {
            channels.add(liveChannelEntity);
            if (liveCategoryEntity.getProgramTotal() == null || liveCategoryEntity.getProgramTotal().intValue() < channels.size()) {
                liveCategoryEntity.setProgramTotal(Integer.valueOf(channels.size()));
            }
        }
    }

    public static LinkedHashMap b(List list) {
        LinkedHashMap linkedHashMap = new LinkedHashMap();
        if (list.isEmpty()) {
            return linkedHashMap;
        }
        Iterator it = list.iterator();
        while (it.hasNext()) {
            LiveCategoryEntity liveCategoryEntity = (LiveCategoryEntity) it.next();
            if (liveCategoryEntity != null && liveCategoryEntity.getCategoryId() != null) {
                linkedHashMap.put(liveCategoryEntity.getCategoryId(), liveCategoryEntity);
            }
        }
        return linkedHashMap;
    }

    public static ArrayList c(List list) {
        if (list == null || list.isEmpty()) {
            return new ArrayList();
        }
        ArrayList arrayList = new ArrayList(list.size());
        Iterator it = list.iterator();
        while (it.hasNext()) {
            LiveCategoryEntity liveCategoryEntity = (LiveCategoryEntity) it.next();
            if (liveCategoryEntity != null) {
                LiveCategoryEntity liveCategoryEntityClone = liveCategoryEntity.clone();
                List<LiveChannelEntity> channels = liveCategoryEntity.getChannels();
                liveCategoryEntityClone.setChannels(channels == null ? null : new ArrayList(channels));
                arrayList.add(liveCategoryEntityClone);
            }
        }
        return arrayList;
    }

    public static LiveChannelEntity i(int i10, ArrayList arrayList) {
        if (arrayList.isEmpty()) {
            return null;
        }
        Iterator it = arrayList.iterator();
        while (it.hasNext()) {
            ArrayList<LiveChannelEntity> arrayListK = k((LiveCategoryEntity) it.next());
            if (!arrayListK.isEmpty()) {
                for (LiveChannelEntity liveChannelEntity : arrayListK) {
                    if (liveChannelEntity != null && liveChannelEntity.getId() != null && Objects.equals(liveChannelEntity.getId(), Integer.valueOf(i10))) {
                        return liveChannelEntity;
                    }
                }
            }
        }
        return null;
    }

    public static ArrayList k(LiveCategoryEntity liveCategoryEntity) {
        return (liveCategoryEntity == null || liveCategoryEntity.getChannels() == null || liveCategoryEntity.getChannels().isEmpty()) ? new ArrayList() : new ArrayList(liveCategoryEntity.getChannels());
    }

    public static re.a o() {
        if (f35897g == null) {
            return null;
        }
        f35897g.getClass();
        return re.a.u();
    }

    public final void d(boolean z10) {
        c cVar = this.f35903f;
        o oVarF = o.f();
        oVarF.getClass();
        String str = "live/channels/adult";
        HashMap map = new HashMap();
        if (z10) {
            map.put("nocache", "1");
        }
        map.put("ttl", String.valueOf(86400L));
        o.g("live/channels/adult", oVarF.n(), map, new j(oVarF, cVar, str, map, 1));
    }

    public final void e(f fVar) {
        this.f35899b.execute(new ve.b(this, fVar, 25));
    }

    public final void f(boolean z10, f fVar) {
        r(fVar);
        b bVar = new b(this, z10);
        o oVarF = o.f();
        oVarF.getClass();
        String str = "live/categories/list";
        HashMap map = new HashMap();
        if (z10) {
            map.put("nocache", "1");
        }
        map.put("ttl", String.valueOf(86400L));
        o.g("live/categories/list", oVarF.n(), map, new j(oVarF, bVar, str, map, 0));
    }

    public final boolean g(int i10) {
        ArrayList arrayList = this.f35901d;
        if (arrayList == null || arrayList.isEmpty()) {
            return true;
        }
        return !this.f35901d.contains(Integer.valueOf(i10));
    }

    public final LiveChannelEntity h(int i10) {
        return i(i10, c(this.f35898a));
    }

    public final List j(List list) {
        if (list == null || list.isEmpty()) {
            return new ArrayList();
        }
        ArrayList<LiveCategoryEntity> arrayListC = c(this.f35898a);
        if (arrayListC.isEmpty()) {
            return list;
        }
        ArrayList arrayList = new ArrayList();
        HashSet hashSet = new HashSet();
        Iterator it = list.iterator();
        while (it.hasNext()) {
            LiveChannelEntity liveChannelEntity = (LiveChannelEntity) it.next();
            if (liveChannelEntity != null && liveChannelEntity.getId() != null) {
                if (liveChannelEntity.getIsIptv() == 1) {
                    arrayList.add(liveChannelEntity);
                } else {
                    Integer id2 = liveChannelEntity.getId();
                    if (!hashSet.contains(id2)) {
                        boolean z10 = false;
                        for (LiveCategoryEntity liveCategoryEntity : arrayListC) {
                            ArrayList arrayListK = k(liveCategoryEntity);
                            if (liveCategoryEntity != null && !arrayListK.isEmpty()) {
                                Iterator it2 = arrayListK.iterator();
                                while (true) {
                                    if (!it2.hasNext()) {
                                        break;
                                    }
                                    LiveChannelEntity liveChannelEntity2 = (LiveChannelEntity) it2.next();
                                    if (liveChannelEntity2 != null && liveChannelEntity2.getId() != null && Objects.equals(liveChannelEntity2.getId(), id2)) {
                                        arrayList.add(liveChannelEntity2);
                                        hashSet.add(id2);
                                        z10 = true;
                                        break;
                                    }
                                }
                                if (z10) {
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
        return arrayList;
    }

    public final List l() {
        if (!xyz.stream.utils.e.h() || c(this.f35898a).isEmpty()) {
            return new ArrayList();
        }
        int i10 = 1;
        CountDownLatch countDownLatch = new CountDownLatch(1);
        AtomicReference atomicReference = new AtomicReference();
        l lVarA = l.a();
        e eVar = new e(this, atomicReference, countDownLatch, i10);
        lVarA.getClass();
        lVarA.f35828a.execute(new xyz.stream.api.database.g(lVarA, eVar, i10));
        try {
            countDownLatch.await();
            return (List) atomicReference.get();
        } catch (InterruptedException unused) {
            Thread.currentThread().interrupt();
            return new ArrayList();
        }
    }

    public final void m(xyz.stream.api.e eVar) {
        if (!xyz.stream.utils.e.h() || c(this.f35898a).isEmpty()) {
            eVar.onSuccess(new ArrayList());
            return;
        }
        l lVarA = l.a();
        i iVar = new i(this, eVar, 2);
        lVarA.getClass();
        lVarA.f35828a.execute(new xyz.stream.api.database.g(lVarA, iVar, 0));
    }

    public final List n() {
        if (!xyz.stream.utils.e.h() || c(this.f35898a).isEmpty()) {
            return new ArrayList();
        }
        CountDownLatch countDownLatch = new CountDownLatch(1);
        AtomicReference atomicReference = new AtomicReference();
        l lVarA = l.a();
        int i10 = 0;
        e eVar = new e(this, atomicReference, countDownLatch, i10);
        lVarA.getClass();
        lVarA.f35828a.execute(new xyz.stream.api.database.g(lVarA, eVar, i10));
        try {
            countDownLatch.await();
            return (List) atomicReference.get();
        } catch (InterruptedException unused) {
            Thread.currentThread().interrupt();
            return new ArrayList();
        }
    }

    public final void p() {
        ArrayList<f> arrayList;
        ArrayList arrayList2;
        ArrayList arrayList3;
        synchronized (this.f35900c) {
            arrayList = new ArrayList(this.f35900c);
            this.f35900c.clear();
        }
        ArrayList<LiveCategoryEntity> arrayListC = c(this.f35898a);
        if (arrayListC.isEmpty()) {
            arrayList3 = new ArrayList();
        } else {
            if (xyz.stream.utils.e.d().f36116e) {
                arrayList2 = new ArrayList(arrayListC);
            } else {
                arrayList2 = new ArrayList();
                for (LiveCategoryEntity liveCategoryEntity : arrayListC) {
                    if (liveCategoryEntity == null || liveCategoryEntity.getIsAdult() != 1) {
                        arrayList2.add(liveCategoryEntity);
                    }
                }
            }
            arrayList3 = arrayList2;
        }
        for (f fVar : arrayList) {
            if (fVar != null) {
                a aVar = new a(fVar, arrayList3, 1);
                re.a aVar2 = (re.a) this;
                if (Looper.myLooper() == Looper.getMainLooper()) {
                    aVar.run();
                } else {
                    aVar2.f32537h.post(aVar);
                }
            }
        }
    }

    public final void q(f fVar, ArrayList arrayList) {
        ArrayList arrayList2;
        if (fVar == null) {
            return;
        }
        if (arrayList == null || arrayList.isEmpty()) {
            arrayList2 = new ArrayList();
        } else if (xyz.stream.utils.e.d().f36116e) {
            arrayList2 = new ArrayList(arrayList);
        } else {
            arrayList2 = new ArrayList();
            Iterator it = arrayList.iterator();
            while (it.hasNext()) {
                LiveChannelEntity liveChannelEntity = (LiveChannelEntity) it.next();
                if (liveChannelEntity == null || liveChannelEntity.getIsAdult() != 1) {
                    arrayList2.add(liveChannelEntity);
                }
            }
        }
        a aVar = new a(fVar, arrayList2, 0);
        re.a aVar2 = (re.a) this;
        if (Looper.myLooper() == Looper.getMainLooper()) {
            aVar.run();
        } else {
            aVar2.f32537h.post(aVar);
        }
    }

    public final void r(f fVar) {
        if (fVar == null) {
            return;
        }
        synchronized (this.f35900c) {
            try {
                if (!this.f35900c.contains(fVar)) {
                    this.f35900c.add(fVar);
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final void s(int i10) {
        synchronized (this) {
            try {
                if (c(this.f35898a).isEmpty()) {
                    return;
                }
                if (this.f35901d == null) {
                    this.f35901d = new ArrayList();
                }
                if (this.f35901d.contains(Integer.valueOf(i10))) {
                    this.f35901d.remove(Integer.valueOf(i10));
                } else {
                    this.f35901d.add(Integer.valueOf(i10));
                }
                f0.o().C("sp_selected_live_category", this.f35901d);
            } catch (Throwable th) {
                throw th;
            }
        }
    }
}
