package com.ss.android.ugc.aweme.story.feed.common;

import X.0CD;
import X.0CH;
import X.2HN;
import X.2cN;
import X.4Dd;
import X.4V2;
import X.7he;
import X.C0114Ckk;
import X.C0119Ckp;
import X.C0126Ckw;
import X.C0128Cky;
import X.C0129Ckz;
import X.C0244Cpk;
import X.C0295Crj;
import X.CkL;
import X.Cl0;
import X.Cl1;
import X.ClA;
import X.ClK;
import X.ClT;
import X.ESC;
import X.M12;
import X.Mjy;
import X.Mk2;
import X.Mk3;
import X.PhE;
import X.PlI;
import X.TnV;
import android.animation.ValueAnimator;
import android.text.TextUtils;
import com.bytedance.covode.number.Covode;
import com.ss.android.ugc.aweme.arch.widgets.base.DataCenter;
import com.ss.android.ugc.aweme.feed.model.Aweme;
import com.ss.android.ugc.aweme.feed.model.story.Story;
import com.ss.android.ugc.aweme.feed.model.story.UserStory;
import com.ss.android.ugc.aweme.profile.model.User;
import com.ss.android.ugc.aweme.story.api.StoryApi;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import kotlin.h.b.n;
import kotlin.o.y;

public final class StoryFeedViewModel extends DataCenter implements C0114Ckk {
    public static final Cl0 LJI = new Cl0((byte) 0);
    public final Map<String, Aweme> LIZJ = new LinkedHashMap();
    public final Map<String, List<C0128Cky>> LIZLLL = new LinkedHashMap();
    public final C0126Ckw LJ;
    public boolean LJFF;
    public final String LJII;
    public final String LJIIIIZZ;
    public final C0295Crj LJIIIZ = 7he.LIZ(CkL.LIZ);
    public final Mjy LJIIJ = new Mjy();
    public final 0CH LJIIJJI;
    public final TnV LJIIL;

    static {
        Covode.recordClassIndex(117601);
    }

    public final ValueAnimator LIZ() {
        return (ValueAnimator) this.LJIIIZ.getValue();
    }

    public final boolean LIZJ() {
        return TextUtils.equals("STORY_ENTRANCE_INBOX_TOP_LIST", this.LJIIL.getFrom());
    }

    public final boolean LJI() {
        return TextUtils.equals("story_archive", this.LJII);
    }

    public final boolean LJIIIIZZ() {
        return TextUtils.equals("homepage_follow", this.LJII);
    }

    public final boolean LJIIIZ() {
        return TextUtils.equals("homepage_nearby", this.LJII);
    }

    public final boolean LJIIJ() {
        return TextUtils.equals("homepage_hot", this.LJII);
    }

    public final boolean LJIIJJI() {
        return TextUtils.equals("homepage_friends", this.LJII);
    }

    public final boolean LJIIL() {
        return n.LIZ(this.LJIIL.getFrom(), "STORY_ENTRANCE_DEFAULT");
    }

    public final 0CD getLifecycle() {
        0CD lifecycle = this.LJIIJJI.getLifecycle();
        n.LIZIZ(lifecycle, "");
        return lifecycle;
    }

    public final void onCleared() {
        super.onCleared();
        this.LJIIJ.LIZ();
        ClK.LJIIIIZZ.LJ(this.LJIIIIZZ);
    }

    private boolean LJIILIIL() {
        if (n.LIZ("story_widget_extension_large", this.LJII) || n.LIZ("story_widget_extension_small", this.LJII)) {
            return true;
        }
        return false;
    }

    public final List<Aweme> LIZIZ() {
        if (LJ() || LJIIIIZZ()) {
            return ClK.LJIIIIZZ.LIZIZ();
        }
        return ClK.LIZIZ;
    }

    public final boolean LIZLLL() {
        if (!TextUtils.equals("personal_homepage", this.LJII) || !n.LIZ(this.LJIIL.getFrom(), "STORY_ENTRANCE_MINE")) {
            return false;
        }
        return true;
    }

    public final boolean LJ() {
        if (!TextUtils.equals("personal_homepage", this.LJII) || !n.LIZ("from_profile_self", this.LJIIL.getFrom())) {
            return false;
        }
        return true;
    }

