package xyz.netfly.ui.live.epg;

import android.app.Activity;
import android.app.Dialog;
import androidx.recyclerview.widget.LinearLayoutManager;
import ie.z4;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.Timer;
import org.drive_hunter.R;
import xyz.stream.api.entity.LiveChannelEntity;
import xyz.stream.api.entity.LiveEpgEntity;

/* JADX INFO: loaded from: classes3.dex */
public final class z {
    public static volatile z B;

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

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

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

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public Timer f35420e;

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

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public ArrayList f35425j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public ArrayList f35426k;

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    public com.google.gson.internal.a f35436u;

    /* JADX INFO: renamed from: v, reason: collision with root package name */
    public int f35437v;

    /* JADX INFO: renamed from: y, reason: collision with root package name */
    public se.c f35440y;

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

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public boolean f35422g = false;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public boolean f35423h = false;

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

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    public volatile int f35427l = 0;

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

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

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

    /* JADX INFO: renamed from: p, reason: collision with root package name */
    public final HashMap f35431p = new HashMap();

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    public final HashMap f35432q = new HashMap();

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    public final HashSet f35433r = new HashSet();

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    public final HashSet f35434s = new HashSet();

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    public String f35435t = kotlinx.coroutines.flow.h.y("yyyy-MM-dd");

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

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

    /* JADX INFO: renamed from: z, reason: collision with root package name */
    public int f35441z = 0;
    public int A = -1;

    public static z g() {
        if (B == null) {
            synchronized (z.class) {
                try {
                    if (B == null) {
                        B = new z();
                    }
                } finally {
                }
            }
        }
        return B;
    }

    public final List a() {
        ArrayList arrayList = this.f35426k;
        if (arrayList != null) {
            return arrayList;
        }
        ArrayList arrayList2 = new ArrayList();
        arrayList2.add(new LiveEpgEntity(-1, this.f35418c.getString(R.string.no_epg), "", kotlinx.coroutines.flow.h.h("yyyy-MM-dd'T'HH:mm:ss", kotlinx.coroutines.y.D(this.f35435t).getTime()), kotlinx.coroutines.y.v(this.f35435t)));
        this.f35426k = arrayList2;
        return arrayList2;
    }

    public final void b() {
        this.f35427l++;
        q.o().b();
        Dialog dialog = this.f35421f;
        if (dialog == null || !dialog.isShowing()) {
            return;
        }
        this.f35421f.dismiss();
    }

    public final int c(Integer num) {
        ArrayList arrayList = this.f35417b;
        if (num != null) {
            for (int i10 = 0; i10 < arrayList.size(); i10++) {
                LiveChannelEntity liveChannelEntity = (LiveChannelEntity) arrayList.get(i10);
                if (liveChannelEntity != null && liveChannelEntity.getId() != null && liveChannelEntity.getId().equals(num)) {
                    return i10;
                }
            }
        }
        return arrayList.isEmpty() ? -1 : 0;
    }

    public final void d(int i10, int i11, int i12) {
        if (k(i10, i11, i12)) {
            this.f35433r.add(Integer.valueOf(i10));
            this.f35429n.remove(Integer.valueOf(i10));
            this.f35430o.remove(Integer.valueOf(i10));
            this.f35431p.remove(Integer.valueOf(i10));
            this.f35432q.remove(Integer.valueOf(i10));
        }
    }

    public final void e() {
        if (this.f35419d == null || this.f35438w.isEmpty()) {
            return;
        }
        int iH = h();
        this.f35441z = iH;
        this.f35419d.f27804e.scrollToPosition(iH);
        this.f35419d.f27804e.post(new t(this, iH, 0));
    }

    public final void f(int i10, com.google.gson.internal.a aVar) {
        this.f35428m++;
        this.f35429n.clear();
        this.f35430o.clear();
        this.f35431p.clear();
        this.f35432q.clear();
        this.f35433r.clear();
        this.f35434s.clear();
        this.f35425j = null;
        this.f35426k = null;
        ArrayList arrayList = this.f35424i;
        arrayList.clear();
        for (int i11 = 0; i11 < this.f35417b.size(); i11++) {
            ArrayList arrayList2 = this.f35425j;
            if (arrayList2 == null) {
                arrayList2 = new ArrayList();
                arrayList2.add(new LiveEpgEntity(-2, "", "", kotlinx.coroutines.flow.h.h("yyyy-MM-dd'T'HH:mm:ss", kotlinx.coroutines.y.D(this.f35435t).getTime()), kotlinx.coroutines.y.v(this.f35435t)));
                this.f35425j = arrayList2;
            }
            arrayList.add(arrayList2);
        }
        this.f35418c.runOnUiThread(new androidx.activity.n(this, i10, aVar, 14));
    }

