package com.ss.android.ugc.aweme.notification.module;

import X.2HN;
import X.2cN;
import X.2ce;
import X.2kR;
import X.3K7;
import X.3PM;
import X.4Dd;
import X.7Ze;
import X.7he;
import X.C0295Crj;
import X.C3426EDu;
import X.C3493EGj;
import X.C3495EGl;
import X.C3504EGu;
import X.C3506EGw;
import X.C3507EGx;
import X.C3508EGy;
import X.Cdw;
import X.CeH;
import X.EH0;
import X.EH2;
import X.EH3;
import X.EH4;
import X.EH6;
import X.EH8;
import X.EHD;
import X.EHE;
import X.EHF;
import X.EHH;
import X.Lel;
import X.M12;
import X.Mk2;
import X.Mk3;
import X.PhE;
import X.PlI;
import com.bytedance.covode.number.Covode;
import com.bytedance.frameworks.apm.trace.MethodCollector;
import com.ss.android.ugc.aweme.notice.repo.list.bean.MusNotice;
import com.ss.android.ugc.aweme.notice.repo.list.bean.NoticeItems;
import com.ss.android.ugc.aweme.notice.repo.list.bean.NoticeListsResponse;
import com.ss.android.ugc.aweme.notification.api.NotificationApi;
import com.ss.android.ugc.aweme.notification.vm.BaseNotificationVM;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.ListIterator;
import kotlin.h.b.n;

public final class NotificationChunkVM extends BaseNotificationVM implements 3K7 {
    public static final EHF LJFF = new EHF((byte) 0);
    public EH0 LIZ = EH0.UNKNOWN;
    public volatile C3495EGl LIZIZ = new C3495EGl();
    public final 7Ze LIZJ;
    public final CeH LIZLLL;
    public final 2ce<Cdw> LJ;
    public final C0295Crj LJIIJ = 7he.LIZ(EH2.LIZ);
    public final C0295Crj LJIIJJI = 7he.LIZ(EH6.LIZ);
    public final C0295Crj LJIIL = 7he.LIZ(C3493EGj.LIZ);

    static {
        Covode.recordClassIndex(95618);
    }

    public final 2ce<List<EHE>> LIZ() {
        return (2ce) this.LJIIJ.getValue();
    }

    public final 2ce<Boolean> LIZIZ() {
        return (2ce) this.LJIIJJI.getValue();
    }

    public final 2ce<C3426EDu> LIZJ() {
        return (2ce) this.LJIIL.getValue();
    }

    private final boolean LJIILLIIL() {
        if (LIZJ().getValue() == C3426EDu.LOADING) {
            return true;
        }
        return false;
    }

    public final void LIZLLL() {
        LJIIL();
        LIZ(Cdw.LOADING);
        3PM.LIZIZ("NotificationChunkVM", LIZ("refresh start"));
        NotificationApi.Api LIZ2 = NotificationApi.LIZ.LIZ();
        2kR r2 = new 2kR(this.LIZJ.LIZ, 0, 0, 0, 0, 30, (2HN) null);
        r2.mayWithMergedTTShopArg(this.LIZJ.LJ);
        2cN LIZ3 = LIZ2.fetchGroupNotice(r2.toReqStr(), 0).LIZIZ(PhE.LIZIZ(PlI.LIZJ)).LIZLLL(new EH4(this)).LIZ(Mk2.LIZ(Mk3.LIZ)).LIZ(new C3504EGu(this), new C3507EGx(this));
        n.LIZIZ(LIZ3, "");
        4Dd.LIZ(LIZ3, LJFF());
    }

    public final void LJ() {
        if (this.LIZ != EH0.EXPAND || LJIILLIIL() || !this.LIZIZ.LIZLLL || this.LIZIZ.LJ <= 0) {
            3PM.LIZJ("NotificationChunkVM", LIZ("loadMore invalid:" + this.LIZ + ", " + LJIILLIIL()));
            return;
        }
        LIZJ().setValue(C3426EDu.LOADING);
        3PM.LIZIZ("NotificationChunkVM", LIZ("loadMore start"));
        NotificationApi.Api LIZ2 = NotificationApi.LIZ.LIZ();
        2kR r2 = new 2kR(this.LIZJ.LIZ, this.LIZIZ.LJ, this.LIZIZ.LJFF, 0, 0, 24, (2HN) null);
        r2.mayWithMergedTTShopArg(this.LIZJ.LJ);
        2cN LIZ3 = LIZ2.fetchGroupNotice(r2.toReqStr(), 0).LIZLLL(new EH3(this)).LIZIZ(PhE.LIZIZ(PlI.LIZJ)).LIZ(Mk2.LIZ(Mk3.LIZ)).LIZ(new C3506EGw(this), new C3508EGy(this));
        n.LIZIZ(LIZ3, "");
        4Dd.LIZ(LIZ3, LJFF());
    }