    public final boolean LJFF() {
        if (!TextUtils.equals("others_homepage", this.LJII) || !n.LIZ("from_profile_other", this.LJIIL.getFrom())) {
            return false;
        }
        return true;
    }

    public final boolean LJII() {
        if ((TextUtils.equals("homepage_follow", this.LJII) || TextUtils.equals("homepage_hot", this.LJII) || TextUtils.equals("homepage_nearby", this.LJII) || TextUtils.equals("homepage_friends", this.LJII)) && !TextUtils.equals("STORY_ENTRANCE_AVATAR", this.LJIIL.getFrom())) {
            return true;
        }
        return false;
    }

    public final boolean LIZLLL(String str) {
        M12.LIZ(str);
        if (!LJ(str) || LIZIZ().size() <= 0) {
            return false;
        }
        if (LIZLLL()) {
            return true;
        }
        if ((!C0244Cpk.LIZ.LIZJ() || (!LJIIIIZZ() && !LJ())) && !LIZJ() && !this.LJIIL.isShouldQueryMyStoryFromCache()) {
            return false;
        }
        return true;
    }

    public final boolean LJ(String str) {
        String str2;
        M12.LIZ(str);
        User LIZJ2 = ESC.LIZJ();
        if (LIZJ2 != null) {
            str2 = LIZJ2.getUid();
        } else {
            str2 = null;
        }
        return TextUtils.equals(str, str2);
    }

    private final Aweme LJFF(String str) {
        User author;
        Aweme aweme = new Aweme();
        aweme.setAid("0");
        aweme.setAwemeType(40);
        aweme.setStoryFakeAweme(true);
        aweme.setFakeAid(UUID.randomUUID().toString());
        Story story = new Story(0, false, 0, false, 0, false, false, false, false, 511, (2HN) null);
        story.setFaked(true);
        aweme.setStory(story);
        Aweme aweme2 = this.LIZJ.get(str);
        if (!(aweme2 == null || (author = aweme2.getAuthor()) == null)) {
            aweme.setAuthor(author);
        }
        return aweme;
    }

    public final String LIZ(Aweme aweme) {
        String authorUid;
        M12.LIZ(aweme);
        User author = aweme.getAuthor();
        if (author != null) {
            String uid = author.getUid();
            if (uid == null) {
                return "";
            }
            return uid;
        }
        UserStory userStory = aweme.getUserStory();
        if (userStory == null) {
            return "";
        }
        n.LIZIZ(userStory, "");
        if (userStory.getStories().size() <= 0 || (authorUid = userStory.getStories().get(0).getAuthorUid()) == null) {
            return "";
        }
        return authorUid;
    }

    public final List<C0128Cky> LIZJ(String str) {
        UserStory userStory;
        Aweme aweme = this.LIZJ.get(str);
        if (aweme == null || (userStory = aweme.getUserStory()) == null) {
            return new ArrayList();
        }
        n.LIZIZ(userStory, "");
        int totalCount = (int) userStory.getTotalCount();
        ArrayList arrayList = new ArrayList();
        int i = 0;
        int i2 = 0;
        for (int i3 = 0; i3 < totalCount; i3++) {
            long j = (long) i3;
            if (j >= userStory.getCurrentPosition() && j < ((long) userStory.getStories().size()) + userStory.getCurrentPosition()) {
                String aid = userStory.getStories().get(i).getAid();
                if (aid == null) {
                    aid = "";
                }
                arrayList.add(new C0128Cky(aid, C0129Ckz.LOADED));
                i++;
            } else if (!LIZLLL(str) || i3 < totalCount - LIZIZ().size()) {
                arrayList.add(new C0128Cky("", C0129Ckz.UNLOADING));
            } else {
                String aid2 = LIZIZ().get(i2).getAid();
                if (aid2 == null) {
                    aid2 = "";
                }
                arrayList.add(new C0128Cky(aid2, C0129Ckz.PUBLISH));
                i2++;
            }
        }
        this.LIZLLL.put(str, arrayList);
        return arrayList;
    }

