package X6FRs_4.Xgtqvk_8.KQgRr5e_0;

import android.graphics.Rect;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
/* compiled from: FocusStrategy.java */
/* loaded from: classes.dex */
public class QxJ9d_1 {

    /* compiled from: FocusStrategy.java */
    /* loaded from: classes.dex */
    public interface KQgRr5e_0<T> {
        void KQgRr5e_0(T t, Rect rect);
    }

    /* compiled from: FocusStrategy.java */
    /* loaded from: classes.dex */
    public static class PGPF6hQ_2<T> implements Comparator<T> {
        public final KQgRr5e_0<T> KQgRr5e_0;

        /* renamed from: KQgRr5e_0  reason: collision with other field name */
        public final Rect f1256KQgRr5e_0 = new Rect();
        public final Rect QxJ9d_1 = new Rect();

        /* renamed from: QxJ9d_1  reason: collision with other field name */
        public final boolean f1257QxJ9d_1;

        public PGPF6hQ_2(boolean z, KQgRr5e_0<T> kQgRr5e_0) {
            this.f1257QxJ9d_1 = z;
            this.KQgRr5e_0 = kQgRr5e_0;
        }

        @Override // java.util.Comparator
        public int compare(T t, T t2) {
            Rect rect = this.f1256KQgRr5e_0;
            Rect rect2 = this.QxJ9d_1;
            this.KQgRr5e_0.KQgRr5e_0(t, rect);
            this.KQgRr5e_0.KQgRr5e_0(t2, rect2);
            int i = rect.top;
            int i2 = rect2.top;
            if (i < i2) {
                return -1;
            }
            if (i > i2) {
                return 1;
            }
            int i3 = rect.left;
            int i4 = rect2.left;
            if (i3 < i4) {
                return this.f1257QxJ9d_1 ? 1 : -1;
            } else if (i3 > i4) {
                return this.f1257QxJ9d_1 ? -1 : 1;
            } else {
                int i5 = rect.bottom;
                int i6 = rect2.bottom;
                if (i5 < i6) {
                    return -1;
                }
                if (i5 > i6) {
                    return 1;
                }
                int i7 = rect.right;
                int i8 = rect2.right;
                if (i7 < i8) {
                    return this.f1257QxJ9d_1 ? 1 : -1;
                } else if (i7 > i8) {
                    return this.f1257QxJ9d_1 ? -1 : 1;
                } else {
                    return 0;
                }
            }
        }
    }

    /* compiled from: FocusStrategy.java */
    /* renamed from: X6FRs_4.Xgtqvk_8.KQgRr5e_0.QxJ9d_1$QxJ9d_1  reason: collision with other inner class name */
    /* loaded from: classes.dex */
    public interface InterfaceC0069QxJ9d_1<T, V> {
        int KQgRr5e_0(T t);

        V KQgRr5e_0(T t, int i);
    }

    public static int KQgRr5e_0(int i, int i2) {
        return (i * 13 * i) + (i2 * i2);
    }

    public static <L, T> T KQgRr5e_0(L l, InterfaceC0069QxJ9d_1<L, T> interfaceC0069QxJ9d_1, KQgRr5e_0<T> kQgRr5e_0, T t, int i, boolean z, boolean z2) {
        int KQgRr5e_02 = interfaceC0069QxJ9d_1.KQgRr5e_0(l);
        ArrayList arrayList = new ArrayList(KQgRr5e_02);
        for (int i2 = 0; i2 < KQgRr5e_02; i2++) {
            arrayList.add(interfaceC0069QxJ9d_1.KQgRr5e_0(l, i2));
        }
        Collections.sort(arrayList, new PGPF6hQ_2(z, kQgRr5e_0));
        if (i != 1) {
            if (i == 2) {
                return (T) KQgRr5e_0(t, arrayList, z2);
            }
            throw new IllegalArgumentException("direction must be one of {FOCUS_FORWARD, FOCUS_BACKWARD}.");
        }
        return (T) QxJ9d_1(t, arrayList, z2);
    }

    public static int Mqv2r_3(int i, Rect rect, Rect rect2) {
        int i2;
        int i3;
        if (i == 17) {
            i2 = rect.left;
            i3 = rect2.left;
        } else if (i == 33) {
            i2 = rect.top;
            i3 = rect2.top;
        } else if (i == 66) {
            i2 = rect2.right;
            i3 = rect.right;
        } else if (i == 130) {
            i2 = rect2.bottom;
            i3 = rect.bottom;
        } else {
            throw new IllegalArgumentException("direction must be one of {FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT}.");
        }
        return i2 - i3;
    }

    public static int PGPF6hQ_2(int i, Rect rect, Rect rect2) {
        return Math.max(1, Mqv2r_3(i, rect, rect2));
    }

    public static <T> T QxJ9d_1(T t, ArrayList<T> arrayList, boolean z) {
        int size = arrayList.size();
        int indexOf = (t == null ? size : arrayList.indexOf(t)) - 1;
        if (indexOf >= 0) {
            return arrayList.get(indexOf);
        }
        if (!z || size <= 0) {
            return null;
        }
        return arrayList.get(size - 1);
    }

    public static int X6FRs_4(int i, Rect rect, Rect rect2) {
        if (i != 17) {
            if (i != 33) {
                if (i != 66) {
                    if (i != 130) {
                        throw new IllegalArgumentException("direction must be one of {FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT}.");
                    }
                }
            }
            return Math.abs((rect.left + (rect.width() / 2)) - (rect2.left + (rect2.width() / 2)));
        }
        return Math.abs((rect.top + (rect.height() / 2)) - (rect2.top + (rect2.height() / 2)));
    }

