package android.support.v7.widget.a;

import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.animation.ValueAnimator;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.Build.VERSION;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.d;
import android.support.v7.widget.RecyclerView.h;
import android.support.v7.widget.RecyclerView.i;
import android.support.v7.widget.RecyclerView.j;
import android.support.v7.widget.RecyclerView.l;
import android.support.v7.widget.RecyclerView.s;
import android.support.v7.widget.RecyclerView.v;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewParent;
import java.util.ArrayList;
import java.util.List;

public final class a extends h implements j {
    RecyclerView aiB;
    private d aog = null;
    private int arL;
    private List<Integer> avA;
    View avB = null;
    int avC = -1;
    android.support.v4.view.c avD;
    private b avE;
    private final l avF = new 2(this);
    long avG;
    final List<View> avj = new ArrayList();
    private final float[] avk = new float[2];
    v avl = null;
    float avm;
    float avn;
    float avo;
    float avp;
    float avq;
    float avr;
    float avs;
    float avt;
    a avu;
    int avv = 0;
    int avw;
    List<c> avx = new ArrayList();
    final Runnable avy = new 1(this);
    private List<v> avz;
    int mActivePointerId = -1;
    VelocityTracker mVelocityTracker;
    Rect oT;

    static class c implements AnimatorListener {
        boolean Ag = false;
        final v aoM;
        final float avQ;
        final float avR;
        final float avS;
        final float avT;
        final ValueAnimator avU;
        final int avV;
        public boolean avW;
        float avX;
        float avY;
        boolean avZ = false;
        final int avv;
        float awa;

        c(v vVar, int i, int i2, float f, float f2, float f3, float f4) {
            this.avv = i2;
            this.avV = i;
            this.aoM = vVar;
            this.avQ = f;
            this.avR = f2;
            this.avS = f3;
            this.avT = f4;
            this.avU = ValueAnimator.ofFloat(new float[]{0.0f, 1.0f});
            this.avU.addUpdateListener(new 1(this));
            this.avU.setTarget(vVar.apJ);
            this.avU.addListener(this);
            this.awa = 0.0f;
        }

        public void onAnimationStart(Animator animator) {
        }

        public void onAnimationEnd(Animator animator) {
            if (!this.Ag) {
                this.aoM.ao(true);
            }
            this.Ag = true;
        }

        public void onAnimationRepeat(Animator animator) {
        }

        public void onAnimationCancel(Animator animator) {
            this.awa = 1.0f;
        }
    }

    public a(a aVar) {
        this.avu = aVar;
    }

    private static boolean a(View view, float f, float f2, float f3, float f4) {
        return f >= f3 && f <= ((float) view.getWidth()) + f3 && f2 >= f4 && f2 <= ((float) view.getHeight()) + f4;
    }

    public final void i(RecyclerView recyclerView) {
        if (this.aiB != recyclerView) {
            RecyclerView recyclerView2;
            if (this.aiB != null) {
                this.aiB.c(this);
                this.aiB.b(this.avF);
                recyclerView2 = this.aiB;
                if (recyclerView2.anz != null) {
                    recyclerView2.anz.remove(this);
                }
                for (int size = this.avx.size() - 1; size >= 0; size--) {
                    this.avu.a(this.aiB, ((c) this.avx.get(0)).aoM);
                }
                this.avx.clear();
                this.avB = null;
                this.avC = -1;
                lA();
                if (this.avE != null) {
                    this.avE.avP = false;
                    this.avE = null;
                }
                if (this.avD != null) {
                    this.avD = null;
                }
            }
            this.aiB = recyclerView;
            if (recyclerView != null) {
                Resources resources = recyclerView.getResources();
                this.avo = resources.getDimension(2131428462);
                this.avp = resources.getDimension(2131428461);
                this.arL = ViewConfiguration.get(this.aiB.getContext()).getScaledTouchSlop();
                this.aiB.a(this);
                this.aiB.a(this.avF);
                recyclerView2 = this.aiB;
                if (recyclerView2.anz == null) {
                    recyclerView2.anz = new ArrayList();
                }
                recyclerView2.anz.add(this);
                this.avE = new b(this);
                this.avD = new android.support.v4.view.c(this.aiB.getContext(), this.avE);
            }
        }
    }