    public final int h() {
        int i10 = 0;
        while (true) {
            ArrayList arrayList = this.f35438w;
            if (i10 >= arrayList.size()) {
                return Math.max(0, Math.min(this.f35441z, arrayList.size() - 1));
            }
            if (((String) arrayList.get(i10)).equals(this.f35435t)) {
                return i10;
            }
            i10++;
        }
    }

    public final void i(int i10, com.google.gson.internal.a aVar, int i11) {
        int i12;
        ArrayList arrayList = this.f35438w;
        arrayList.clear();
        ArrayList arrayList2 = this.f35439x;
        arrayList2.clear();
        Calendar calendar = Calendar.getInstance();
        int i13 = -3;
        int size = 0;
        while (true) {
            i12 = 3;
            if (i13 > 3) {
                break;
            }
            Calendar calendar2 = (Calendar) calendar.clone();
            calendar2.add(6, i13);
            String str = new SimpleDateFormat("yyyy-MM-dd", Locale.US).format(calendar2.getTime());
            arrayList.add(str);
            arrayList2.add(i13 == 0 ? this.f35418c.getString(R.string.today) : i13 == -1 ? this.f35418c.getString(R.string.yesterday) : i13 == 1 ? this.f35418c.getString(R.string.tomorrow) : kotlinx.coroutines.flow.h.k(str, "yyyy-MM-dd", "MMM dd"));
            if (str.equals(this.f35435t)) {
                size = arrayList.size() - 1;
            }
            i13++;
        }
        this.f35441z = size;
        se.c cVar = this.f35440y;
        if (cVar == null) {
            this.f35419d.f27804e.setLayoutManager(new LinearLayoutManager(0, false));
            this.f35419d.f27804e.setFocusable(false);
            this.f35419d.f27804e.setFocusableInTouchMode(false);
            se.c cVar2 = new se.c(arrayList2, this.f35418c);
            cVar2.f32751b = size;
            this.f35440y = cVar2;
            cVar2.setOnItemListener(new oe.g(this, i12));
            this.f35419d.f27804e.setAdapter(this.f35440y);
        } else {
            cVar.setData(arrayList2);
            this.f35440y.setSelectedPosition(size);
        }
        this.f35419d.f27804e.postDelayed(new s(this, 0), 120L);
        this.f35419d.f27803d.setVisibility(0);
        ArrayList arrayList3 = this.f35417b;
        if (i10 == -1) {
            List listL = re.a.u().l();
            if (listL == null || listL.isEmpty()) {
                this.f35419d.f27803d.setVisibility(8);
                return;
            }
            arrayList3.clear();
            arrayList3.addAll(listL);
            f(i11, aVar);
            return;
        }
        if (i10 != -2) {
            re.a aVarU = re.a.u();
            int iIntValue = this.f35416a.getCategoryId().intValue();
            w wVar = new w(i11, aVar, this);
            aVarU.getClass();
            aVarU.f35899b.execute(new androidx.activity.n(aVarU, wVar, iIntValue, 21));
            return;
        }
        List listN = re.a.u().n();
        if (listN == null || listN.isEmpty()) {
            this.f35419d.f27803d.setVisibility(8);
            return;
        }
        arrayList3.clear();
        arrayList3.addAll(listN);
        f(i11, aVar);
    }

    public final boolean j(int i10) {
        Dialog dialog;
        return (i10 != this.f35427l || this.f35418c == null || this.f35419d == null || (dialog = this.f35421f) == null || !dialog.isShowing()) ? false : true;
    }

    public final boolean k(int i10, int i11, int i12) {
        return j(i11) && this.f35429n.contains(Integer.valueOf(i10)) && i12 == this.f35428m;
    }

    public final void l(int i10, int i11, int i12, int i13) {
        if (k(i11, i12, i13)) {
            LiveChannelEntity liveChannelEntity = (LiveChannelEntity) this.f35417b.get(i10);
            if (liveChannelEntity != null && liveChannelEntity.getId() != null) {
                q.o().l(liveChannelEntity.getId().intValue(), this.f35435t, new x(this, i10, i11, i12, i13));
            } else {
                this.f35418c.runOnUiThread(new v(i11, i12, i13, i10, a(), this));
            }
        }
    }

    public final void m(Set set, int i10, int i11) {
        if (set == null || set.isEmpty() || !j(i10)) {
            return;
        }
        Iterator it = set.iterator();
        while (it.hasNext()) {
            n(((Integer) it.next()).intValue(), i10, i11);
        }
    }