    public final List<Aweme> LIZIZ(String str) {
        UserStory userStory;
        long originTotalCount;
        M12.LIZ(str);
        Aweme aweme = this.LIZJ.get(str);
        if (aweme == null || (userStory = aweme.getUserStory()) == null) {
            return null;
        }
        n.LIZIZ(userStory, "");
        if (!userStory.getHasMoreAfter() && !userStory.getHasMoreBefore()) {
            userStory.setOriginTotalCount((long) userStory.getStories().size());
            4V2.LIZ.LIZIZ("huangxin.2020", "getWrappedStories: originTotalCount: " + userStory.getOriginTotalCount() + ", userStory instance: " + System.identityHashCode(userStory));
            if (LIZLLL(str)) {
                originTotalCount = (userStory.getOriginTotalCount() + ((long) LIZIZ().size())) - ((long) ClK.LJIIIIZZ.LIZ(userStory, true));
            } else {
                originTotalCount = userStory.getOriginTotalCount();
            }
            userStory.setTotalCount(originTotalCount);
        }
        if (userStory.getTotalCount() <= ((long) userStory.getStories().size())) {
            ArrayList arrayList = new ArrayList();
            arrayList.addAll(userStory.getStories());
            if (LIZLLL(str)) {
                User LIZJ2 = ESC.LIZJ();
                n.LIZIZ(LIZJ2, "");
                if (y.LIZ(str, LIZJ2.getUid(), true)) {
                    List<Aweme> LIZIZ = LIZIZ();
                    if (!LIZIZ.isEmpty()) {
                        arrayList.addAll(LIZIZ);
                    }
                }
            }
            return arrayList;
        }
        ArrayList arrayList2 = new ArrayList();
        arrayList2.addAll(userStory.getStories());
        if (LIZLLL(str)) {
            if (userStory.getCurrentPosition() + ((long) userStory.getStories().size()) < userStory.getTotalCount() - ((long) LIZIZ().size())) {
                long totalCount = ((userStory.getTotalCount() - ((long) LIZIZ().size())) - userStory.getCurrentPosition()) - ((long) userStory.getStories().size());
                do {
                    totalCount--;
                    arrayList2.add(LJFF(str));
                } while (totalCount > 0);
            }
            List<Aweme> LIZIZ2 = LIZIZ();
            if (true ^ LIZIZ2.isEmpty()) {
                arrayList2.addAll(LIZIZ2);
            }
        } else if (userStory.getCurrentPosition() + ((long) userStory.getStories().size()) < userStory.getTotalCount()) {
            long totalCount2 = (userStory.getTotalCount() - userStory.getCurrentPosition()) - ((long) userStory.getStories().size());
            do {
                totalCount2--;
                arrayList2.add(LJFF(str));
            } while (totalCount2 > 0);
        }
        if (userStory.getCurrentPosition() > 0) {
            long currentPosition = userStory.getCurrentPosition();
            do {
                currentPosition--;
                arrayList2.add(0, LJFF(str));
            } while (currentPosition > 0);
        }
        return arrayList2;
    }

    public StoryFeedViewModel(0CH r4, TnV tnV) {
        M12.LIZ(r4, tnV);
        this.LJIIJJI = r4;
        this.LJIIL = tnV;
        String eventType = tnV.getEventType();
        this.LJII = eventType;
        String str = eventType + ":story_feed_view_model";
        this.LJIIIIZZ = str;
        this.LIZ = r4;
        if (LIZLLL() || LIZJ()) {
            ClK.LJIIIIZZ.LIZ((Object) str, (ClT) new ClA(this));
        }
        this.LJ = new C0126Ckw();
    }

    private final void LIZ(String str, boolean z) {
        UserStory userStory;
        long currentPosition;
        long currentPosition2;
        Aweme aweme = this.LIZJ.get(str);
        if (aweme != null && (userStory = aweme.getUserStory()) != null) {
            n.LIZIZ(userStory, "");
            List<C0128Cky> list = this.LIZLLL.get(str);
            if (list == null) {
                list = LIZJ(str);
            }
            if (z) {
                if (userStory.getCurrentPosition() < 5) {
                    currentPosition2 = 0;
                } else {
                    currentPosition2 = (userStory.getCurrentPosition() + 1) - 5;
                }
                int currentPosition3 = (int) userStory.getCurrentPosition();
                for (int i = (int) currentPosition2; i < currentPosition3; i++) {
                    if (i < list.size()) {
                        list.set(i, new C0128Cky("", C0129Ckz.LOADING));
                    }
                }
                return;
            }
            if (userStory.getCurrentPosition() + ((long) userStory.getStories().size()) + 5 >= userStory.getTotalCount()) {
                currentPosition = userStory.getTotalCount();
            } else {
                currentPosition = userStory.getCurrentPosition() + ((long) userStory.getStories().size()) + 5;
            }
            int i2 = (int) currentPosition;
            for (int size = userStory.getStories().size() + ((int) userStory.getCurrentPosition()); size < i2; size++) {
                if (size < list.size() && list.get(size).LIZIZ == C0129Ckz.UNLOADING) {
                    list.get(size).LIZ(C0129Ckz.LOADING);
                }
            }
        }
    }