    public static boolean QxJ9d_1(int i, Rect rect, Rect rect2, Rect rect3) {
        if (KQgRr5e_0(rect, rect2, i)) {
            if (KQgRr5e_0(rect, rect3, i) && !KQgRr5e_0(i, rect, rect2, rect3)) {
                return !KQgRr5e_0(i, rect, rect3, rect2) && KQgRr5e_0(KQgRr5e_0(i, rect, rect2), X6FRs_4(i, rect, rect2)) < KQgRr5e_0(KQgRr5e_0(i, rect, rect3), X6FRs_4(i, rect, rect3));
            }
            return true;
        }
        return false;
    }

    public static <T> T KQgRr5e_0(T t, ArrayList<T> arrayList, boolean z) {
        int size = arrayList.size();
        int lastIndexOf = (t == null ? -1 : arrayList.lastIndexOf(t)) + 1;
        if (lastIndexOf < size) {
            return arrayList.get(lastIndexOf);
        }
        if (!z || size <= 0) {
            return null;
        }
        return arrayList.get(0);
    }

    public static <L, T> T KQgRr5e_0(L l, InterfaceC0069QxJ9d_1<L, T> interfaceC0069QxJ9d_1, KQgRr5e_0<T> kQgRr5e_0, T t, Rect rect, int i) {
        Rect rect2 = new Rect(rect);
        if (i == 17) {
            rect2.offset(rect.width() + 1, 0);
        } else if (i == 33) {
            rect2.offset(0, rect.height() + 1);
        } else if (i == 66) {
            rect2.offset(-(rect.width() + 1), 0);
        } else if (i == 130) {
            rect2.offset(0, -(rect.height() + 1));
        } else {
            throw new IllegalArgumentException("direction must be one of {FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT}.");
        }
        T t2 = null;
        int KQgRr5e_02 = interfaceC0069QxJ9d_1.KQgRr5e_0(l);
        Rect rect3 = new Rect();
        for (int i2 = 0; i2 < KQgRr5e_02; i2++) {
            T KQgRr5e_03 = interfaceC0069QxJ9d_1.KQgRr5e_0(l, i2);
            if (KQgRr5e_03 != t) {
                kQgRr5e_0.KQgRr5e_0(KQgRr5e_03, rect3);
                if (QxJ9d_1(i, rect, rect3, rect2)) {
                    rect2.set(rect3);
                    t2 = KQgRr5e_03;
                }
            }
        }
        return t2;
    }

    /* renamed from: QxJ9d_1  reason: collision with other method in class */
    public static boolean m597QxJ9d_1(int i, Rect rect, Rect rect2) {
        if (i == 17) {
            return rect.left >= rect2.right;
        } else if (i == 33) {
            return rect.top >= rect2.bottom;
        } else if (i == 66) {
            return rect.right <= rect2.left;
        } else if (i == 130) {
            return rect.bottom <= rect2.top;
        } else {
            throw new IllegalArgumentException("direction must be one of {FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT}.");
        }
    }

    public static int QxJ9d_1(int i, Rect rect, Rect rect2) {
        int i2;
        int i3;
        if (i == 17) {
            i2 = rect.left;
            i3 = rect2.right;
        } else if (i == 33) {
            i2 = rect.top;
            i3 = rect2.bottom;
        } else if (i == 66) {
            i2 = rect2.left;
            i3 = rect.right;
        } else if (i == 130) {
            i2 = rect2.top;
            i3 = rect.bottom;
        } else {
            throw new IllegalArgumentException("direction must be one of {FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT}.");
        }
        return i2 - i3;
    }

    public static boolean KQgRr5e_0(int i, Rect rect, Rect rect2, Rect rect3) {
        boolean m596KQgRr5e_0 = m596KQgRr5e_0(i, rect, rect2);
        if (m596KQgRr5e_0(i, rect, rect3) || !m596KQgRr5e_0) {
            return false;
        }
        return !m597QxJ9d_1(i, rect, rect3) || i == 17 || i == 66 || KQgRr5e_0(i, rect, rect2) < PGPF6hQ_2(i, rect, rect3);
    }

    public static boolean KQgRr5e_0(Rect rect, Rect rect2, int i) {
        if (i == 17) {
            int i2 = rect.right;
            int i3 = rect2.right;
            return (i2 > i3 || rect.left >= i3) && rect.left > rect2.left;
        } else if (i == 33) {
            int i4 = rect.bottom;
            int i5 = rect2.bottom;
            return (i4 > i5 || rect.top >= i5) && rect.top > rect2.top;
        } else if (i == 66) {
            int i6 = rect.left;
            int i7 = rect2.left;
            return (i6 < i7 || rect.right <= i7) && rect.right < rect2.right;
        } else if (i == 130) {
            int i8 = rect.top;
            int i9 = rect2.top;
            return (i8 < i9 || rect.bottom <= i9) && rect.bottom < rect2.bottom;
        } else {
            throw new IllegalArgumentException("direction must be one of {FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT}.");
        }
    }

    /* renamed from: KQgRr5e_0  reason: collision with other method in class */
    public static boolean m596KQgRr5e_0(int i, Rect rect, Rect rect2) {
        if (i != 17) {
            if (i != 33) {
                if (i != 66) {
                    if (i != 130) {
                        throw new IllegalArgumentException("direction must be one of {FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT}.");
                    }
                }
            }
            return rect2.right >= rect.left && rect2.left <= rect.right;
        }
        return rect2.bottom >= rect.top && rect2.top <= rect.bottom;
    }

    public static int KQgRr5e_0(int i, Rect rect, Rect rect2) {
        return Math.max(0, QxJ9d_1(i, rect, rect2));
    }
}
