package ve;

import android.app.Activity;
import android.os.Handler;
import android.os.Looper;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.gms.measurement.internal.u1;
import ie.i3;
import ie.j3;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
import l.u2;
import xyz.stream.api.entity.LiveCategoryEntity;
import xyz.stream.api.entity.LiveChannelEntity;
import xyz.stream.api.entity.LiveEpgEntity;
import xyz.stream.api.viewmodel.LiveViewModel;
import xyz.stream.utils.f0;
import xyz.stream.view.SpacesItemVerDecoration;
import xyz.stream.view.UiUtils;
import xyz.stream.view.tvrecyclerview.TvRecyclerView;

/* JADX INFO: loaded from: classes3.dex */
public final class p {
    public e0 A;
    public j F;
    public androidx.lifecycle.x G;

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public TvRecyclerView f33884a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public TvRecyclerView f33885b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public WeakReference f33886c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public LiveViewModel f33887d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public ie.j f33888e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public List f33889f;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public we.b f33893j;

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    public we.c f33895l;

    /* JADX INFO: renamed from: p, reason: collision with root package name */
    public i3 f33899p;

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    public j3 f33900q;

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    public List f33904u;

    /* JADX INFO: renamed from: x, reason: collision with root package name */
    public we.e f33907x;

    /* JADX INFO: renamed from: y, reason: collision with root package name */
    public xe.a f33908y;

    /* JADX INFO: renamed from: z, reason: collision with root package name */
    public LiveChannelEntity f33909z;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final ArrayList f33890g = new ArrayList();

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public final ArrayList f33891h = new ArrayList();

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public int f33892i = 0;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public int f33894k = 0;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public final ArrayList f33896m = new ArrayList();

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    public final ArrayList f33897n = new ArrayList();

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    public final ArrayList f33898o = new ArrayList();

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    public final Handler f33901r = new Handler(Looper.getMainLooper());

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    public int f33902s = -1000;

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    public int f33903t = -4;

    /* JADX INFO: renamed from: v, reason: collision with root package name */
    public final ArrayList f33905v = new ArrayList();

    /* JADX INFO: renamed from: w, reason: collision with root package name */
    public final ArrayList f33906w = new ArrayList();
    public int B = -4;
    public boolean C = false;
    public boolean D = false;
    public final ArrayList E = new ArrayList();
    public int H = Integer.MIN_VALUE;
    public boolean I = false;
    public int J = -1;
    public long K = 0;

    public static void a(p pVar, int i10, LiveEpgEntity liveEpgEntity) {
        WeakReference weakReference = pVar.f33886c;
        AppCompatActivity appCompatActivity = weakReference != null ? (AppCompatActivity) weakReference.get() : null;
        if (pVar.i(appCompatActivity)) {
            pVar.f33901r.post(new b7.e(i10, 3, pVar, appCompatActivity, liveEpgEntity));
        }
    }

    public static ArrayList h(List list, int... iArr) {
        if (list == null || list.isEmpty()) {
            return new ArrayList();
        }
        int i10 = 0;
        int i11 = iArr.length > 0 ? iArr[0] : 10;
        ArrayList arrayList = new ArrayList();
        while (i10 < list.size()) {
            int i12 = i10 + i11;
            arrayList.add(new ArrayList(list.subList(i10, Math.min(i12, list.size()))));
            i10 = i12;
        }
        return arrayList;
    }

    public final void b(String str) {
        ArrayList arrayListM = f0.o().m(String.class, "sp_search_history_live");
        arrayListM.remove(str);
        arrayListM.add(0, str);
        int size = arrayListM.size();
        List listSubList = arrayListM;
        if (size > 5) {
            listSubList = arrayListM.subList(0, 5);
        }
        f0.o().C("sp_search_history_live", listSubList);
        ArrayList arrayList = this.f33906w;
        arrayList.clear();
        arrayList.addAll(listSubList.subList(0, Math.min(listSubList.size(), 3)));
        this.f33907x.notifyDataSetChanged();
    }