    public final void n(int i10, int i11, int i12) {
        if (!j(i11) || i10 < 0) {
            return;
        }
        ArrayList arrayList = this.f35417b;
        if (i10 <= (arrayList.isEmpty() ? -1 : (arrayList.size() - 1) / 5)) {
            int i13 = i10 * 5;
            int i14 = i13 + 5;
            int iMin = Math.min(i14, arrayList.size());
            HashSet hashSet = this.f35433r;
            HashSet hashSet2 = this.f35434s;
            if (i13 >= 0 && i13 < iMin) {
                int i15 = i13;
                while (true) {
                    if (i15 >= iMin) {
                        hashSet.add(Integer.valueOf(i10));
                        break;
                    } else if (!hashSet2.contains(Integer.valueOf(i15))) {
                        break;
                    } else {
                        i15++;
                    }
                }
            }
            if (hashSet.contains(Integer.valueOf(i10))) {
                return;
            }
            HashSet hashSet3 = this.f35429n;
            if (hashSet3.contains(Integer.valueOf(i10))) {
                return;
            }
            hashSet3.add(Integer.valueOf(i10));
            int i16 = this.f35428m;
            int iMin2 = Math.min(i14, arrayList.size());
            if (k(i10, i11, i16)) {
                ArrayList arrayList2 = new ArrayList();
                while (i13 < iMin2) {
                    if (!hashSet2.contains(Integer.valueOf(i13))) {
                        arrayList2.add(Integer.valueOf(i13));
                    }
                    i13++;
                }
                int iIndexOf = arrayList2.indexOf(Integer.valueOf(i12));
                if (iIndexOf > 0) {
                    arrayList2.remove(iIndexOf);
                    arrayList2.add(0, Integer.valueOf(i12));
                }
                this.f35430o.put(Integer.valueOf(i10), Integer.valueOf(arrayList2.size()));
                if (arrayList2.isEmpty()) {
                    d(i10, i11, i16);
                    return;
                }
                int iIntValue = ((Integer) arrayList2.remove(0)).intValue();
                if (!arrayList2.isEmpty()) {
                    this.f35431p.put(Integer.valueOf(i10), arrayList2);
                }
                l(iIntValue, i10, i11, i16);
            }
        }
    }

    public final void o(int i10, int i11) {
        if (!xyz.stream.utils.e.d().f36119h && j(i11) && i10 >= 0 && i10 < this.f35417b.size()) {
            HashSet hashSet = new HashSet();
            hashSet.add(Integer.valueOf(i10 / 5));
            p(hashSet, i11, i10);
        }
    }

    public final void p(HashSet hashSet, int i10, int i11) {
        HashMap map;
        if (hashSet.isEmpty() || !j(i10)) {
            return;
        }
        HashSet hashSet2 = this.f35429n;
        Iterator it = hashSet2.iterator();
        while (true) {
            boolean zHasNext = it.hasNext();
            map = this.f35432q;
            if (!zHasNext) {
                break;
            }
            if (!hashSet.contains((Integer) it.next())) {
                hashSet2.clear();
                this.f35430o.clear();
                this.f35431p.clear();
                map.clear();
                this.f35428m++;
                q.o().b();
                break;
            }
        }
        int i12 = i11 / 5;
        HashSet hashSet3 = new HashSet(hashSet);
        hashSet3.remove(Integer.valueOf(i12));
        if (hashSet.contains(Integer.valueOf(i12)) && !hashSet3.isEmpty() && i11 >= 0 && i11 < this.f35417b.size() && !this.f35434s.contains(Integer.valueOf(i11))) {
            map.put(Integer.valueOf(i12), new y(hashSet3, i11));
            n(i12, i10, i11);
        } else {
            if (hashSet.contains(Integer.valueOf(i12))) {
                n(i12, i10, i11);
            }
            m(hashSet3, i10, i11);
        }
    }

    public final void q(int i10, String str) {
        if (str == null) {
            return;
        }
        this.f35441z = i10;
        se.c cVar = this.f35440y;
        if (cVar != null) {
            cVar.setSelectedPosition(i10);
        }
        if (str.equals(this.f35435t)) {
            e();
            return;
        }
        this.f35435t = str;
        int i11 = this.f35427l + 1;
        this.f35427l = i11;
        q.o().b();
        EPGView ePGView = this.f35419d.f27802c;
        if (ePGView.f35321g0 != null) {
            ePGView.f();
        }
        if (!this.f35417b.isEmpty()) {
            f(i11, this.f35436u);
        } else {
            this.f35419d.f27803d.setVisibility(0);
            i(this.f35437v, this.f35436u, i11);
        }
    }

    public final void r(int i10, int i11, int i12) {
        y yVar;
        if (j(i11) && i12 == this.f35428m && (yVar = (y) this.f35432q.remove(Integer.valueOf(i10))) != null) {
            Set set = yVar.f35414a;
            if (set.isEmpty()) {
                return;
            }
            m(set, i11, yVar.f35415b);
        }
    }
}
