package xyz.stream.api.viewmodel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import xyz.stream.api.entity.TagVideoEntity;
import xyz.stream.api.entity.VideoEntity;

/* JADX INFO: loaded from: classes3.dex */
public class SearchResultViewModel extends ApiViewModel {
    public androidx.lifecycle.v liveSearch;
    public androidx.lifecycle.v liveSearchResult;
    private androidx.lifecycle.w searchLiveData;
    private List<VideoEntity> trendingList;
    private final ExecutorService searchCoordinator = Executors.newSingleThreadExecutor();
    private final ExecutorService searchWorkers = Executors.newFixedThreadPool(3);

    /* JADX INFO: Access modifiers changed from: private */
    public /* synthetic */ void lambda$search$3(final String str, androidx.lifecycle.w wVar) {
        final int i10 = 0;
        Future futureSubmit = this.searchWorkers.submit(new Callable(this) { // from class: xyz.stream.api.viewmodel.v

            /* JADX INFO: renamed from: b, reason: collision with root package name */
            public final /* synthetic */ SearchResultViewModel f36012b;

            {
                this.f36012b = this;
            }

            @Override // java.util.concurrent.Callable
            public final Object call() {
                int i11 = i10;
                String str2 = str;
                SearchResultViewModel searchResultViewModel = this.f36012b;
                switch (i11) {
                    case 0:
                        return searchResultViewModel.lambda$search$0(str2);
                    case 1:
                        return searchResultViewModel.lambda$search$1(str2);
                    default:
                        return searchResultViewModel.lambda$search$2(str2);
                }
            }
        });
        final int i11 = 1;
        Future futureSubmit2 = this.searchWorkers.submit(new Callable(this) { // from class: xyz.stream.api.viewmodel.v

            /* JADX INFO: renamed from: b, reason: collision with root package name */
            public final /* synthetic */ SearchResultViewModel f36012b;

            {
                this.f36012b = this;
            }

            @Override // java.util.concurrent.Callable
            public final Object call() {
                int i112 = i11;
                String str2 = str;
                SearchResultViewModel searchResultViewModel = this.f36012b;
                switch (i112) {
                    case 0:
                        return searchResultViewModel.lambda$search$0(str2);
                    case 1:
                        return searchResultViewModel.lambda$search$1(str2);
                    default:
                        return searchResultViewModel.lambda$search$2(str2);
                }
            }
        });
        final int i12 = 2;
        Future futureSubmit3 = this.searchWorkers.submit(new Callable(this) { // from class: xyz.stream.api.viewmodel.v

            /* JADX INFO: renamed from: b, reason: collision with root package name */
            public final /* synthetic */ SearchResultViewModel f36012b;

            {
                this.f36012b = this;
            }

            @Override // java.util.concurrent.Callable
            public final Object call() {
                int i112 = i12;
                String str2 = str;
                SearchResultViewModel searchResultViewModel = this.f36012b;
                switch (i112) {
                    case 0:
                        return searchResultViewModel.lambda$search$0(str2);
                    case 1:
                        return searchResultViewModel.lambda$search$1(str2);
                    default:
                        return searchResultViewModel.lambda$search$2(str2);
                }
            }
        });
        try {
            ArrayList arrayList = new ArrayList();
            TimeUnit timeUnit = TimeUnit.SECONDS;
            List list = (List) futureSubmit.get(15L, timeUnit);
            List list2 = (List) futureSubmit2.get(15L, timeUnit);
            List list3 = (List) futureSubmit3.get(15L, timeUnit);
            if (list != null && !list.isEmpty()) {
                arrayList.addAll(list);
            }
            if (list3 != null && !list3.isEmpty()) {
                arrayList.addAll(list3);
            }
            if (list2 != null && !list2.isEmpty()) {
                arrayList.addAll(list2);
            }
            wVar.j(arrayList);
        } catch (Exception e10) {
            xyz.stream.utils.h.c("search futures error: " + e10.getMessage());
            wVar.j(new ArrayList());
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* JADX INFO: renamed from: searchChannel, reason: merged with bridge method [inline-methods] */
    public List<TagVideoEntity> lambda$search$2(String str) {
        re.a aVarO = xyz.stream.api.live.g.o();
        if (aVarO == null) {
            return new ArrayList();
        }
        CountDownLatch countDownLatch = new CountDownLatch(1);
        AtomicReference atomicReference = new AtomicReference();
        aVarO.f35899b.execute(new androidx.emoji2.text.v(aVarO, new x(atomicReference, countDownLatch), str, 23));
        try {
            countDownLatch.await(15L, TimeUnit.SECONDS);
        } catch (Exception e10) {
            u.a.g(e10, new StringBuilder("searchChannel latch await error: "));
        }
        List<TagVideoEntity> list = (List) atomicReference.get();
        return list == null ? new ArrayList() : list;
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* JADX INFO: renamed from: searchPersonFromApi, reason: merged with bridge method [inline-methods] */
    public List<TagVideoEntity> lambda$search$1(String str) {
        CountDownLatch countDownLatch = new CountDownLatch(1);
        AtomicReference atomicReference = new AtomicReference();
        xyz.stream.api.o oVarF = xyz.stream.api.o.f();
        w wVar = new w(atomicReference, countDownLatch, 1);
        oVarF.getClass();
        HashMap map = new HashMap();
        map.put("q", str.trim());
        xyz.stream.api.o.g("people/search", oVarF.n(), map, new xyz.stream.api.j(oVarF, wVar, "people/search", map, 10));
        try {
            countDownLatch.await(15L, TimeUnit.SECONDS);
        } catch (Exception e10) {
            u.a.g(e10, new StringBuilder("searchPersonFromApi latch await error: "));
        }
        List<TagVideoEntity> list = (List) atomicReference.get();
        return list == null ? new ArrayList() : list;
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* JADX INFO: renamed from: searchVideoFromApi, reason: merged with bridge method [inline-methods] */
    public List<TagVideoEntity> lambda$search$0(String str) {
        CountDownLatch countDownLatch = new CountDownLatch(1);
        AtomicReference atomicReference = new AtomicReference();
        xyz.stream.api.o.f().C(str, new w(atomicReference, countDownLatch, 0));
        try {
            countDownLatch.await(15L, TimeUnit.SECONDS);
        } catch (Exception e10) {
            u.a.g(e10, new StringBuilder("searchVideoFromApi latch await error: "));
        }
        List<TagVideoEntity> list = (List) atomicReference.get();
        return list == null ? new ArrayList() : list;
    }

    public void getSearchList() {
        androidx.lifecycle.w wVar = new androidx.lifecycle.w();
        this.searchLiveData = wVar;
        this.liveSearch = wVar;
        List<VideoEntity> list = this.trendingList;
        if (list != null && !list.isEmpty()) {
            this.searchLiveData.j(this.trendingList);
        } else {
            this.trendingList = null;
            xyz.stream.api.o.f().t(1, 20, 0, 0, null, null, null, "search", null, null, null, null, new s(this, 1));
        }
    }

    @Override // androidx.lifecycle.s0
    public void onCleared() {
        this.searchCoordinator.shutdownNow();
        this.searchWorkers.shutdownNow();
        super.onCleared();
    }

    public void search(String str) {
        androidx.lifecycle.w wVar = new androidx.lifecycle.w();
        this.liveSearchResult = wVar;
        this.searchCoordinator.execute(new androidx.emoji2.text.v(this, str, wVar, 24));
    }
}