    public final void c(int i10, boolean z10) {
        LiveViewModel liveViewModel;
        int i11 = 0;
        if (this.f33903t == i10 && i10 != -1 && i10 != -2) {
            this.D = false;
            return;
        }
        this.f33903t = i10;
        this.f33893j.f(i10);
        this.f33896m.clear();
        this.f33898o.clear();
        this.f33894k = 0;
        this.f33895l.notifyDataSetChanged();
        if (i10 != -3) {
            this.f33900q.f27075b.setVisibility(8);
            this.f33900q.f27078e.setVisibility(8);
            this.f33900q.f27079f.setVisibility(8);
            this.f33900q.f27080g.setVisibility(8);
            this.f33900q.f27077d.setVisibility(8);
            this.f33900q.f27076c.setVisibility(0);
            this.f33888e.f27043h.removeAllViews();
            this.f33888e.f27043h.addView(this.f33900q.f27074a);
            WeakReference weakReference = this.f33886c;
            AppCompatActivity appCompatActivity = weakReference != null ? (AppCompatActivity) weakReference.get() : null;
            if (appCompatActivity != null && (liveViewModel = this.f33887d) != null && this.F == null) {
                j jVar = new j(this, i11);
                this.F = jVar;
                liveViewModel.liveChannel.c(appCompatActivity, jVar);
            }
            this.H = i10;
            this.I = z10;
            this.J = this.f33887d.getLiveChannel(i10);
            return;
        }
        this.D = false;
        this.f33902s = 0;
        we.e eVar = this.f33907x;
        ArrayList arrayList = this.f33906w;
        if (eVar == null) {
            SpacesItemVerDecoration spacesItemVerDecoration = new SpacesItemVerDecoration(UiUtils.dp2px(10.0f), 0, 0);
            this.f33900q.f27077d.setSelectedItemAtCentered(true);
            this.f33900q.f27077d.setAnimation(null);
            this.f33900q.f27077d.addItemDecoration(spacesItemVerDecoration);
            we.e eVar2 = new we.e(arrayList, (Activity) this.f33886c.get());
            this.f33907x = eVar2;
            this.f33900q.f27077d.setAdapter(eVar2);
            this.f33907x.setOnItemListener(new n(this, 1));
            int i12 = 2;
            this.f33900q.f27075b.setOnKeyListener(new oe.d(this, i12));
            this.f33900q.f27075b.addTextChangedListener(new u2(this, 2));
            this.f33900q.f27075b.setOnEditorActionListener(new i(this, i11));
            this.f33900q.f27075b.setOnFocusChangeListener(new com.google.android.material.textfield.c(this, i12));
        }
        this.f33900q.f27075b.setVisibility(0);
        this.f33900q.f27078e.setVisibility(8);
        this.f33900q.f27079f.setVisibility(8);
        this.f33900q.f27076c.setVisibility(8);
        ArrayList arrayListM = f0.o().m(String.class, "sp_search_history_live");
        if (arrayListM.isEmpty()) {
            this.f33900q.f27078e.setVisibility(8);
            this.f33900q.f27077d.setVisibility(8);
            return;
        }
        arrayList.clear();
        arrayList.addAll(arrayListM.subList(0, Math.min(arrayListM.size(), 5)));
        this.f33900q.f27078e.setVisibility(0);
        this.f33900q.f27077d.setVisibility(0);
        this.f33907x.notifyDataSetChanged();
    }

    public final void d() {
        xyz.netfly.ui.live.epg.q.o().c();
        int i10 = 0;
        while (true) {
            ArrayList arrayList = this.f33898o;
            if (i10 >= arrayList.size()) {
                return;
            }
            int iIntValue = ((LiveChannelEntity) arrayList.get(i10)).getId().intValue();
            xyz.netfly.ui.live.epg.q.o().n(iIntValue, new te.m(iIntValue, 1, this));
            i10++;
        }
    }

    public final void e() {
        ArrayList arrayList;
        if (!this.f33905v.isEmpty()) {
            c(-2, false);
            UiUtils.dismissProgressDialog();
            return;
        }
        if (this.f33891h.size() < 4) {
            return;
        }
        LiveCategoryEntity liveCategoryEntity = null;
        int i10 = 0;
        while (true) {
            arrayList = this.f33890g;
            if (i10 >= arrayList.size()) {
                break;
            }
            int i11 = 0;
            while (true) {
                if (i11 < ((List) arrayList.get(i10)).size()) {
                    if (((LiveCategoryEntity) ((List) arrayList.get(i10)).get(i11)).getCategoryId().intValue() != -1 && ((LiveCategoryEntity) ((List) arrayList.get(i10)).get(i11)).getCategoryId().intValue() != -2 && ((LiveCategoryEntity) ((List) arrayList.get(i10)).get(i11)).getCategoryId().intValue() != -3 && ((LiveCategoryEntity) ((List) arrayList.get(i10)).get(i11)).getType() != 1) {
                        liveCategoryEntity = (LiveCategoryEntity) ((List) arrayList.get(i10)).get(i11);
                        break;
                    }
                    i11++;
                } else {
                    break;
                }
            }
            if (liveCategoryEntity != null) {
                break;
            } else {
                i10++;
            }
        }
        if (liveCategoryEntity == null) {
            int size = this.f33904u.isEmpty() ? 2 : 3;
            if (size >= ((List) arrayList.get(0)).size()) {
                size = ((List) arrayList.get(0)).size() - 1;
            }
            liveCategoryEntity = (LiveCategoryEntity) ((List) arrayList.get(0)).get(size);
        }
        c(liveCategoryEntity.getCategoryId().intValue(), false);
    }