    private void f(float[] fArr) {
        if ((this.avw & 12) != 0) {
            fArr[0] = (this.avs + this.avq) - ((float) this.avl.apJ.getLeft());
        } else {
            fArr[0] = this.avl.apJ.getTranslationX();
        }
        if ((this.avw & 3) != 0) {
            fArr[1] = (this.avt + this.avr) - ((float) this.avl.apJ.getTop());
        } else {
            fArr[1] = this.avl.apJ.getTranslationY();
        }
    }

    public final void a(Canvas canvas, RecyclerView recyclerView) {
        Object obj = null;
        if (this.avl != null) {
            f(this.avk);
        }
        v vVar = this.avl;
        List list = this.avx;
        int size = list.size();
        for (int i = 0; i < size; i++) {
            list.get(i);
            canvas.restoreToCount(canvas.save());
        }
        if (vVar != null) {
            canvas.restoreToCount(canvas.save());
        }
        int i2 = size - 1;
        while (i2 >= 0) {
            Object obj2;
            c cVar = (c) list.get(i2);
            if (cVar.Ag && !cVar.avW) {
                list.remove(i2);
                obj2 = obj;
            } else if (cVar.Ag) {
                obj2 = obj;
            } else {
                obj2 = 1;
            }
            i2--;
            obj = obj2;
        }
        if (obj != null) {
            recyclerView.invalidate();
        }
    }

    public final void a(Canvas canvas, RecyclerView recyclerView, s sVar) {
        float f;
        float f2;
        int i;
        this.avC = -1;
        if (this.avl != null) {
            f(this.avk);
            float f3 = this.avk[0];
            f = this.avk[1];
            f2 = f3;
        } else {
            f = 0.0f;
            f2 = 0.0f;
        }
        a aVar = this.avu;
        v vVar = this.avl;
        List list = this.avx;
        int i2 = this.avv;
        int size = list.size();
        int i3 = 0;
        while (true) {
            i = i3;
            if (i >= size) {
                break;
            }
            c cVar = (c) list.get(i);
            if (cVar.avQ == cVar.avS) {
                cVar.avX = cVar.aoM.apJ.getTranslationX();
            } else {
                cVar.avX = cVar.avQ + (cVar.awa * (cVar.avS - cVar.avQ));
            }
            if (cVar.avR == cVar.avT) {
                cVar.avY = cVar.aoM.apJ.getTranslationY();
            } else {
                cVar.avY = cVar.avR + (cVar.awa * (cVar.avT - cVar.avR));
            }
            int save = canvas.save();
            aVar.a(canvas, recyclerView, cVar.aoM, cVar.avX, cVar.avY, cVar.avv, false);
            canvas.restoreToCount(save);
            i3 = i + 1;
        }
        if (vVar != null) {
            i = canvas.save();
            aVar.a(canvas, recyclerView, vVar, f2, f, i2, true);
            canvas.restoreToCount(i);
        }
    }