    /* JADX WARNING: Removed duplicated region for block: B:11:0x002c A[RETURN] */
    /* JADX WARNING: Removed duplicated region for block: B:16:0x003d  */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public final int LIZ(java.lang.String r11, java.util.List<? extends com.ss.android.ugc.aweme.feed.model.Aweme> r12) {
        /*
            r10 = this;
            X.M12.LIZ(r11, r12)
            java.util.Map<java.lang.String, com.ss.android.ugc.aweme.feed.model.Aweme> r0 = r10.LIZJ
            java.lang.Object r0 = r0.get(r11)
            com.ss.android.ugc.aweme.feed.model.Aweme r0 = (com.ss.android.ugc.aweme.feed.model.Aweme) r0
            r8 = 0
            if (r0 == 0) goto L_0x0014
            com.ss.android.ugc.aweme.feed.model.story.UserStory r7 = r0.getUserStory()
            if (r7 != 0) goto L_0x0015
        L_0x0014:
            return r8
        L_0x0015:
            java.lang.String r0 = ""
            kotlin.h.b.n.LIZIZ(r7, r0)
            boolean r0 = r10.LJIILIIL()
            if (r0 == 0) goto L_0x002d
            long r0 = r7.getCurrentPosition()
        L_0x0024:
            int r2 = (int) r0
        L_0x0025:
            boolean r0 = r12.isEmpty()
            r6 = -1
            if (r0 == 0) goto L_0x003d
            return r6
        L_0x002d:
            java.lang.Integer r0 = com.ss.android.ugc.aweme.feed.model.story.UserStoryKt.currentIndex(r7)
            if (r0 == 0) goto L_0x0038
            int r2 = r0.intValue()
            goto L_0x0025
        L_0x0038:
            long r0 = r7.getCurrentPosition()
            goto L_0x0024
        L_0x003d:
            int r0 = r12.size()
            r5 = 1
            int r0 = r0 - r5
            if (r2 != r0) goto L_0x005e
            java.lang.Object r0 = r12.get(r2)
            com.ss.android.ugc.aweme.feed.model.Aweme r0 = (com.ss.android.ugc.aweme.feed.model.Aweme) r0
            com.ss.android.ugc.aweme.feed.model.story.Story r0 = r0.getStory()
            if (r0 == 0) goto L_0x005e
            boolean r0 = r0.getViewed()
            if (r0 != r5) goto L_0x005e
            boolean r0 = r10.LJIILIIL()
            if (r0 != 0) goto L_0x005e
            r2 = 0
        L_0x005e:
            r4 = 0
            if (r2 >= 0) goto L_0x00ac
            java.lang.Object r0 = r12.get(r8)
            com.ss.android.ugc.aweme.feed.model.Aweme r0 = (com.ss.android.ugc.aweme.feed.model.Aweme) r0
            r6 = 0
        L_0x0068:
            boolean r1 = r10.LIZLLL(r11)
            if (r1 == 0) goto L_0x009d
            java.util.List r1 = r10.LIZIZ()
            int r1 = r1.size()
            if (r1 <= 0) goto L_0x009d
            java.util.Iterator r9 = r12.iterator()
        L_0x007c:
            boolean r1 = r9.hasNext()
            if (r1 == 0) goto L_0x009d
            java.lang.Object r3 = r9.next()
            int r2 = r8 + 1
            if (r8 >= 0) goto L_0x008d
            X.LeS.LIZIZ()
        L_0x008d:
            com.ss.android.ugc.aweme.feed.model.Aweme r3 = (com.ss.android.ugc.aweme.feed.model.Aweme) r3
            com.ss.android.ugc.aweme.feed.model.story.Story r1 = r3.getStory()
            if (r1 == 0) goto L_0x00aa
            boolean r1 = r1.isPublishing()
            if (r1 != r5) goto L_0x00aa
            r6 = r8
            r0 = r3
        L_0x009d:
            X.Ckw r1 = r10.LJ
            if (r6 >= 0) goto L_0x00a5
        L_0x00a1:
            r1.LIZ(r11, r7, r4, r0)
            return r6
        L_0x00a5:
            java.lang.Integer r4 = java.lang.Integer.valueOf(r6)
            goto L_0x00a1
        L_0x00aa:
            r8 = r2
            goto L_0x007c
        L_0x00ac:
            int r0 = r12.size()
            if (r5 <= r0) goto L_0x00c0
        L_0x00b2:
            int r0 = r12.size()
            if (r2 >= r0) goto L_0x00cf
            java.lang.Object r0 = r12.get(r2)
            com.ss.android.ugc.aweme.feed.model.Aweme r0 = (com.ss.android.ugc.aweme.feed.model.Aweme) r0
            r6 = r2
            goto L_0x0068
        L_0x00c0:
            if (r2 < r0) goto L_0x00b2
            int r0 = r12.size()
            int r6 = r0 + -1
            java.lang.Object r0 = r12.get(r6)
            com.ss.android.ugc.aweme.feed.model.Aweme r0 = (com.ss.android.ugc.aweme.feed.model.Aweme) r0
            goto L_0x0068
        L_0x00cf:
            r0 = r4
            goto L_0x0068
        */
        throw new UnsupportedOperationException("Method not decompiled: com.ss.android.ugc.aweme.story.feed.common.StoryFeedViewModel.LIZ(java.lang.String, java.util.List):int");
    }

