package com.bytedance.android.livesdk.list;

import X.0CB;
import X.0CD;
import X.0CH;
import X.0CN;
import X.0cm;
import X.6Is;
import X.C3023Glc;
import X.C3245GpC;
import X.C3296Gq1;
import X.C3456Gsb;
import X.C3464Gsj;
import X.C3477Gsw;
import X.C3588Guj;
import X.C3589Guk;
import X.GRC;
import X.Mjy;
import X.Mk2;
import X.Mk3;
import android.text.TextUtils;
import com.bytedance.android.live.base.model.feed.FeedExtra;
import com.bytedance.android.livesdk.live.data.DrawRoomListModel;
import com.bytedance.android.livesdk.live.model.LiveFeedDraw;
import com.bytedance.android.livesdk.model.FeedItem;
import com.bytedance.android.livesdkapi.depend.model.live.Room;
import com.bytedance.android.livesdkapi.depend.model.live.RoomInfo;
import com.bytedance.android.livesdkapi.host.IHostContext;
import com.bytedance.android.livesdkapi.session.EnterRoomConfig;
import com.bytedance.covode.number.Covode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class MultiPlusFeedRoomListProvider extends C3464Gsj implements 6Is {
    public List<Room> LIZ = new ArrayList();
    public List<EnterRoomConfig> LIZIZ = new ArrayList();
    public EnterRoomConfig LIZJ;
    public boolean LIZLLL;
    public boolean LJ;
    public FeedExtra LJFF;
    public final boolean LJI;
    public final String LJII;
    public final String LJIIIIZZ;
    public final Long LJIIIZ;
    public final List<Room> LJIIJ = new ArrayList();
    public List<EnterRoomConfig> LJIIJJI = new ArrayList();
    public String LJIIL;
    public HashSet<Long> LJIILIIL = new HashSet<>();
    public final long[] LJIILJJIL;
    public List<RoomInfo> LJIILL = new ArrayList();
    public HashMap<Long, String> LJIILLIIL = new HashMap<>();
    public DrawRoomListModel LJIIZILJ;
    public final Boolean LJIJ;
    public final int LJIJI;
    public final Mjy LJIJJ = new Mjy();
    public int LJIJJLI;
    public int LJIL;

    static {
        Covode.recordClassIndex(17534);
    }

    public final boolean LJ() {
        return true;
    }

    public void onStateChanged(0CH r2, 0CB r3) {
        if (r3 == 0CB.ON_DESTROY) {
            onDestroy();
        }
    }

    public final boolean LIZLLL() {
        return this.LIZLLL;
    }

    public final List<Room> LIZIZ() {
        ArrayList arrayList = new ArrayList();
        arrayList.addAll(this.LIZ);
        arrayList.addAll(this.LJIIJ);
        return arrayList;
    }

    @0CN(LIZ = 0CB.ON_DESTROY)
    public void onDestroy() {
        this.LJIJJ.LIZ();
        C3296Gq1.LIZ.LIZLLL((String) null);
    }

    public final int LIZ() {
        return this.LJIILL.size() + this.LJIIJ.size();
    }

    public final String LIZIZ(long j) {
        String str = this.LJIILLIIL.get(Long.valueOf(j));
        if (TextUtils.isEmpty(str)) {
            return "";
        }
        return str;
    }

    private void LIZIZ(EnterRoomConfig enterRoomConfig) {
        if (enterRoomConfig != null && enterRoomConfig.LIZLLL != null && enterRoomConfig.LIZLLL.LJLL != null && 0cm.LIZ(IHostContext.class).isOffline()) {
            for (Map.Entry next : enterRoomConfig.LIZLLL.LJLL.entrySet()) {
                this.LJIILLIIL.put(next.getKey(), next.getValue());
            }
        }
    }

    private long[] LIZLLL(int i) {
        int size = this.LJIILL.size();
        long[] jArr = new long[0];
        if (i >= this.LJIL) {
            int min = Math.min(size - i, 30);
            int i2 = (i + min) - 1;
            jArr = new long[min];
            LIZ(jArr, i, i2);
            if (this.LJIJJLI == -1) {
                this.LJIJJLI = i;
            }
            this.LJIL = i2;
        } else if (i <= this.LJIJJLI) {
            int min2 = Math.min(30, i);
            int i3 = (i - min2) + 1;
            jArr = new long[min2];
            LIZ(jArr, i3, i);
            if (this.LJIL == -1) {
                this.LJIL = i;
            }
            this.LJIJJLI = i3;
        }
        return jArr;
    }

    public final Room LIZJ(int i) {
        if (i >= 0 && i < LIZ()) {
            if (i < this.LIZ.size()) {
                if (i >= this.LJIJJLI && i <= this.LJIL) {
                    return this.LIZ.get(i);
                }
                LIZ(LIZLLL(i), this.LJIJJLI);
                return null;
            } else if (this.LJIIJ != null && this.LJIIJJI.size() > i - this.LIZ.size()) {
                return this.LJIIJ.get(i - this.LIZ.size());
            }
        }
        return null;
    }

    public final int LIZ(EnterRoomConfig enterRoomConfig) {
        long j = enterRoomConfig.LIZLLL.LJJJJZI;
        for (int i = 0; i < LIZ(); i++) {
            if (i < this.LJIILL.size() && j == this.LJIILL.get(i).getRoomId()) {
                return i;
            }
            int size = i - this.LJIILL.size();
            if (size >= 0 && size < this.LJIIJ.size() && j == this.LJIIJ.get(size).getId()) {
                return i;
            }
        }
        return -1;
    }

    public final EnterRoomConfig LIZ(int i) {
        if (i < this.LJIILL.size()) {
            C3296Gq1.LIZ.LIZLLL(this.LIZJ.LIZLLL.LJJJJJ);
            if (TextUtils.equals(this.LIZJ.LIZLLL.LJJJJJ, "weekly_rank_notice")) {
                C3296Gq1.LIZ.LIZLLL(C3245GpC.WEEKLY_RANK.getRankName());
            }
        } else {
            C3296Gq1.LIZ.LIZLLL("live_merge_content");
        }
        EnterRoomConfig enterRoomConfig = new EnterRoomConfig();
        if (i >= 0 && i < LIZ()) {
            if (i < this.LJIILL.size()) {
                if (i >= this.LIZIZ.size() || this.LIZIZ.get(i) == null) {
                    enterRoomConfig.LIZLLL.LJJJJZI = this.LJIILL.get(i).getRoomId();
                    enterRoomConfig.LIZJ.LIZIZ = this.LJIILL.get(i).getAnchorId();
                } else {
                    enterRoomConfig = this.LIZIZ.get(i);
                }
                if (this.LIZJ.LIZLLL.LJJJJZI > 0 && this.LJIILL.get(i) != null && this.LIZJ.LIZLLL.LJJJJZI == this.LJIILL.get(i).getRoomId()) {
                    enterRoomConfig = this.LIZJ;
                }
            } else if (i < LIZ()) {
                if (i < this.LJIILL.size() + this.LJIIJJI.size()) {
                    enterRoomConfig = this.LJIIJJI.get(i - this.LJIILL.size());
                } else {
                    C3023Glc.LIZ(this.LJIIJ.get(i - this.LJIILL.size()), enterRoomConfig);
                    this.LJIIJJI.add(enterRoomConfig);
                }
                enterRoomConfig.LIZLLL.LJJJJI = this.LIZJ.LIZLLL.LJJJJJ;
                enterRoomConfig.LIZLLL.LJJJJJ = "draw_loadmore";
            }
            EnterRoomConfig enterRoomConfig2 = this.LIZJ;
            if (enterRoomConfig2 != null) {
                if (!TextUtils.isEmpty(enterRoomConfig2.LIZJ.LIZ)) {
                    enterRoomConfig.LIZJ.LIZ = this.LIZJ.LIZJ.LIZ;
                }
                if (!TextUtils.isEmpty(this.LIZJ.LIZJ.LJI)) {
                    enterRoomConfig.LIZJ.LJI = this.LIZJ.LIZJ.LJI;
                }
            }
            enterRoomConfig.LIZLLL.LJJJJLI = this.LJI;
            enterRoomConfig.LIZLLL.LJLJL = this.LJIJ.booleanValue();
            enterRoomConfig.LIZLLL.LJLJLLL = this.LJIJI;
        }
        return enterRoomConfig;
    }

    public final void LIZIZ(int i) {
        String str;
        long j;
        if (!this.LIZLLL) {
            EnterRoomConfig enterRoomConfig = this.LIZJ;
            if (enterRoomConfig == null || !"hourly_rank".equals(enterRoomConfig.LIZLLL.LJJJJJ) || !this.LIZJ.LIZLLL.LJJJJIZL) {
                if (this.LJIIZILJ == null) {
                    this.LJIIZILJ = new DrawRoomListModel();
                }
                if (TextUtils.equals(this.LIZJ.LIZLLL.LJJJJJ, C3245GpC.WEEKLY_RANK.getRankName()) || TextUtils.equals(this.LIZJ.LIZLLL.LJJJJJ, "weekly_rank_notice")) {
                    str = "weekly_explore";
                } else if (TextUtils.equals(this.LIZJ.LIZLLL.LJJJJJ, C3245GpC.WEEKLY_RISING_RANK.getRankName())) {
                    str = "weekly_rising_explore";
                } else if (TextUtils.equals(this.LIZJ.LIZLLL.LJJJJJ, C3245GpC.WEEKLY_ROOKIE_RANK.getRankName())) {
                    str = "weekly_rookie_explore";
                } else {
                    str = "hourly_explore";
                }
                if (!TextUtils.isEmpty(this.LJIIL) && this.LJIIL.contains("/webcast/feed/") && 0cm.LIZ(IHostContext.class).isOffline() && !this.LJIIL.contains("&debug=true")) {
                    this.LJIIL += "&debug=true";
                }
                long currentTimeMillis = System.currentTimeMillis();
                DrawRoomListModel drawRoomListModel = this.LJIIZILJ;
                FeedExtra feedExtra = this.LJFF;
                if (feedExtra == null) {
                    j = 0;
                } else {
                    j = feedExtra.LIZLLL;
                }
                this.LJIJJ.LIZ(drawRoomListModel.getRoomList(j, str + "_draw_loadmore", LiveFeedDraw.getChannelId(str, "draw_loadmore"), this.LIZJ.LIZLLL.LJJJJZI, Long.parseLong(this.LIZJ.LIZLLL.LJJIJIIJI), this.LJIIL).LIZ(Mk2.LIZ(Mk3.LIZ)).LIZ(new C3456Gsb(this, currentTimeMillis), new C3589Guk(this)));
            }
        }
    }

    public final void LIZ(long j) {
        Iterator<Room> it = this.LIZ.iterator();
        while (it.hasNext()) {
            Room next = it.next();
            if (next == null || next.getId() == j) {
                it.remove();
            }
        }
        Iterator<EnterRoomConfig> it2 = this.LIZIZ.iterator();
        while (it2.hasNext()) {
            EnterRoomConfig next2 = it2.next();
            if (next2 != null && next2.LIZLLL.LJJJJZI == j) {
                it2.remove();
            }
        }
        Iterator<RoomInfo> it3 = this.LJIILL.iterator();
        while (it3.hasNext()) {
            RoomInfo next3 = it3.next();
            if (next3 == null || next3.getRoomId() == j) {
                it3.remove();
            }
        }
        Iterator<Room> it4 = this.LJIIJ.iterator();
        while (it4.hasNext()) {
            Room next4 = it4.next();
            if (next4 == null || next4.getId() == j) {
                it4.remove();
            }
        }
        Iterator<EnterRoomConfig> it5 = this.LJIIJJI.iterator();
        while (it5.hasNext()) {
            EnterRoomConfig next5 = it5.next();
            if (next5 == null || next5.LIZLLL.LJJJJZI == j) {
                it5.remove();
            }
        }
        this.LJIILIIL.remove(Long.valueOf(j));
        LJII();
    }

    public final void LIZ(List<FeedItem> list) {
        if (list != null && 0cm.LIZ(IHostContext.class).isOffline()) {
            for (int i = 0; i < list.size(); i++) {
                FeedItem feedItem = list.get(i);
                if (!(feedItem == null || feedItem.getRoom() == null)) {
                    this.LJIILLIIL.put(Long.valueOf(feedItem.getRoom().getId()), feedItem.debugInfo);
                }
            }
        }
    }

    private void LIZ(List<?> list, int i) {
        for (int i2 = 0; i2 < i; i2++) {
            list.add((Object) null);
        }
    }

    private void LIZ(long[] jArr, int i) {
        if (jArr != null && jArr.length != 0 && !this.LJ) {
            this.LJ = true;
            this.LJIJJ.LIZ(GRC.LIZ.LIZ(jArr).LIZ(Mk2.LIZ(Mk3.LIZ)).LIZ(new C3477Gsw(this, i, jArr), new C3588Guj(this)));
        }
    }

    private void LIZ(long[] jArr, int i, int i2) {
        for (int i3 = i; i3 <= i2; i3++) {
            jArr[i3 - i] = this.LJIILJJIL[i3];
        }
    }

    public MultiPlusFeedRoomListProvider(0CD r5, List<RoomInfo> list, int i, EnterRoomConfig enterRoomConfig, String str) {
        this.LIZJ = enterRoomConfig;
        this.LJIILL.addAll(list);
        this.LJIIL = str;
        this.LJIILJJIL = new long[list.size()];
        LIZ((List<?>) this.LIZ, list.size());
        LIZ((List<?>) this.LIZIZ, list.size());
        for (int i2 = 0; i2 < list.size(); i2++) {
            if (list.get(i2) != null) {
                this.LJIILJJIL[i2] = list.get(i2).getRoomId();
            } else {
                this.LJIILJJIL[i2] = 0;
            }
        }
        this.LJIIIZ = Long.valueOf(this.LIZJ.LIZLLL.LJJJJZI);
        this.LJI = this.LIZJ.LIZLLL.LJJJJLI;
        this.LJII = this.LIZJ.LIZLLL.LJJJJLL;
        this.LJIIIIZZ = this.LIZJ.LIZLLL.LJJJJZ;
        this.LJIJ = Boolean.valueOf(this.LIZJ.LIZLLL.LJLJL);
        this.LJIJI = this.LIZJ.LIZLLL.LJLJLLL;
        r5.LIZ(this);
        this.LJIJJLI = i;
        this.LJIL = i;
        LIZ(LIZLLL(i), i);
        LIZIZ(enterRoomConfig);
    }
}