    /* Access modifiers changed, original: final */
    public final void e(v vVar, int i) {
        if (vVar != this.avl || i != this.avv) {
            this.avG = Long.MIN_VALUE;
            int i2 = this.avv;
            b(vVar, true);
            this.avv = i;
            if (i == 2) {
                this.avB = vVar.apJ;
                if (VERSION.SDK_INT < 21) {
                    if (this.aog == null) {
                        this.aog = new 5(this);
                    }
                    this.aiB.setChildDrawingOrderCallback(this.aog);
                }
            }
            int i3 = (1 << ((i * 8) + 8)) - 1;
            Object obj = null;
            if (this.avl != null) {
                v vVar2 = this.avl;
                if (vVar2.apJ.getParent() != null) {
                    int i4;
                    if (!(i2 == 2 || this.avv == 2)) {
                        a.cZ(android.support.v4.view.s.T(this.aiB));
                    }
                    lA();
                    if (i2 == 2) {
                        i4 = 8;
                    } else {
                        i4 = 4;
                    }
                    f(this.avk);
                    3 3 = new 3(this, vVar2, i4, i2, this.avk[0], this.avk[1], 0.0f, 0.0f, 0, vVar2);
                    3.avU.setDuration(a.d(this.aiB, i4));
                    this.avx.add(3);
                    3.aoM.ao(false);
                    3.avU.start();
                    obj = 1;
                } else {
                    bY(vVar2.apJ);
                    this.avu.a(this.aiB, vVar2);
                }
                this.avl = null;
            }
            Object obj2 = obj;
            if (vVar != null) {
                this.avw = (a.j(this.aiB) & i3) >> (this.avv * 8);
                this.avs = (float) vVar.apJ.getLeft();
                this.avt = (float) vVar.apJ.getTop();
                this.avl = vVar;
                if (i == 2) {
                    this.avl.apJ.performHapticFeedback(0);
                }
            }
            ViewParent parent = this.aiB.getParent();
            if (parent != null) {
                parent.requestDisallowInterceptTouchEvent(this.avl != null);
            }
            if (obj2 == null) {
                this.aiB.getLayoutManager().aoD = true;
            }
            this.avu.f(this.avl, this.avv);
            this.aiB.invalidate();
        }
    }

    /* Access modifiers changed, original: final */
    public final void M(v vVar) {
        if (!this.aiB.isLayoutRequested() && this.avv == 2) {
            float lC = this.avu.lC();
            int i = (int) (this.avs + this.avq);
            int i2 = (int) (this.avt + this.avr);
            if (((float) Math.abs(i2 - vVar.apJ.getTop())) >= ((float) vVar.apJ.getHeight()) * lC || ((float) Math.abs(i - vVar.apJ.getLeft())) >= lC * ((float) vVar.apJ.getWidth())) {
                if (this.avz == null) {
                    this.avz = new ArrayList();
                    this.avA = new ArrayList();
                } else {
                    this.avz.clear();
                    this.avA.clear();
                }
                int round = Math.round(this.avs + this.avq) + 0;
                int round2 = Math.round(this.avt + this.avr) + 0;
                int width = (vVar.apJ.getWidth() + round) + 0;
                int height = (vVar.apJ.getHeight() + round2) + 0;
                int i3 = (round + width) / 2;
                int i4 = (round2 + height) / 2;
                i layoutManager = this.aiB.getLayoutManager();
                int childCount = layoutManager.getChildCount();
                for (int i5 = 0; i5 < childCount; i5++) {
                    View childAt = layoutManager.getChildAt(i5);
                    if (childAt != vVar.apJ && childAt.getBottom() >= round2 && childAt.getTop() <= height && childAt.getRight() >= round && childAt.getLeft() <= width) {
                        v aZ = this.aiB.aZ(childAt);
                        int abs = Math.abs(i3 - ((childAt.getLeft() + childAt.getRight()) / 2));
                        int abs2 = Math.abs(i4 - ((childAt.getBottom() + childAt.getTop()) / 2));
                        int i6 = (abs * abs) + (abs2 * abs2);
                        int i7 = 0;
                        int size = this.avz.size();
                        abs2 = 0;
                        while (true) {
                            abs = abs2;
                            if (abs >= size || i6 <= ((Integer) this.avA.get(abs)).intValue()) {
                                this.avz.add(i7, aZ);
                                this.avA.add(i7, Integer.valueOf(i6));
                            } else {
                                i7++;
                                abs2 = abs + 1;
                            }
                        }
                        this.avz.add(i7, aZ);
                        this.avA.add(i7, Integer.valueOf(i6));
                    }
                }
                List list = this.avz;
                if (list.size() != 0) {
                    v a = a.a(vVar, list, i, i2);
                    if (a == null) {
                        this.avz.clear();
                        this.avA.clear();
                        return;
                    }
                    a.kj();
                    vVar.kj();
                    this.avu.a(vVar, a);
                }
            }
        }
    }

