package androidx.recyclerview.widget;

import android.R;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.view.MotionEvent;
import androidx.core.view.s0;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.firebase.perf.util.Constants;

/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes.dex */
public class h extends RecyclerView.o implements RecyclerView.t {
    private static final int[] D = {R.attr.state_pressed};
    private static final int[] E = new int[0];
    int A;
    private final Runnable B;
    private final RecyclerView.u C;

    /* renamed from: a, reason: collision with root package name */
    private final int f5396a;

    /* renamed from: b, reason: collision with root package name */
    private final int f5397b;

    /* renamed from: c, reason: collision with root package name */
    final StateListDrawable f5398c;

    /* renamed from: d, reason: collision with root package name */
    final Drawable f5399d;

    /* renamed from: e, reason: collision with root package name */
    private final int f5400e;

    /* renamed from: f, reason: collision with root package name */
    private final int f5401f;

    /* renamed from: g, reason: collision with root package name */
    private final StateListDrawable f5402g;

    /* renamed from: h, reason: collision with root package name */
    private final Drawable f5403h;

    /* renamed from: i, reason: collision with root package name */
    private final int f5404i;

    /* renamed from: j, reason: collision with root package name */
    private final int f5405j;

    /* renamed from: k, reason: collision with root package name */
    int f5406k;

    /* renamed from: l, reason: collision with root package name */
    int f5407l;

    /* renamed from: m, reason: collision with root package name */
    float f5408m;

    /* renamed from: n, reason: collision with root package name */
    int f5409n;

    /* renamed from: o, reason: collision with root package name */
    int f5410o;

    /* renamed from: p, reason: collision with root package name */
    float f5411p;

    /* renamed from: s, reason: collision with root package name */
    private RecyclerView f5414s;

    /* renamed from: z, reason: collision with root package name */
    final ValueAnimator f5421z;

    /* renamed from: q, reason: collision with root package name */
    private int f5412q = 0;

    /* renamed from: r, reason: collision with root package name */
    private int f5413r = 0;

    /* renamed from: t, reason: collision with root package name */
    private boolean f5415t = false;

    /* renamed from: u, reason: collision with root package name */
    private boolean f5416u = false;

    /* renamed from: v, reason: collision with root package name */
    private int f5417v = 0;

    /* renamed from: w, reason: collision with root package name */
    private int f5418w = 0;

    /* renamed from: x, reason: collision with root package name */
    private final int[] f5419x = new int[2];

    /* renamed from: y, reason: collision with root package name */
    private final int[] f5420y = new int[2];

    /* loaded from: classes.dex */
    class a implements Runnable {
        a() {
        }

        @Override // java.lang.Runnable
        public void run() {
            h.this.q(500);
        }
    }

    /* loaded from: classes.dex */
    class b extends RecyclerView.u {
        b() {
        }

        @Override // androidx.recyclerview.widget.RecyclerView.u
        public void b(RecyclerView recyclerView, int i10, int i11) {
            h.this.B(recyclerView.computeHorizontalScrollOffset(), recyclerView.computeVerticalScrollOffset());
        }
    }

    /* loaded from: classes.dex */
    private class c extends AnimatorListenerAdapter {

        /* renamed from: a, reason: collision with root package name */
        private boolean f5424a = false;

        c() {
        }

        @Override // android.animation.AnimatorListenerAdapter, android.animation.Animator.AnimatorListener
        public void onAnimationCancel(Animator animator) {
            this.f5424a = true;
        }

        @Override // android.animation.AnimatorListenerAdapter, android.animation.Animator.AnimatorListener
        public void onAnimationEnd(Animator animator) {
            if (this.f5424a) {
                this.f5424a = false;
                return;
            }
            if (((Float) h.this.f5421z.getAnimatedValue()).floatValue() == BitmapDescriptorFactory.HUE_RED) {
                h hVar = h.this;
                hVar.A = 0;
                hVar.y(0);
            } else {
                h hVar2 = h.this;
                hVar2.A = 2;
                hVar2.v();
            }
        }
    }

    /* loaded from: classes.dex */
    private class d implements ValueAnimator.AnimatorUpdateListener {
        d() {
        }