    public final void LIZ(Cdw cdw) {
        this.LJ.setValue(cdw);
    }

    public final void LIZ(EH0 eh0) {
        List<EHE> list;
        this.LIZ = eh0;
        2ce<List<EHE>> LIZ2 = LIZ();
        if (eh0 == EH0.COLLAPSE) {
            list = this.LIZIZ.LIZIZ;
        } else {
            list = this.LIZIZ.LIZ;
        }
        LIZ2.setValue(list);
    }

    public final String LIZ(String str) {
        return str + ", style:" + this.LIZ + ", data:" + this.LIZIZ;
    }

    public final synchronized void LIZ(NoticeListsResponse noticeListsResponse, boolean z) {
        boolean z2;
        EH8 eh8;
        List list;
        MethodCollector.i(7702);
        List<NoticeItems> notices = noticeListsResponse.getNotices();
        T t = null;
        int i = 1;
        if (notices != null) {
            Iterator<T> it = notices.iterator();
            while (true) {
                if (!it.hasNext()) {
                    break;
                }
                T next = it.next();
                if (((NoticeItems) next).getGroup() == this.LIZJ.LIZ) {
                    t = next;
                    break;
                }
            }
            NoticeItems noticeItems = (NoticeItems) t;
            if (noticeItems != null) {
                C3495EGl eGl = new C3495EGl();
                eGl.LIZLLL = noticeItems.getHasMore();
                eGl.LJ = noticeItems.getMaxTime();
                eGl.LJFF = noticeItems.getMinTime();
                if (!z) {
                    eGl.LIZJ.addAll(this.LIZIZ.LIZJ);
                }
                List<MusNotice> list2 = eGl.LIZJ;
                Collection items = noticeItems.getItems();
                if (items == null) {
                    items = Lel.INSTANCE;
                }
                list2.addAll(items);
                List<MusNotice> list3 = eGl.LIZJ;
                LinkedHashMap linkedHashMap = new LinkedHashMap();
                ListIterator<MusNotice> listIterator = list3.listIterator();
                int i2 = 1;
                while (listIterator.hasNext()) {
                    MusNotice next2 = listIterator.next();
                    if (next2.templateNotice == null) {
                        3PM.LIZJ("NotificationChunkVM", "Only support template type:" + next2.nid + ", " + next2.type);
                        listIterator.remove();
                    }
                    if (((MusNotice) linkedHashMap.get(Long.valueOf(next2.getUniqueId()))) != null) {
                        next2.hackUniqueId(next2.getUniqueId() + ((long) i2));
                        i2 = (i2 * 31) + 1;
                    }
                    linkedHashMap.put(Long.valueOf(next2.getUniqueId()), next2);
                }
                if (eGl.LIZJ.isEmpty()) {
                    this.LIZIZ = eGl;
                    3PM.LIZJ("NotificationChunkVM", LIZ("processResp empty, isRefresh:".concat(String.valueOf(z))));
                    MethodCollector.o(7702);
                    return;
                }
                if (eGl.LIZJ.size() > 2) {
                    z2 = true;
                } else {
                    z2 = false;
                }
                List<EHE> list4 = eGl.LIZIZ;
                if (z2) {
                    String str = this.LIZJ.LIZJ;
                    String str2 = this.LIZJ.LIZLLL;
                    eh8 = new EH8(str, str2, "aweme://notice/detail?title=" + this.LIZJ.LIZJ + "&from_where=" + this.LIZJ.LIZ + "&ec_merged_tiktok_shop=" + this.LIZJ.LJ);
                } else {
                    eh8 = new EH8(this.LIZJ.LIZJ);
                }
                list4.add(eh8);
                eGl.LIZ.add(new EH8(this.LIZJ.LIZJ));
                ArrayList arrayList = new ArrayList();
                for (MusNotice ehd : eGl.LIZJ) {
                    arrayList.add(new EHD(ehd, this.LIZJ.LIZIZ, i));
                    i++;
                }
                List<EHE> list5 = eGl.LIZIZ;
                if (z2) {
                    list = arrayList.subList(0, 2);
                } else {
                    list = arrayList;
                }
                list5.addAll(list);
                eGl.LIZ.addAll(arrayList);
                if (eGl.LIZLLL) {
                    eGl.LIZ.add(new EHH(i));
                }
                this.LIZIZ = eGl;
                MethodCollector.o(7702);
                return;
            }
        }
        3PM.LIZJ("NotificationChunkVM", LIZ("processResp invalid, isRefresh:".concat(String.valueOf(z))));
        MethodCollector.o(7702);
    }

    public NotificationChunkVM(7Ze r2, CeH ceH, 2ce<Cdw> r4) {
        M12.LIZ(r2, ceH, r4);
        this.LIZJ = r2;
        this.LIZLLL = ceH;
        this.LJ = r4;
    }
}