    public final void LIZ(String str, UserStory userStory) {
        if (LIZLLL(str)) {
            userStory.setTotalCount((userStory.getOriginTotalCount() + ((long) LIZIZ().size())) - ((long) ClK.LJIIIIZZ.LIZ(userStory, true)));
            LIZJ(str);
        }
    }

    private final void LIZ(String str, long j, boolean z) {
        boolean z2 = z;
        String str2 = str;
        LIZ(str2, z2);
        2cN LIZ = StoryApi.LIZ.getUserStory(str2, j, z2, 5).LIZIZ(PhE.LIZIZ(PlI.LIZJ)).LIZ(Mk2.LIZ(Mk3.LIZ)).LIZ(new Cl1(this, str2, z2), new C0119Ckp(this, str2));
        n.LIZIZ(LIZ, "");
        4Dd.LIZ(LIZ, this.LJIIJ);
    }

    public final void LIZ(String str, int i, List<? extends Aweme> list) {
        UserStory userStory;
        M12.LIZ(str, list);
        Aweme aweme = this.LIZJ.get(str);
        if (aweme != null && (userStory = aweme.getUserStory()) != null) {
            n.LIZIZ(userStory, "");
            List<C0128Cky> list2 = this.LIZLLL.get(str);
            if (list2 == null) {
                list2 = LIZJ(str);
            }
            if (userStory.getCurrentPosition() + ((long) userStory.getStories().size()) < userStory.getTotalCount() && ((long) (i + 1)) >= userStory.getCurrentPosition() + ((long) userStory.getStories().size()) && userStory.getHasMoreAfter() && ((int) userStory.getCurrentPosition()) + userStory.getStories().size() < list2.size() && list2.get(((int) userStory.getCurrentPosition()) + userStory.getStories().size()).LIZIZ == C0129Ckz.UNLOADING) {
                LIZ(str, userStory.getMaxCursor(), false);
            }
            if (userStory.getCurrentPosition() != 0 && ((long) (i - 1)) <= userStory.getCurrentPosition() && userStory.getHasMoreBefore() && ((int) userStory.getCurrentPosition()) - 1 < list2.size() && list2.get(((int) userStory.getCurrentPosition()) - 1).LIZIZ == C0129Ckz.UNLOADING) {
                LIZ(str, userStory.getMinCursor(), true);
            }
        }
    }
}
