package xyz.stream.api.viewmodel;

import android.app.Activity;
import android.content.Context;
import androidx.room.RoomDatabase;
import com.google.android.gms.internal.atv_ads_framework.d1;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.Callable;
import tv.danmaku.ijk.media.player.IjkMediaMeta;
import vf.t;
import xyz.stream.api.database.entity.MyListEntity;
import xyz.stream.api.database.entity.RecordEntity;
import xyz.stream.api.entity.BaseEntity;
import xyz.stream.api.entity.SeriesEntity;
import xyz.stream.utils.f0;

/* JADX INFO: loaded from: classes3.dex */
public class VideoInfoViewModel extends ApiViewModel {
    private static final int EPISODE_GROUP_SIZE = 10;
    private androidx.lifecycle.w delFavoriteLiveData;
    private final androidx.lifecycle.w detailLiveData;
    private androidx.lifecycle.w dislikeLiveData;
    private androidx.lifecycle.w episodeLiveData;
    private androidx.lifecycle.w favoriteLiveData;
    public androidx.lifecycle.v liveDelFavorite;
    public androidx.lifecycle.v liveDetail;
    public androidx.lifecycle.v liveDislike;
    public androidx.lifecycle.v liveEpisodes;
    public androidx.lifecycle.v liveFavorite;
    public androidx.lifecycle.v liveSimilar;
    public androidx.lifecycle.v liveVideoSource;
    public androidx.lifecycle.v liveVideos;
    private final androidx.lifecycle.w similarLiveData;
    private androidx.lifecycle.w videoSourceLiveData;
    private androidx.lifecycle.w videosLiveData;
    public final c0 episodes = new c0(this);
    private final Map<Integer, List<SeriesEntity>> seasonEpisodeCache = new HashMap();

    public VideoInfoViewModel() {
        androidx.lifecycle.w wVar = new androidx.lifecycle.w();
        this.detailLiveData = wVar;
        this.liveDetail = wVar;
        androidx.lifecycle.w wVar2 = new androidx.lifecycle.w();
        this.similarLiveData = wVar2;
        this.liveSimilar = wVar2;
    }

    private List<String> buildEpisodeTitles(List<SeriesEntity> list) {
        String strValueOf;
        ArrayList arrayList = new ArrayList();
        if (list != null && !list.isEmpty()) {
            int i10 = 0;
            while (i10 < list.size()) {
                int i11 = i10 + 10;
                int iMin = Math.min(i11, list.size());
                int displayEpisodeNumber = getDisplayEpisodeNumber(list.get(i10), i10 + 1);
                int displayEpisodeNumber2 = getDisplayEpisodeNumber(list.get(iMin - 1), iMin);
                if (displayEpisodeNumber == displayEpisodeNumber2) {
                    strValueOf = String.valueOf(displayEpisodeNumber);
                } else {
                    strValueOf = displayEpisodeNumber + "-" + displayEpisodeNumber2;
                }
                arrayList.add(strValueOf);
                i10 = i11;
            }
        }
        return arrayList;
    }

    private int findEpisodeIndex(List<SeriesEntity> list, int i10) {
        for (int i11 = 0; i11 < list.size(); i11++) {
            if (Objects.equals(Integer.valueOf(list.get(i11).getVideoId()), Integer.valueOf(i10))) {
                return i11;
            }
        }
        return 0;
    }

    private List<SeriesEntity> getCurrentEpisodeGroup(List<SeriesEntity> list) {
        if (list == null || list.isEmpty()) {
            this.episodes.a(0);
            return new ArrayList();
        }
        int i10 = this.episodes.f35959c * 10;
        if (i10 >= list.size()) {
            this.episodes.b(0);
            i10 = 0;
        }
        int iMin = Math.min(i10 + 10, list.size());
        int iMax = Math.max((iMin - i10) - 1, 0);
        c0 c0Var = this.episodes;
        if (c0Var.f35958b > iMax) {
            c0Var.a(0);
        }
        return new ArrayList(list.subList(i10, iMin));
    }

    private int getDisplayEpisodeNumber(SeriesEntity seriesEntity, int i10) {
        return (seriesEntity == null || seriesEntity.getEpisodeNumber() <= 0) ? i10 : seriesEntity.getEpisodeNumber();
    }