    public final void bH(View view) {
        bY(view);
        v aZ = this.aiB.aZ(view);
        if (aZ != null) {
            if (this.avl == null || aZ != this.avl) {
                b(aZ, false);
                if (this.avj.remove(aZ.apJ)) {
                    this.avu.a(this.aiB, aZ);
                    return;
                }
                return;
            }
            e(null, 0);
        }
    }

    /* Access modifiers changed, original: final */
    public final int b(v vVar, boolean z) {
        for (int size = this.avx.size() - 1; size >= 0; size--) {
            c cVar = (c) this.avx.get(size);
            if (cVar.aoM == vVar) {
                cVar.avZ |= z;
                if (!cVar.Ag) {
                    cVar.avU.cancel();
                }
                this.avx.remove(size);
                return cVar.avV;
            }
        }
        return 0;
    }

    public final void a(Rect rect, View view, RecyclerView recyclerView, s sVar) {
        rect.setEmpty();
    }

    private void lA() {
        if (this.mVelocityTracker != null) {
            this.mVelocityTracker.recycle();
            this.mVelocityTracker = null;
        }
    }

    /* Access modifiers changed, original: final */
    public final boolean a(int i, MotionEvent motionEvent, int i2) {
        v vVar = null;
        if (this.avl == null && i == 2 && this.avv != 2 && this.avu.lB() && this.aiB.getScrollState() != 1) {
            i layoutManager = this.aiB.getLayoutManager();
            if (this.mActivePointerId != -1) {
                int findPointerIndex = motionEvent.findPointerIndex(this.mActivePointerId);
                float x = motionEvent.getX(findPointerIndex) - this.avm;
                float y = motionEvent.getY(findPointerIndex) - this.avn;
                x = Math.abs(x);
                y = Math.abs(y);
                if ((x >= ((float) this.arL) || y >= ((float) this.arL)) && ((x <= y || !layoutManager.iG()) && (y <= x || !layoutManager.iH()))) {
                    View k = k(motionEvent);
                    if (k != null) {
                        vVar = this.aiB.aZ(k);
                    }
                }
            }
            if (vVar != null) {
                a.j(this.aiB);
            }
        }
        return false;
    }

    /* Access modifiers changed, original: final */
    public final View k(MotionEvent motionEvent) {
        float x = motionEvent.getX();
        float y = motionEvent.getY();
        if (this.avl != null) {
            View view = this.avl.apJ;
            if (a(view, x, y, this.avs + this.avq, this.avt + this.avr)) {
                return view;
            }
        }
        for (int size = this.avx.size() - 1; size >= 0; size--) {
            c cVar = (c) this.avx.get(size);
            View view2 = cVar.aoM.apJ;
            if (a(view2, x, y, cVar.avX, cVar.avY)) {
                return view2;
            }
        }
        return this.aiB.x(x, y);
    }

    /* Access modifiers changed, original: final */
    public final void a(MotionEvent motionEvent, int i, int i2) {
        float x = motionEvent.getX(i2);
        float y = motionEvent.getY(i2);
        this.avq = x - this.avm;
        this.avr = y - this.avn;
        if ((i & 4) == 0) {
            this.avq = Math.max(0.0f, this.avq);
        }
        if ((i & 8) == 0) {
            this.avq = Math.min(0.0f, this.avq);
        }
        if ((i & 1) == 0) {
            this.avr = Math.max(0.0f, this.avr);
        }
        if ((i & 2) == 0) {
            this.avr = Math.min(0.0f, this.avr);
        }
    }

    /* Access modifiers changed, original: final */
    public final void bY(View view) {
        if (view == this.avB) {
            this.avB = null;
            if (this.aog != null) {
                this.aiB.setChildDrawingOrderCallback(null);
            }
        }
    }
}