        @Override // android.animation.ValueAnimator.AnimatorUpdateListener
        public void onAnimationUpdate(ValueAnimator valueAnimator) {
            int floatValue = (int) (((Float) valueAnimator.getAnimatedValue()).floatValue() * 255.0f);
            h.this.f5398c.setAlpha(floatValue);
            h.this.f5399d.setAlpha(floatValue);
            h.this.v();
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public h(RecyclerView recyclerView, StateListDrawable stateListDrawable, Drawable drawable, StateListDrawable stateListDrawable2, Drawable drawable2, int i10, int i11, int i12) {
        ValueAnimator ofFloat = ValueAnimator.ofFloat(BitmapDescriptorFactory.HUE_RED, 1.0f);
        this.f5421z = ofFloat;
        this.A = 0;
        this.B = new a();
        this.C = new b();
        this.f5398c = stateListDrawable;
        this.f5399d = drawable;
        this.f5402g = stateListDrawable2;
        this.f5403h = drawable2;
        this.f5400e = Math.max(i10, stateListDrawable.getIntrinsicWidth());
        this.f5401f = Math.max(i10, drawable.getIntrinsicWidth());
        this.f5404i = Math.max(i10, stateListDrawable2.getIntrinsicWidth());
        this.f5405j = Math.max(i10, drawable2.getIntrinsicWidth());
        this.f5396a = i11;
        this.f5397b = i12;
        stateListDrawable.setAlpha(Constants.MAX_HOST_LENGTH);
        drawable.setAlpha(Constants.MAX_HOST_LENGTH);
        ofFloat.addListener(new c());
        ofFloat.addUpdateListener(new d());
        j(recyclerView);
    }

    private void C(float f10) {
        int[] p10 = p();
        float max = Math.max(p10[0], Math.min(p10[1], f10));
        if (Math.abs(this.f5407l - max) < 2.0f) {
            return;
        }
        int x10 = x(this.f5408m, max, p10, this.f5414s.computeVerticalScrollRange(), this.f5414s.computeVerticalScrollOffset(), this.f5413r);
        if (x10 != 0) {
            this.f5414s.scrollBy(0, x10);
        }
        this.f5408m = max;
    }

    private void k() {
        this.f5414s.removeCallbacks(this.B);
    }

    private void l() {
        this.f5414s.Z0(this);
        this.f5414s.a1(this);
        this.f5414s.b1(this.C);
        k();
    }

    private void m(Canvas canvas) {
        int i10 = this.f5413r;
        int i11 = this.f5404i;
        int i12 = this.f5410o;
        int i13 = this.f5409n;
        this.f5402g.setBounds(0, 0, i13, i11);
        this.f5403h.setBounds(0, 0, this.f5412q, this.f5405j);
        canvas.translate(BitmapDescriptorFactory.HUE_RED, i10 - i11);
        this.f5403h.draw(canvas);
        canvas.translate(i12 - (i13 / 2), BitmapDescriptorFactory.HUE_RED);
        this.f5402g.draw(canvas);
        canvas.translate(-r2, -r0);
    }

    private void n(Canvas canvas) {
        int i10 = this.f5412q;
        int i11 = this.f5400e;
        int i12 = i10 - i11;
        int i13 = this.f5407l;
        int i14 = this.f5406k;
        int i15 = i13 - (i14 / 2);
        this.f5398c.setBounds(0, 0, i11, i14);
        this.f5399d.setBounds(0, 0, this.f5401f, this.f5413r);
        if (!s()) {
            canvas.translate(i12, BitmapDescriptorFactory.HUE_RED);
            this.f5399d.draw(canvas);
            canvas.translate(BitmapDescriptorFactory.HUE_RED, i15);
            this.f5398c.draw(canvas);
            canvas.translate(-i12, -i15);
            return;
        }
        this.f5399d.draw(canvas);
        canvas.translate(this.f5400e, i15);
        canvas.scale(-1.0f, 1.0f);
        this.f5398c.draw(canvas);
        canvas.scale(-1.0f, 1.0f);
        canvas.translate(-this.f5400e, -i15);
    }

    private int[] o() {
        int[] iArr = this.f5420y;
        int i10 = this.f5397b;
        iArr[0] = i10;
        iArr[1] = this.f5412q - i10;
        return iArr;
    }

    private int[] p() {
        int[] iArr = this.f5419x;
        int i10 = this.f5397b;
        iArr[0] = i10;
        iArr[1] = this.f5413r - i10;
        return iArr;
    }

    private void r(float f10) {
        int[] o10 = o();
        float max = Math.max(o10[0], Math.min(o10[1], f10));
        if (Math.abs(this.f5410o - max) < 2.0f) {
            return;
        }
        int x10 = x(this.f5411p, max, o10, this.f5414s.computeHorizontalScrollRange(), this.f5414s.computeHorizontalScrollOffset(), this.f5412q);
        if (x10 != 0) {
            this.f5414s.scrollBy(x10, 0);
        }
        this.f5411p = max;
    }

    private boolean s() {
        return s0.z(this.f5414s) == 1;
    }

    private void w(int i10) {
        k();
        this.f5414s.postDelayed(this.B, i10);
    }

    private int x(float f10, float f11, int[] iArr, int i10, int i11, int i12) {
        int i13 = iArr[1] - iArr[0];
        if (i13 == 0) {
            return 0;
        }
        int i14 = i10 - i12;
        int i15 = (int) (((f11 - f10) / i13) * i14);
        int i16 = i11 + i15;
        if (i16 >= i14 || i16 < 0) {
            return 0;
        }
        return i15;
    }

    private void z() {
        this.f5414s.h(this);
        this.f5414s.k(this);
        this.f5414s.l(this.C);
    }

    public void A() {
        int i10 = this.A;
        if (i10 != 0) {
            if (i10 != 3) {
                return;
            } else {
                this.f5421z.cancel();
            }
        }
        this.A = 1;
        ValueAnimator valueAnimator = this.f5421z;
        valueAnimator.setFloatValues(((Float) valueAnimator.getAnimatedValue()).floatValue(), 1.0f);
        this.f5421z.setDuration(500L);
        this.f5421z.setStartDelay(0L);
        this.f5421z.start();
    }

    void B(int i10, int i11) {
        int computeVerticalScrollRange = this.f5414s.computeVerticalScrollRange();
        int i12 = this.f5413r;
        this.f5415t = computeVerticalScrollRange - i12 > 0 && i12 >= this.f5396a;
        int computeHorizontalScrollRange = this.f5414s.computeHorizontalScrollRange();
        int i13 = this.f5412q;
        boolean z10 = computeHorizontalScrollRange - i13 > 0 && i13 >= this.f5396a;
        this.f5416u = z10;
        boolean z11 = this.f5415t;
        if (!z11 && !z10) {
            if (this.f5417v != 0) {
                y(0);
                return;
            }
            return;
        }
        if (z11) {
            float f10 = i12;
            this.f5407l = (int) ((f10 * (i11 + (f10 / 2.0f))) / computeVerticalScrollRange);
            this.f5406k = Math.min(i12, (i12 * i12) / computeVerticalScrollRange);
        }
        if (this.f5416u) {
            float f11 = i13;
            this.f5410o = (int) ((f11 * (i10 + (f11 / 2.0f))) / computeHorizontalScrollRange);
            this.f5409n = Math.min(i13, (i13 * i13) / computeHorizontalScrollRange);
        }
        int i14 = this.f5417v;
        if (i14 == 0 || i14 == 1) {
            y(1);
        }
    }

    @Override // androidx.recyclerview.widget.RecyclerView.t
    public void a(RecyclerView recyclerView, MotionEvent motionEvent) {
        if (this.f5417v == 0) {
            return;
        }
        if (motionEvent.getAction() == 0) {
            boolean u10 = u(motionEvent.getX(), motionEvent.getY());
            boolean t10 = t(motionEvent.getX(), motionEvent.getY());
            if (u10 || t10) {
                if (t10) {
                    this.f5418w = 1;
                    this.f5411p = (int) motionEvent.getX();
                } else if (u10) {
                    this.f5418w = 2;
                    this.f5408m = (int) motionEvent.getY();
                }
                y(2);
                return;
            }
            return;
        }
        if (motionEvent.getAction() == 1 && this.f5417v == 2) {
            this.f5408m = BitmapDescriptorFactory.HUE_RED;
            this.f5411p = BitmapDescriptorFactory.HUE_RED;
            y(1);
            this.f5418w = 0;
            return;
        }
        if (motionEvent.getAction() == 2 && this.f5417v == 2) {
            A();
            if (this.f5418w == 1) {
                r(motionEvent.getX());
            }
            if (this.f5418w == 2) {
                C(motionEvent.getY());
            }
        }
    }

    @Override // androidx.recyclerview.widget.RecyclerView.t
    public boolean b(RecyclerView recyclerView, MotionEvent motionEvent) {
        int i10 = this.f5417v;
        if (i10 == 1) {
            boolean u10 = u(motionEvent.getX(), motionEvent.getY());
            boolean t10 = t(motionEvent.getX(), motionEvent.getY());
            if (motionEvent.getAction() != 0) {
                return false;
            }
            if (!u10 && !t10) {
                return false;
            }
            if (t10) {
                this.f5418w = 1;
                this.f5411p = (int) motionEvent.getX();
            } else if (u10) {
                this.f5418w = 2;
                this.f5408m = (int) motionEvent.getY();
            }
            y(2);
        } else if (i10 != 2) {
            return false;
        }
        return true;
    }

    @Override // androidx.recyclerview.widget.RecyclerView.t
    public void c(boolean z10) {
    }

    @Override // androidx.recyclerview.widget.RecyclerView.o
    public void i(Canvas canvas, RecyclerView recyclerView, RecyclerView.b0 b0Var) {
        if (this.f5412q != this.f5414s.getWidth() || this.f5413r != this.f5414s.getHeight()) {
            this.f5412q = this.f5414s.getWidth();
            this.f5413r = this.f5414s.getHeight();
            y(0);
        } else if (this.A != 0) {
            if (this.f5415t) {
                n(canvas);
            }
            if (this.f5416u) {
                m(canvas);
            }
        }
    }

    public void j(RecyclerView recyclerView) {
        RecyclerView recyclerView2 = this.f5414s;
        if (recyclerView2 == recyclerView) {
            return;
        }
        if (recyclerView2 != null) {
            l();
        }
        this.f5414s = recyclerView;
        if (recyclerView != null) {
            z();
        }
    }

    void q(int i10) {
        int i11 = this.A;
        if (i11 == 1) {
            this.f5421z.cancel();
        } else if (i11 != 2) {
            return;
        }
        this.A = 3;
        ValueAnimator valueAnimator = this.f5421z;
        valueAnimator.setFloatValues(((Float) valueAnimator.getAnimatedValue()).floatValue(), BitmapDescriptorFactory.HUE_RED);
        this.f5421z.setDuration(i10);
        this.f5421z.start();
    }

    boolean t(float f10, float f11) {
        if (f11 >= this.f5413r - this.f5404i) {
            int i10 = this.f5410o;
            int i11 = this.f5409n;
            if (f10 >= i10 - (i11 / 2) && f10 <= i10 + (i11 / 2)) {
                return true;
            }
        }
        return false;
    }

    boolean u(float f10, float f11) {
        if (!s() ? f10 >= this.f5412q - this.f5400e : f10 <= this.f5400e) {
            int i10 = this.f5407l;
            int i11 = this.f5406k;
            if (f11 >= i10 - (i11 / 2) && f11 <= i10 + (i11 / 2)) {
                return true;
            }
        }
        return false;
    }

    void v() {
        this.f5414s.invalidate();
    }

    void y(int i10) {
        if (i10 == 2 && this.f5417v != 2) {
            this.f5398c.setState(D);
            k();
        }
        if (i10 == 0) {
            v();
        } else {
            A();
        }
        if (this.f5417v == 2 && i10 != 2) {
            this.f5398c.setState(E);
            w(1200);
        } else if (i10 == 1) {
            w(ConnectionResult.DRIVE_EXTERNAL_STORAGE_REQUIRED);
        }
        this.f5417v = i10;
    }
}