    private List<SeriesEntity> getSeasonEpisodes(int i10, Integer num, Integer num2) {
        if (num2 == null && num != null && this.seasonEpisodeCache.containsKey(num)) {
            return this.seasonEpisodeCache.get(num);
        }
        List<SeriesEntity> listM = xyz.stream.api.o.f().m(i10, num2, num);
        if (listM == null) {
            return new ArrayList();
        }
        if (!listM.isEmpty()) {
            this.seasonEpisodeCache.put(Integer.valueOf(listM.get(0).getSeasonNumber()), listM);
        } else if (num != null) {
            this.seasonEpisodeCache.put(num, listM);
        }
        return listM;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void lambda$getSeriesList$0(boolean z10, int i10, int i11) {
        List<SeriesEntity> listRestoreEpisodesFromRecord = z10 ? restoreEpisodesFromRecord(i10) : null;
        if (listRestoreEpisodesFromRecord == null) {
            listRestoreEpisodesFromRecord = getSeasonEpisodes(i10, Integer.valueOf(this.episodes.f35957a), null);
            updateEpisodeState(listRestoreEpisodesFromRecord, i11, null);
        }
        this.episodeLiveData.j(getCurrentEpisodeGroup(listRestoreEpisodesFromRecord));
    }

    private List<SeriesEntity> restoreEpisodesFromRecord(int i10) {
        RecordEntity recordEntity;
        List<SeriesEntity> seasonEpisodes;
        List listB = xyz.stream.api.database.q.a().b(Integer.valueOf(i10));
        if (listB == null || listB.isEmpty() || (recordEntity = (RecordEntity) listB.get(0)) == null || recordEntity.getVideoId() == null || (seasonEpisodes = getSeasonEpisodes(i10, null, recordEntity.getVideoId())) == null || seasonEpisodes.isEmpty()) {
            return null;
        }
        this.episodes.c(seasonEpisodes.get(0).getSeasonNumber());
        updateEpisodeState(seasonEpisodes, 1, recordEntity.getVideoId());
        return seasonEpisodes;
    }

    private void updateEpisodeState(List<SeriesEntity> list, int i10, Integer num) {
        c0 c0Var = this.episodes;
        List<String> listBuildEpisodeTitles = buildEpisodeTitles(list);
        c0Var.getClass();
        c0Var.f35960d = listBuildEpisodeTitles == null ? new ArrayList() : new ArrayList(listBuildEpisodeTitles);
        if (list == null || list.isEmpty()) {
            this.episodes.b(0);
            this.episodes.a(0);
        } else if (num == null) {
            this.episodes.b(Math.min(Math.max(i10 - 1, 0), Math.max(this.episodes.f35960d.size() - 1, 0)));
            this.episodes.a(0);
        } else {
            int iFindEpisodeIndex = findEpisodeIndex(list, num.intValue());
            this.episodes.b(Math.min(iFindEpisodeIndex / 10, r5.f35960d.size() - 1));
            this.episodes.a(iFindEpisodeIndex % 10);
        }
    }

    public void delFavorite(Activity activity, int i10, int i11) {
        androidx.lifecycle.w wVar = new androidx.lifecycle.w();
        this.delFavoriteLiveData = wVar;
        this.liveDelFavorite = wVar;
        xyz.stream.api.o oVarF = xyz.stream.api.o.f();
        a0 a0Var = new a0(this, activity, 3);
        oVarF.getClass();
        vf.t tVarA = xyz.stream.api.database.n.a();
        if (tVarA != null) {
            xyz.stream.api.database.n.c(tVarA);
            xyz.stream.api.database.n.f35835a.execute(new i0.o(i10, 15, tVarA));
        }
        if (xyz.stream.api.o.D()) {
            new BaseEntity().setCode(200);
            a0Var.onSuccess(Boolean.TRUE);
            return;
        }
        HashMap map = new HashMap();
        map.put("video_id", Integer.valueOf(i10));
        map.put("source_type", Integer.valueOf(i11));
        map.put("role_id", f0.o().q("flutter.selectRoles", ""));
        xyz.stream.api.o.v("user/favorite/del", oVarF.n(), xyz.stream.utils.m.a(map), new xyz.stream.api.m(oVarF, a0Var, "user/favorite/del", map, 5));
    }

    public void dislike(Activity activity, int i10, int i11) {
        androidx.lifecycle.w wVar = new androidx.lifecycle.w();
        this.dislikeLiveData = wVar;
        this.liveDislike = wVar;
        xyz.stream.api.o oVarF = xyz.stream.api.o.f();
        a0 a0Var = new a0(this, activity, 1);
        oVarF.getClass();
        ArrayList arrayListM = f0.o().m(Integer.class, "sp_dislike_list");
        if (!arrayListM.contains(Integer.valueOf(i10))) {
            arrayListM.add(Integer.valueOf(i10));
            f0.o().C("sp_dislike_list", arrayListM);
        }
        if (xyz.stream.api.o.D()) {
            a0Var.onSuccess(Boolean.TRUE);
            return;
        }
        HashMap map = new HashMap();
        map.put("video_id", Integer.valueOf(i10));
        map.put("source_type", Integer.valueOf(i11));
        map.put("role_id", f0.o().q("flutter.selectRoles", ""));
        xyz.stream.api.o.v("user/dislike", oVarF.n(), xyz.stream.utils.m.a(map), new xyz.stream.api.m(oVarF, a0Var, "user/dislike", map, 3));
    }

    public void favorite(Activity activity, MyListEntity myListEntity) {
        androidx.lifecycle.w wVar = new androidx.lifecycle.w();
        this.favoriteLiveData = wVar;
        this.liveFavorite = wVar;
        xyz.stream.api.o oVarF = xyz.stream.api.o.f();
        a0 a0Var = new a0(this, activity, 2);
        oVarF.getClass();
        vf.t tVarA = xyz.stream.api.database.n.a();
        if (tVarA != null) {
            xyz.stream.api.database.n.c(tVarA);
            xyz.stream.api.database.n.f35835a.execute(new ve.b(tVarA, myListEntity, 20));
        }
        if (xyz.stream.api.o.D()) {
            a0Var.onSuccess(Boolean.TRUE);
            return;
        }
        HashMap map = new HashMap();
        map.put("video_id", myListEntity.getVideoId());
        map.put("video_pic", myListEntity.getVideoPic());
        map.put("backdrop_path", myListEntity.getBackdropPath());
        map.put("wide_backdrop_path", myListEntity.getWideBackdropPath());
        map.put("vote_average", myListEntity.getVoteAverage());
        map.put("source_type", myListEntity.getSourceType());
        map.put(IjkMediaMeta.IJKM_KEY_TITLE, myListEntity.getTitle());
        map.put("role_id", myListEntity.getRoleId());
        map.put("update_time", myListEntity.getUpdateTime());
        xyz.stream.api.o.v("user/favorite", oVarF.n(), xyz.stream.utils.m.a(map), new xyz.stream.api.m(oVarF, a0Var, "user/favorite", map, 4));
    }

    public void getSeriesList(final int i10, final int i11, final boolean z10) {
        androidx.lifecycle.w wVar = new androidx.lifecycle.w();
        this.episodeLiveData = wVar;
        this.liveEpisodes = wVar;
        new Thread(new Runnable() { // from class: xyz.stream.api.viewmodel.z
            @Override // java.lang.Runnable
            public final void run() {
                this.f36022a.lambda$getSeriesList$0(z10, i10, i11);
            }
        }).start();
    }

    public void getSimilarVideos(Context context, List<String> list, int i10, String str, List<Integer> list2) {
        if (str != null) {
            xyz.stream.api.o.f().C(str, new b0(this, context, 0));
            return;
        }
        xyz.stream.api.o oVarF = xyz.stream.api.o.f();
        if (list2 != null && !list2.isEmpty()) {
            list = null;
        }
        if (list2 == null || list2.isEmpty()) {
            list2 = null;
        }
        oVarF.q(list, i10, list2, new b0(this, context, 1));
    }

    public void getVideoDetailFromApi(Activity activity, int i10, int i11) {
        if (activity == null || activity.isFinishing() || activity.isDestroyed()) {
            return;
        }
        xyz.stream.api.a0.h().f35758b = null;
        xyz.stream.api.o.f().s(i10, i11, new a0(this, activity, 0));
    }

    public void getVideoList(Context context, final Integer num, final int i10, final int i11) {
        ArrayList arrayList;
        androidx.lifecycle.w wVar = new androidx.lifecycle.w();
        this.videosLiveData = wVar;
        this.liveVideos = wVar;
        xyz.stream.api.o.f().getClass();
        if (xyz.stream.api.o.D()) {
            final vf.t tVarA = xyz.stream.api.database.n.a();
            if (tVarA == null) {
                arrayList = new ArrayList();
            } else {
                xyz.stream.api.database.n.c(tVarA);
                try {
                    List list = (List) xyz.stream.api.database.n.f35835a.submit(new Callable() { // from class: xyz.stream.api.database.m
                        @Override // java.util.concurrent.Callable
                        public final Object call() {
                            Integer num2 = num;
                            t tVar = tVarA;
                            final int i12 = i11;
                            int i13 = i10;
                            if (num2 == null || num2.intValue() == 0) {
                                final int i14 = (i13 - 1) * i12;
                                final String strQ = f0.o().q("flutter.selectRoles", "");
                                return (List) androidx.room.util.b.b((RoomDatabase) ((d1) tVar).f19914b, true, false, new sd.l() { // from class: vf.u
                                    @Override // sd.l
                                    public final Object invoke(Object obj) throws Exception {
                                        int i15 = i12;
                                        int i16 = i14;
                                        k3.c cVarJ = ((k3.a) obj).J("SELECT * FROM my_list WHERE roleId = ? ORDER BY updateTime DESC LIMIT ? OFFSET ?");
                                        String str = strQ;
                                        try {
                                            if (str == null) {
                                                cVarJ.d(1);
                                            } else {
                                                cVarJ.s(1, str);
                                            }
                                            cVarJ.a(2, i15);
                                            cVarJ.a(3, i16);
                                            int iC = androidx.room.util.f.c(cVarJ, "videoId");
                                            int iC2 = androidx.room.util.f.c(cVarJ, "videoPic");
                                            int iC3 = androidx.room.util.f.c(cVarJ, "backdropPath");
                                            int iC4 = androidx.room.util.f.c(cVarJ, "wideBackdropPath");
                                            int iC5 = androidx.room.util.f.c(cVarJ, "voteAverage");
                                            int iC6 = androidx.room.util.f.c(cVarJ, "sourceType");
                                            int iC7 = androidx.room.util.f.c(cVarJ, IjkMediaMeta.IJKM_KEY_TITLE);
                                            int iC8 = androidx.room.util.f.c(cVarJ, "updateTime");
                                            int iC9 = androidx.room.util.f.c(cVarJ, "roleId");
                                            ArrayList arrayList2 = new ArrayList();
                                            while (cVarJ.G()) {
                                                MyListEntity myListEntity = new MyListEntity();
                                                if (cVarJ.isNull(iC)) {
                                                    myListEntity.videoId = null;
                                                } else {
                                                    myListEntity.videoId = Integer.valueOf((int) cVarJ.getLong(iC));
                                                }
                                                if (cVarJ.isNull(iC2)) {
                                                    myListEntity.videoPic = null;
                                                } else {
                                                    myListEntity.videoPic = cVarJ.A(iC2);
                                                }
                                                if (cVarJ.isNull(iC3)) {
                                                    myListEntity.backdropPath = null;
                                                } else {
                                                    myListEntity.backdropPath = cVarJ.A(iC3);
                                                }
                                                if (cVarJ.isNull(iC4)) {
                                                    myListEntity.wideBackdropPath = null;
                                                } else {
                                                    myListEntity.wideBackdropPath = cVarJ.A(iC4);
                                                }
                                                if (cVarJ.isNull(iC5)) {
                                                    myListEntity.voteAverage = null;
                                                } else {
                                                    myListEntity.voteAverage = cVarJ.A(iC5);
                                                }
                                                if (cVarJ.isNull(iC6)) {
                                                    myListEntity.sourceType = null;
                                                } else {
                                                    myListEntity.sourceType = Integer.valueOf((int) cVarJ.getLong(iC6));
                                                }
                                                if (cVarJ.isNull(iC7)) {
                                                    myListEntity.title = null;
                                                } else {
                                                    myListEntity.title = cVarJ.A(iC7);
                                                }
                                                if (cVarJ.isNull(iC8)) {
                                                    myListEntity.updateTime = null;
                                                } else {
                                                    myListEntity.updateTime = Long.valueOf(cVarJ.getLong(iC8));
                                                }
                                                if (cVarJ.isNull(iC9)) {
                                                    myListEntity.roleId = null;
                                                } else {
                                                    myListEntity.roleId = cVarJ.A(iC9);
                                                }
                                                arrayList2.add(myListEntity);
                                            }
                                            cVarJ.close();
                                            return arrayList2;
                                        } catch (Throwable th) {
                                            cVarJ.close();
                                            throw th;
                                        }
                                    }
                                });
                            }
                            final int iIntValue = num2.intValue();
                            final int i15 = (i13 - 1) * i12;
                            final String strQ2 = f0.o().q("flutter.selectRoles", "");
                            return (List) androidx.room.util.b.b((RoomDatabase) ((d1) tVar).f19914b, true, false, new sd.l() { // from class: vf.v
                                @Override // sd.l
                                public final Object invoke(Object obj) throws Exception {
                                    int i16 = iIntValue;
                                    int i17 = i12;
                                    int i18 = i15;
                                    k3.c cVarJ = ((k3.a) obj).J("SELECT * FROM my_list WHERE sourceType = ? AND roleId = ? ORDER BY updateTime DESC LIMIT ? OFFSET ?");
                                    try {
                                        cVarJ.a(1, i16);
                                        String str = strQ2;
                                        if (str == null) {
                                            cVarJ.d(2);
                                        } else {
                                            cVarJ.s(2, str);
                                        }
                                        cVarJ.a(3, i17);
                                        cVarJ.a(4, i18);
                                        int iC = androidx.room.util.f.c(cVarJ, "videoId");
                                        int iC2 = androidx.room.util.f.c(cVarJ, "videoPic");
                                        int iC3 = androidx.room.util.f.c(cVarJ, "backdropPath");
                                        int iC4 = androidx.room.util.f.c(cVarJ, "wideBackdropPath");
                                        int iC5 = androidx.room.util.f.c(cVarJ, "voteAverage");
                                        int iC6 = androidx.room.util.f.c(cVarJ, "sourceType");
                                        int iC7 = androidx.room.util.f.c(cVarJ, IjkMediaMeta.IJKM_KEY_TITLE);
                                        int iC8 = androidx.room.util.f.c(cVarJ, "updateTime");
                                        int iC9 = androidx.room.util.f.c(cVarJ, "roleId");
                                        ArrayList arrayList2 = new ArrayList();
                                        while (cVarJ.G()) {
                                            MyListEntity myListEntity = new MyListEntity();
                                            if (cVarJ.isNull(iC)) {
                                                myListEntity.videoId = null;
                                            } else {
                                                myListEntity.videoId = Integer.valueOf((int) cVarJ.getLong(iC));
                                            }
                                            if (cVarJ.isNull(iC2)) {
                                                myListEntity.videoPic = null;
                                            } else {
                                                myListEntity.videoPic = cVarJ.A(iC2);
                                            }
                                            if (cVarJ.isNull(iC3)) {
                                                myListEntity.backdropPath = null;
                                            } else {
                                                myListEntity.backdropPath = cVarJ.A(iC3);
                                            }
                                            if (cVarJ.isNull(iC4)) {
                                                myListEntity.wideBackdropPath = null;
                                            } else {
                                                myListEntity.wideBackdropPath = cVarJ.A(iC4);
                                            }
                                            if (cVarJ.isNull(iC5)) {
                                                myListEntity.voteAverage = null;
                                            } else {
                                                myListEntity.voteAverage = cVarJ.A(iC5);
                                            }
                                            if (cVarJ.isNull(iC6)) {
                                                myListEntity.sourceType = null;
                                            } else {
                                                myListEntity.sourceType = Integer.valueOf((int) cVarJ.getLong(iC6));
                                            }
                                            if (cVarJ.isNull(iC7)) {
                                                myListEntity.title = null;
                                            } else {
                                                myListEntity.title = cVarJ.A(iC7);
                                            }
                                            if (cVarJ.isNull(iC8)) {
                                                myListEntity.updateTime = null;
                                            } else {
                                                myListEntity.updateTime = Long.valueOf(cVarJ.getLong(iC8));
                                            }
                                            if (cVarJ.isNull(iC9)) {
                                                myListEntity.roleId = null;
                                            } else {
                                                myListEntity.roleId = cVarJ.A(iC9);
                                            }
                                            arrayList2.add(myListEntity);
                                        }
                                        cVarJ.close();
                                        return arrayList2;
                                    } catch (Throwable th) {
                                        cVarJ.close();
                                        throw th;
                                    }
                                }
                            });
                        }
                    }).get();
                    arrayList = list == null ? new ArrayList() : t4.f.x(list).m(new xyz.netfly.viewmodel.a(8)).e(new xyz.netfly.viewmodel.a(9)).E();
                } catch (Exception e10) {
                    xyz.stream.utils.h.c(e10.getMessage());
                    arrayList = new ArrayList();
                }
            }
            this.videosLiveData.j(arrayList);
            return;
        }
        xyz.stream.api.o oVarF = xyz.stream.api.o.f();
        b0 b0Var = new b0(this, context, 2);
        oVarF.getClass();
        String str = "user/favorite";
        HashMap map = new HashMap();
        map.put("page", String.valueOf(i10));
        map.put("page_size", String.valueOf(i11));
        map.put("role_id", f0.o().q("flutter.selectRoles", ""));
        if (num != null && num.intValue() > 0) {
            map.put("source_type", String.valueOf(num));
        }
        xyz.stream.api.o.g("user/favorite", oVarF.n(), map, new xyz.stream.api.m(oVarF, str, map, b0Var, 6));
    }

    public void getVideoSource(int i10, SeriesEntity seriesEntity) {
        androidx.lifecycle.w wVar = new androidx.lifecycle.w();
        this.videoSourceLiveData = wVar;
        this.liveVideoSource = wVar;
        xyz.stream.api.o.f().l(i10, seriesEntity.getVideoId(), new s(this, 2));
    }
}
