package xyz.stream.api.viewmodel;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicReference;
import xyz.stream.api.database.entity.LiveHistory;
import xyz.stream.api.database.entity.RecordEntity;
import xyz.stream.api.entity.MainRecordEntity;
import xyz.stream.api.entity.VideoInfoEntity;

/* JADX INFO: loaded from: classes3.dex */
public class RecommendViewModel extends ApiViewModel {
    private final ExecutorService executors = Executors.newFixedThreadPool(4);
    public List<LiveHistory> liveHistoryList;
    public androidx.lifecycle.v liveRandom;
    public androidx.lifecycle.v liveRecentList;
    public androidx.lifecycle.v liveRecommend;
    public List<RecordEntity> recentList;

    /* JADX INFO: Access modifiers changed from: private */
    public List<LiveHistory> getLiveRecordSync() {
        re.a aVarO = xyz.stream.api.live.g.o();
        if (aVarO == null) {
            return new ArrayList();
        }
        CountDownLatch countDownLatch = new CountDownLatch(1);
        AtomicReference atomicReference = new AtomicReference(new ArrayList());
        aVarO.m(new p(this, atomicReference, countDownLatch, 1));
        try {
            countDownLatch.await();
            return (List) atomicReference.get();
        } catch (InterruptedException unused) {
            Thread.currentThread().interrupt();
            return new ArrayList();
        }
    }

    private List<RecordEntity> getVideoRecordSync() {
        return lambda$getRecentList$2(true);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static /* synthetic */ void lambda$getRandomVideo$0(androidx.lifecycle.w wVar) {
        ArrayList arrayListE = xyz.stream.api.d.b().e(new Random(1000L).nextInt() % 2 != 0 ? 3 : 2);
        if (arrayListE == null || arrayListE.isEmpty()) {
            return;
        }
        wVar.j((VideoInfoEntity) arrayListE.get(new Random().nextInt(arrayListE.size())));
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static /* synthetic */ int lambda$getRecentList$3(MainRecordEntity mainRecordEntity, MainRecordEntity mainRecordEntity2) {
        return mainRecordEntity2.getUpdateDate().compareTo(mainRecordEntity.getUpdateDate());
    }

    /* JADX INFO: Access modifiers changed from: private */
    public /* synthetic */ void lambda$getRecentList$4(boolean z10, androidx.lifecycle.w wVar) {
        try {
            Future futureSubmit = this.executors.submit(new u(0, this, z10));
            Future futureSubmit2 = this.executors.submit(new a4.s(this, 5));
            this.recentList = (List) futureSubmit.get();
            ArrayList arrayList = new ArrayList();
            List<RecordEntity> list = this.recentList;
            if (list != null && !list.isEmpty()) {
                for (RecordEntity recordEntity : this.recentList) {
                    arrayList.add(new MainRecordEntity(recordEntity.getId().intValue(), recordEntity.getTitle(), recordEntity.getVideoPic(), recordEntity.getUpdateDate(), recordEntity.getPlayTime(), recordEntity.getTotalTime(), 2));
                }
            }
            if (futureSubmit2 != null) {
                List<LiveHistory> list2 = (List) futureSubmit2.get();
                this.liveHistoryList = list2;
                if (list2 != null && !list2.isEmpty()) {
                    for (LiveHistory liveHistory : this.liveHistoryList) {
                        arrayList.add(new MainRecordEntity(liveHistory.getChannelId().intValue(), liveHistory.getTvgName(), liveHistory.getTvgLogo(), liveHistory.getUpdateDate(), 0, 0, 1));
                    }
                }
            }
            Collections.sort(arrayList, new androidx.browser.trusted.b(13));
            wVar.j(arrayList.subList(0, Math.min(arrayList.size(), 20)));
        } catch (Exception e10) {
            xyz.stream.utils.h.c("getRecentList error: " + e10.getMessage());
            wVar.j(new ArrayList());
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static /* synthetic */ void lambda$getRecommend$1(int i10, int i11, androidx.lifecycle.w wVar) {
        ArrayList arrayListD = xyz.stream.api.d.b().d(1, i10, i11);
        if (arrayListD == null) {
            arrayListD = new ArrayList();
        }
        wVar.j(arrayListD);
        xyz.stream.api.d.b().h(1, i10, i11);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static /* synthetic */ void lambda$getVideoRecordSync$5(AtomicReference atomicReference, CountDownLatch countDownLatch, List list) {
        if (list == null) {
            list = new ArrayList();
        }
        atomicReference.set(list);
        countDownLatch.countDown();
    }

    public void getRandomVideo() {
        androidx.lifecycle.w wVar = new androidx.lifecycle.w();
        this.liveRandom = wVar;
        this.executors.execute(new o(2, wVar));
    }

    public void getRecentList() {
        getRecentList(true);
    }

    public void getRecommend(int i10, int i11) {
        androidx.lifecycle.w wVar = new androidx.lifecycle.w();
        this.liveRecommend = wVar;
        this.executors.execute(new a(i10, i11, 3, wVar));
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* JADX INFO: renamed from: getVideoRecordSync, reason: merged with bridge method [inline-methods] */
    public List<RecordEntity> lambda$getRecentList$2(boolean z10) {
        CountDownLatch countDownLatch = new CountDownLatch(1);
        AtomicReference atomicReference = new AtomicReference(new ArrayList());
        xyz.stream.api.database.q qVarA = xyz.stream.api.database.q.a();
        t tVar = new t(atomicReference, countDownLatch);
        qVarA.getClass();
        xyz.stream.api.database.q.d(z10, tVar);
        try {
            countDownLatch.await();
            return (List) atomicReference.get();
        } catch (InterruptedException unused) {
            Thread.currentThread().interrupt();
            return new ArrayList();
        }
    }

    public void getRecentList(boolean z10) {
        androidx.lifecycle.w wVar = new androidx.lifecycle.w();
        this.liveRecentList = wVar;
        if (!z10 || xyz.stream.utils.e.h()) {
            this.executors.execute(new com.google.firebase.messaging.v(this, z10, wVar, 3));
        }
    }
}
