package xyz.netfly.ui.iptv;

import android.view.KeyEvent;
import android.view.WindowManager;
import com.google.android.material.textfield.b;
import i0.o;
import ie.u;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import kotlinx.coroutines.flow.h;
import oe.g;
import org.drive_hunter.R;
import pe.m;
import pe.n;
import qe.c;
import x1.a;
import xyz.stream.api.entity.LiveCategoryEntity;
import xyz.stream.api.entity.LiveChannelEntity;
import xyz.stream.api.viewmodel.LiveViewModel;
import xyz.stream.download.config.InnerConstant;
import xyz.stream.utils.e;
import xyz.stream.view.BaseActivity;
import xyz.stream.view.NonContentActivity;
import xyz.stream.view.SpacesItemVerDecoration;
import xyz.stream.view.UiUtils;

/* JADX INFO: loaded from: classes3.dex */
public class IptvSelectChannelActivity extends BaseActivity<LiveViewModel, u> implements NonContentActivity {

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

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

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public qe.a f35306c;

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

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

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

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

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

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

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

    public static void K(IptvSelectChannelActivity iptvSelectChannelActivity, int i10, boolean z10) {
        if (i10 < 0) {
            iptvSelectChannelActivity.getClass();
            return;
        }
        if (i10 >= iptvSelectChannelActivity.f35308e.size()) {
            return;
        }
        if (iptvSelectChannelActivity.f35310g != i10 || iptvSelectChannelActivity.f35309f.isEmpty()) {
            iptvSelectChannelActivity.f35310g = i10;
            iptvSelectChannelActivity.f35306c.f(i10);
            iptvSelectChannelActivity.M(i10, z10);
        } else {
            iptvSelectChannelActivity.f35306c.f(i10);
            if (z10) {
                iptvSelectChannelActivity.L();
            }
        }
    }

    public final void L() {
        if (this.f35309f.isEmpty()) {
            return;
        }
        ((u) this.binding).f27508d.post(new o(Math.min(Math.max(this.f35313j, 0), r0.size() - 1), 8, this));
    }

    public final void M(int i10, boolean z10) {
        ArrayList arrayListG;
        ArrayList arrayList = this.f35309f;
        arrayList.clear();
        int i11 = -1;
        if (this.f35305b != null && i10 >= 0) {
            ArrayList arrayList2 = this.f35308e;
            if (i10 < arrayList2.size()) {
                Integer categoryId = ((LiveCategoryEntity) arrayList2.get(i10)).getCategoryId();
                a aVar = this.f35305b;
                int iIntValue = categoryId == null ? -100000000 : categoryId.intValue();
                if (iIntValue == -2) {
                    arrayListG = aVar.g(h.Q(h.u("iptv_recent_" + aVar.f34323a)));
                } else if (iIntValue == -1) {
                    arrayListG = aVar.g(h.Q(h.u("iptv_favorites_" + aVar.f34323a)));
                } else {
                    aVar.getClass();
                    ArrayList arrayList3 = new ArrayList();
                    for (LiveChannelEntity liveChannelEntity : (List) aVar.f34325c) {
                        if (liveChannelEntity != null && liveChannelEntity.getCategoryId() != null && liveChannelEntity.getCategoryId().intValue() == iIntValue) {
                            arrayList3.add(liveChannelEntity);
                        }
                    }
                    arrayListG = arrayList3;
                }
                arrayList.addAll(arrayListG);
            }
        }
        this.f35307d.notifyDataSetChanged();
        if (arrayList.isEmpty()) {
            ((u) this.binding).f27507c.setVisibility(0);
            ((u) this.binding).f27508d.setVisibility(8);
            return;
        }
        ((u) this.binding).f27507c.setVisibility(8);
        ((u) this.binding).f27508d.setVisibility(0);
        int i12 = this.f35311h;
        int i13 = 0;
        while (true) {
            if (i13 >= arrayList.size()) {
                break;
            }
            LiveChannelEntity liveChannelEntity2 = (LiveChannelEntity) arrayList.get(i13);
            if (liveChannelEntity2 != null && liveChannelEntity2.getId() != null && Objects.equals(liveChannelEntity2.getId(), Integer.valueOf(i12))) {
                i11 = i13;
                break;
            }
            i13++;
        }
        c cVar = this.f35307d;
        int i14 = cVar.f32289b;
        cVar.f32289b = i11;
        if (i14 >= 0 && i14 < cVar.getItemCount()) {
            cVar.notifyItemChanged(i14);
        }
        if (i11 >= 0 && i11 < cVar.getItemCount()) {
            cVar.notifyItemChanged(i11);
        }
        this.f35313j = Math.max(i11, 0);
        if (z10) {
            L();
        } else {
            ((u) this.binding).f27508d.post(new m(this, 1));
        }
    }

    @Override // xyz.stream.view.BaseActivity
    public final v3.a getViewBinding() {
        return u.inflate(getLayoutInflater(), null, false);
    }