    public final LiveChannelEntity f() {
        LiveChannelEntity liveChannelEntity;
        ArrayList arrayList = this.f33897n;
        if (arrayList.isEmpty()) {
            return null;
        }
        for (int i10 = 0; i10 < arrayList.size(); i10++) {
            List list = (List) arrayList.get(i10);
            if (!list.isEmpty() && (liveChannelEntity = (LiveChannelEntity) t4.f.x(list).e(new h(this, 0)).k()) != null) {
                int iIndexOf = list.indexOf(liveChannelEntity);
                if (iIndexOf < list.size() - 1) {
                    int i11 = iIndexOf + 1;
                    this.f33909z = (LiveChannelEntity) list.get(i11);
                    return (LiveChannelEntity) list.get(i11);
                }
                if (i10 < arrayList.size() - 1) {
                    List list2 = (List) arrayList.get(i10 + 1);
                    if (!list2.isEmpty()) {
                        this.f33909z = (LiveChannelEntity) list2.get(0);
                        return (LiveChannelEntity) list2.get(0);
                    }
                } else {
                    List list3 = (List) arrayList.get(0);
                    if (!list3.isEmpty()) {
                        this.f33909z = (LiveChannelEntity) list3.get(0);
                        return (LiveChannelEntity) list3.get(0);
                    }
                }
            }
        }
        return null;
    }

    public final LiveChannelEntity g() {
        LiveChannelEntity liveChannelEntity;
        ArrayList arrayList = this.f33897n;
        if (arrayList.isEmpty()) {
            return null;
        }
        for (int i10 = 0; i10 < arrayList.size(); i10++) {
            List list = (List) arrayList.get(i10);
            if (!list.isEmpty() && (liveChannelEntity = (LiveChannelEntity) t4.f.x(list).e(new h(this, 1)).k()) != null) {
                int iIndexOf = list.indexOf(liveChannelEntity);
                if (iIndexOf > 0) {
                    int i11 = iIndexOf - 1;
                    this.f33909z = (LiveChannelEntity) list.get(i11);
                    return (LiveChannelEntity) list.get(i11);
                }
                if (i10 > 0) {
                    List list2 = (List) arrayList.get(i10 - 1);
                    if (!list2.isEmpty()) {
                        this.f33909z = (LiveChannelEntity) defpackage.e.c(list2, 1);
                        return (LiveChannelEntity) defpackage.e.c(list2, 1);
                    }
                } else {
                    List list3 = (List) u1.e(arrayList, 1);
                    if (!list3.isEmpty()) {
                        this.f33909z = (LiveChannelEntity) defpackage.e.c(list3, 1);
                        return (LiveChannelEntity) defpackage.e.c(list3, 1);
                    }
                }
            }
        }
        return null;
    }

    public final boolean i(AppCompatActivity appCompatActivity) {
        WeakReference weakReference;
        return (appCompatActivity == null || (weakReference = this.f33886c) == null || weakReference.get() != appCompatActivity || this.f33888e == null || appCompatActivity.isFinishing() || appCompatActivity.isDestroyed()) ? false : true;
    }

    public final void j() {
        LiveViewModel liveViewModel;
        String string = this.f33900q.f27075b.getText().toString();
        if (string.isEmpty()) {
            return;
        }
        this.f33900q.f27078e.setVisibility(8);
        this.f33900q.f27077d.setVisibility(8);
        this.f33887d.searchLiveChannel(string);
        j jVar = new j(this, 1);
        WeakReference weakReference = this.f33886c;
        AppCompatActivity appCompatActivity = weakReference != null ? (AppCompatActivity) weakReference.get() : null;
        if (appCompatActivity == null || (liveViewModel = this.f33887d) == null) {
            return;
        }
        androidx.lifecycle.x xVar = this.G;
        if (xVar != null) {
            liveViewModel.liveSearch.g(xVar);
        }
        this.G = jVar;
        this.f33887d.liveSearch.c(appCompatActivity, jVar);
    }
}