    @Override // xyz.stream.view.BaseActivity
    public final void initView() {
        int i10 = 0;
        setRequestedOrientation(0);
        this.f35305b = a.f34322d;
        this.f35311h = getIntent().getIntExtra(InnerConstant.Db.f36069id, 0);
        this.f35312i = getIntent().getIntExtra("groupId", -100000000);
        ((u) this.binding).f27505a.setEnableLoadMore(false);
        ((u) this.binding).f27505a.setEnableRefresh(false);
        if (e.d().f36119h) {
            ((u) this.binding).f27506b.setVisibility(0);
            ((u) this.binding).f27506b.setOnClickListener(new b(this, 9));
            e.d().getClass();
            if (e.e().contains("ar")) {
                ((u) this.binding).f27506b.setScaleX(-1.0f);
            }
        } else {
            ((u) this.binding).f27506b.setVisibility(8);
        }
        SpacesItemVerDecoration spacesItemVerDecoration = new SpacesItemVerDecoration(UiUtils.dp2px(5.0f), 0, 0);
        ((u) this.binding).f27509e.setSelectedItemAtCentered(true);
        ((u) this.binding).f27509e.setAnimation(null);
        ((u) this.binding).f27509e.addItemDecoration(spacesItemVerDecoration);
        ((u) this.binding).f27508d.setSelectedItemAtCentered(true);
        ((u) this.binding).f27508d.setAnimation(null);
        ((u) this.binding).f27508d.addItemDecoration(spacesItemVerDecoration);
        this.f35306c = new qe.a(this.f35308e, this);
        this.f35307d = new c(this.f35309f, this);
        ((u) this.binding).f27509e.setAdapter(this.f35306c);
        ((u) this.binding).f27508d.setAdapter(this.f35307d);
        this.f35306c.setOnItemListener(new n(this, i10));
        this.f35307d.setOnItemListener(new g(this, 2));
        ArrayList arrayList = this.f35308e;
        arrayList.clear();
        this.f35309f.clear();
        a aVar = this.f35305b;
        if (aVar != null) {
            ArrayList arrayList2 = new ArrayList();
            if (((List) aVar.f34325c).size() > 1) {
                arrayList2.add(new LiveCategoryEntity(-2, e.d().f36112a.getString(R.string.recent), Integer.valueOf(aVar.g(h.Q(h.u("iptv_recent_" + aVar.f34323a))).size())));
                arrayList2.add(new LiveCategoryEntity(-1, e.d().f36112a.getString(R.string.favorites), Integer.valueOf(aVar.g(h.Q(h.u("iptv_favorites_" + aVar.f34323a))).size())));
            }
            for (LiveCategoryEntity liveCategoryEntity : (List) aVar.f34324b) {
                LiveCategoryEntity liveCategoryEntityClone = liveCategoryEntity.clone();
                if (liveCategoryEntity.getChannels() != null) {
                    liveCategoryEntityClone.setChannels(new ArrayList(liveCategoryEntity.getChannels()));
                }
                arrayList2.add(liveCategoryEntityClone);
            }
            arrayList.addAll(arrayList2);
        }
        this.f35306c.notifyDataSetChanged();
        this.f35307d.notifyDataSetChanged();
        if (arrayList.isEmpty()) {
            ((u) this.binding).f27507c.setVisibility(0);
            ((u) this.binding).f27508d.setVisibility(8);
            return;
        }
        int i11 = this.f35312i;
        int i12 = 0;
        while (true) {
            if (i12 >= arrayList.size()) {
                i12 = 0;
                break;
            }
            LiveCategoryEntity liveCategoryEntity2 = (LiveCategoryEntity) arrayList.get(i12);
            if (liveCategoryEntity2 != null && liveCategoryEntity2.getCategoryId() != null && liveCategoryEntity2.getCategoryId().intValue() == i11) {
                break;
            } else {
                i12++;
            }
        }
        this.f35310g = i12;
        this.f35306c.f(i12);
        ((u) this.binding).f27509e.post(new m(this, 0));
    }

    @Override // android.app.Activity, android.view.Window.Callback
    public final void onAttachedToWindow() {
        super.onAttachedToWindow();
        WindowManager.LayoutParams attributes = getWindow().getAttributes();
        e.d().getClass();
        if (e.e().contains("ar")) {
            attributes.gravity = 8388691;
        } else {
            attributes.gravity = 8388693;
        }
        attributes.width = -1;
        attributes.height = -1;
        getWindow().setAttributes(attributes);
    }

    @Override // xyz.stream.view.BaseActivity, androidx.appcompat.app.AppCompatActivity, androidx.fragment.app.FragmentActivity, android.app.Activity
    public final void onDestroy() {
        this.f35308e.clear();
        this.f35309f.clear();
        ((u) this.binding).f27509e.setAdapter(null);
        ((u) this.binding).f27508d.setAdapter(null);
        super.onDestroy();
    }

    @Override // androidx.appcompat.app.AppCompatActivity, android.app.Activity, android.view.KeyEvent.Callback
    public final boolean onKeyDown(int i10, KeyEvent keyEvent) {
        if (i10 != 4 || keyEvent.getAction() != 0) {
            return super.onKeyDown(i10, keyEvent);
        }
        finish();
        return true;
    }
}
