package com.ss.android.ugc.aweme.shortvideo.sticker.infoSticker.interact.consume.dialog;

import X.05p;
import X.05s;
import X.06Z;
import X.08v;
import X.08w;
import X.0GZ;
import X.1gt;
import X.2kf;
import X.LuH;
import X.T37;
import android.app.Dialog;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import androidx.core.g.t;
import androidx.customview.view.AbsSavedState;
import com.bytedance.covode.number.Covode;
import java.lang.ref.WeakReference;

public class ViewPagerBottomSheetBehavior<V extends View> extends 05p<V> {
    public int LIZ;
    public int LIZIZ;
    public int LIZJ;
    public boolean LIZLLL;
    public boolean LJ;
    public int LJFF = 4;
    public 08w LJI;
    public int LJII;
    public WeakReference<V> LJIIIIZZ;
    public WeakReference<View> LJIIIZ;
    public WeakReference<Dialog> LJIIJ;
    public LuH LJIIJJI;
    public int LJIIL;
    public boolean LJIILIIL;
    public float LJIILJJIL;
    public int LJIILL;
    public boolean LJIILLIIL;
    public boolean LJIIZILJ;
    public int LJIJ;
    public boolean LJIJI;
    public VelocityTracker LJIJJ;
    public int LJIJJLI;
    public final 08v LJIL = new 08v() {
        static {
            Covode.recordClassIndex(112304);
        }

        public final int LIZIZ() {
            int i;
            int i2;
            if (ViewPagerBottomSheetBehavior.this.LIZLLL) {
                i = ViewPagerBottomSheetBehavior.this.LJII;
                i2 = ViewPagerBottomSheetBehavior.this.LIZIZ;
            } else {
                i = ViewPagerBottomSheetBehavior.this.LIZJ;
                i2 = ViewPagerBottomSheetBehavior.this.LIZIZ;
            }
            return i - i2;
        }

        public final void LIZ(int i) {
            if (i == 1) {
                ViewPagerBottomSheetBehavior.this.LIZ(1);
            }
        }

        public final int LIZJ(View view, int i) {
            return view.getLeft();
        }

        public final boolean LIZIZ(View view, int i) {
            View view2;
            if (ViewPagerBottomSheetBehavior.this.LJFF == 1 || ViewPagerBottomSheetBehavior.this.LJIILIIL) {
                return false;
            }
            if ((ViewPagerBottomSheetBehavior.this.LJFF != 3 || ViewPagerBottomSheetBehavior.this.LJIIL != i || (view2 = (View) ViewPagerBottomSheetBehavior.this.LJIIIZ.get()) == null || !view2.canScrollVertically(-1)) && ViewPagerBottomSheetBehavior.this.LJIIIIZZ != null && ViewPagerBottomSheetBehavior.this.LJIIIIZZ.get() == view) {
                return true;
            }
            return false;
        }

        public final int LIZ(View view, int i, int i2) {
            int i3;
            int i4 = ViewPagerBottomSheetBehavior.this.LIZIZ;
            if (ViewPagerBottomSheetBehavior.this.LIZLLL) {
                i3 = ViewPagerBottomSheetBehavior.this.LJII;
            } else {
                i3 = ViewPagerBottomSheetBehavior.this.LIZJ;
            }
            return 06Z.LIZ(i, i4, i3);
        }

        public final void LIZ(View view, float f, float f2) {
            int i;
            int i2 = 3;
            if (f2 < 0.0f) {
                i = ViewPagerBottomSheetBehavior.this.LIZIZ;
            } else if (!ViewPagerBottomSheetBehavior.this.LIZLLL || !ViewPagerBottomSheetBehavior.this.LIZ(view, f2)) {
                if (f2 == 0.0f) {
                    int top = view.getTop();
                    if (Math.abs(top - ViewPagerBottomSheetBehavior.this.LIZIZ) < Math.abs(top - ViewPagerBottomSheetBehavior.this.LIZJ)) {
                        i = ViewPagerBottomSheetBehavior.this.LIZIZ;
                    } else {
                        i = ViewPagerBottomSheetBehavior.this.LIZJ;
                    }
                } else {
                    i = ViewPagerBottomSheetBehavior.this.LIZJ;
                }
                i2 = 4;
            } else {
                i = ViewPagerBottomSheetBehavior.this.LJII;
                i2 = 5;
            }
            if (ViewPagerBottomSheetBehavior.this.LJI.LIZ(view.getLeft(), i)) {
                ViewPagerBottomSheetBehavior.this.LIZ(2);
                view.postOnAnimation(new T37(ViewPagerBottomSheetBehavior.this, view, i2));
                return;
            }
            ViewPagerBottomSheetBehavior.this.LIZ(i2);
        }

        public final void LIZ(View view, int i, int i2, int i3, int i4) {
            ViewPagerBottomSheetBehavior.this.LIZ();
        }
    };

    static {
        Covode.recordClassIndex(112302);
    }

    public final void LIZ() {
        this.LJIIIIZZ.get();
    }

    public static class SavedState extends AbsSavedState {
        public static final Parcelable.Creator<SavedState> CREATOR = new Parcelable.ClassLoaderCreator<SavedState>() {
            static {
                Covode.recordClassIndex(112306);
            }

            public final /* bridge */ /* synthetic */ Object[] newArray(int i) {
                return new SavedState[i];
            }

            public final /* synthetic */ Object createFromParcel(Parcel parcel) {
                return new SavedState(parcel, (ClassLoader) null);
            }

            public final /* synthetic */ Object createFromParcel(Parcel parcel, ClassLoader classLoader) {
                return new SavedState(parcel, classLoader);
            }
        };
        public final int LIZ;

        static {
            Covode.recordClassIndex(112305);
        }

        public SavedState(Parcelable parcelable, int i) {
            super(parcelable);
            this.LIZ = i;
        }

        public SavedState(Parcel parcel, ClassLoader classLoader) {
            super(parcel, classLoader);
            this.LIZ = parcel.readInt();
        }

        public void writeToParcel(Parcel parcel, int i) {
            ViewPagerBottomSheetBehavior.super.writeToParcel(parcel, i);
            parcel.writeInt(this.LIZ);
        }
    }

    public ViewPagerBottomSheetBehavior() {
    }

    private void LIZIZ() {
        this.LJIIL = -1;
        VelocityTracker velocityTracker = this.LJIJJ;
        if (velocityTracker != null) {
            velocityTracker.recycle();
            this.LJIJJ = null;
        }
    }

    public final void LIZ(Dialog dialog) {
        this.LJIIJ = new WeakReference<>(dialog);
    }

    public final void LIZ(int i) {
        LuH luH;
        if (this.LJFF != i) {
            this.LJFF = i;
            if (this.LJIIIIZZ.get() != null && (luH = this.LJIIJJI) != null) {
                luH.LIZ(i);
            }
        }
    }

    public static <V extends View> ViewPagerBottomSheetBehavior<V> LIZIZ(V v) {
        05s layoutParams = v.getLayoutParams();
        if (layoutParams instanceof 05s) {
            ViewPagerBottomSheetBehavior<V> viewPagerBottomSheetBehavior = layoutParams.LIZ;
            if (viewPagerBottomSheetBehavior instanceof ViewPagerBottomSheetBehavior) {
                return viewPagerBottomSheetBehavior;
            }
            throw new IllegalArgumentException("The view is not associated with ViewPagerBottomSheetBehavior");
        }
        throw new IllegalArgumentException("The view is not a child of CoordinatorLayout");
    }

    public final View LIZ(View view) {
        View LIZ2;
        if (view.isNestedScrollingEnabled()) {
            return view;
        }
        if (view instanceof 0GZ) {
            View focusedChild = ((0GZ) view).getFocusedChild();
            if (!(focusedChild == null || (LIZ2 = LIZ(focusedChild)) == null)) {
                return LIZ2;
            }
        } else if (view instanceof ViewGroup) {
            ViewGroup viewGroup = (ViewGroup) view;
            int childCount = viewGroup.getChildCount();
            for (int i = 0; i < childCount; i++) {
                View LIZ3 = LIZ(viewGroup.getChildAt(i));
                if (LIZ3 != null) {
                    return LIZ3;
                }
            }
        }
        return null;
    }

    private void LIZIZ(int i) {
        WeakReference<V> weakReference;
        View view;
        if (i == -1) {
            if (!this.LJIILLIIL) {
                this.LJIILLIIL = true;
            } else {
                return;
            }
        } else if (this.LJIILLIIL || this.LJIILL != i) {
            this.LJIILLIIL = false;
            this.LJIILL = Math.max(0, i);
            this.LIZJ = this.LJII - i;
        } else {
            return;
        }
        if (this.LJFF == 4 && (weakReference = this.LJIIIIZZ) != null && (view = (View) weakReference.get()) != null) {
            view.requestLayout();
        }
    }

    /* JADX WARNING: type inference failed for: r2v0, types: [android.os.Parcelable, com.ss.android.ugc.aweme.shortvideo.sticker.infoSticker.interact.consume.dialog.ViewPagerBottomSheetBehavior$SavedState] */
    public Parcelable onSaveInstanceState(1gt r4, V v) {
        return new SavedState(ViewPagerBottomSheetBehavior.super.onSaveInstanceState(r4, v), this.LJFF);
    }

    public ViewPagerBottomSheetBehavior(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        TypedArray obtainStyledAttributes = context.obtainStyledAttributes(attributeSet, new int[]{2130969075, 2130969076, 2130969079, 2130969080});
        TypedValue peekValue = obtainStyledAttributes.peekValue(2);
        if (peekValue == null || peekValue.data != -1) {
            LIZIZ(obtainStyledAttributes.getDimensionPixelSize(2, -1));
        } else {
            LIZIZ(peekValue.data);
        }
        this.LIZLLL = obtainStyledAttributes.getBoolean(1, false);
        this.LJ = obtainStyledAttributes.getBoolean(3, false);
        obtainStyledAttributes.recycle();
        this.LJIILJJIL = (float) ViewConfiguration.get(context).getScaledMaximumFlingVelocity();
    }

    public final void LIZ(View view, int i) {
        int i2;
        if (i == 4) {
            i2 = this.LIZJ;
        } else if (i == 3) {
            i2 = this.LIZIZ;
        } else if (!this.LIZLLL || i != 5) {
            throw new IllegalArgumentException("Illegal state argument: ".concat(String.valueOf(i)));
        } else {
            i2 = this.LJII;
        }
        if (this.LJI.LIZ(view, view.getLeft(), i2)) {
            LIZ(2);
            view.postOnAnimation(new T37(this, view, i));
            return;
        }
        LIZ(i);
    }

    public final boolean LIZ(View view, float f) {
        if (this.LJ) {
            return true;
        }
        if (view.getTop() >= this.LIZJ && Math.abs((((float) view.getTop()) + (f * 0.1f)) - ((float) this.LIZJ)) / ((float) this.LJIILL) > 0.5f) {
            return true;
        }
        return false;
    }

    public void onRestoreInstanceState(1gt r3, V v, Parcelable parcelable) {
        SavedState savedState = (SavedState) parcelable;
        ViewPagerBottomSheetBehavior.super.onRestoreInstanceState(r3, v, savedState.LIZLLL);
        if (savedState.LIZ == 1 || savedState.LIZ == 2) {
            this.LJFF = 4;
        } else {
            this.LJFF = savedState.LIZ;
        }
    }

    public boolean onTouchEvent(1gt r5, V v, MotionEvent motionEvent) {
        if (!v.isShown()) {
            return false;
        }
        int actionMasked = motionEvent.getActionMasked();
        if (this.LJFF == 1 && actionMasked == 0) {
            return true;
        }
        08w r0 = this.LJI;
        if (r0 != null) {
            r0.LIZIZ(motionEvent);
        }
        if (actionMasked == 0) {
            LIZIZ();
        }
        if (this.LJIJJ == null) {
            this.LJIJJ = VelocityTracker.obtain();
        }
        this.LJIJJ.addMovement(motionEvent);
        if (actionMasked == 2 && !this.LJIIZILJ && Math.abs(((float) this.LJIJJLI) - motionEvent.getY()) > ((float) this.LJI.LIZIZ)) {
            this.LJI.LIZ(v, motionEvent.getPointerId(motionEvent.getActionIndex()));
        }
        if (!this.LJIIZILJ) {
            return true;
        }
        return false;
    }

    public boolean onInterceptTouchEvent(1gt r8, V v, MotionEvent motionEvent) {
        boolean z;
        View view;
        if (!v.isShown()) {
            this.LJIIZILJ = true;
            return false;
        }
        int actionMasked = motionEvent.getActionMasked();
        if (actionMasked == 0) {
            LIZIZ();
        }
        if (this.LJIJJ == null) {
            this.LJIJJ = VelocityTracker.obtain();
        }
        this.LJIJJ.addMovement(motionEvent);
        if (actionMasked == 0) {
            int x = (int) motionEvent.getX();
            this.LJIJJLI = (int) motionEvent.getY();
            WeakReference<View> weakReference = this.LJIIIZ;
            if (!(weakReference == null || (view = (View) weakReference.get()) == null || !r8.LIZ(view, x, this.LJIJJLI))) {
                this.LJIIL = motionEvent.getPointerId(motionEvent.getActionIndex());
                this.LJIILIIL = true;
            }
            if (this.LJIIL != -1 || r8.LIZ(v, x, this.LJIJJLI)) {
                z = false;
            } else {
                z = true;
            }
            this.LJIIZILJ = z;
        } else if (actionMasked == 1 || actionMasked == 3) {
            this.LJIILIIL = false;
            this.LJIIL = -1;
            if (this.LJIIZILJ) {
                this.LJIIZILJ = false;
                return false;
            }
        }
        if (!this.LJIIZILJ && this.LJI.LIZ(motionEvent)) {
            return true;
        }
        View view2 = (View) this.LJIIIZ.get();
        if (actionMasked != 2 || view2 == null || this.LJIIZILJ || this.LJFF == 1 || r8.LIZ(view2, (int) motionEvent.getX(), (int) motionEvent.getY()) || Math.abs(((float) this.LJIJJLI) - motionEvent.getY()) <= ((float) this.LJI.LIZIZ)) {
            return false;
        }
        return true;
    }

    public boolean onLayoutChild(1gt r7, V v, int i) {
        Dialog dialog;
        int i2;
        if (r7.getFitsSystemWindows() && !v.getFitsSystemWindows()) {
            v.setFitsSystemWindows(true);
        }
        int top = v.getTop();
        try {
            r7.LIZ(v, i);
            this.LJII = r7.getHeight();
            if (this.LJIILLIIL) {
                if (this.LIZ == 0) {
                    this.LIZ = r7.getResources().getDimensionPixelSize(2131165517);
                }
                i2 = Math.max(this.LIZ, this.LJII - ((r7.getWidth() * 9) / 16));
            } else {
                i2 = this.LJIILL;
            }
            int max = Math.max(0, this.LJII - v.getHeight());
            this.LIZIZ = max;
            int max2 = Math.max(this.LJII - i2, max);
            this.LIZJ = max2;
            int i3 = this.LJFF;
            if (i3 == 3) {
                t.LIZIZ(v, this.LIZIZ);
            } else if (this.LIZLLL && i3 == 5) {
                t.LIZIZ(v, this.LJII);
            } else if (i3 == 4) {
                t.LIZIZ(v, max2);
            } else if (i3 == 1 || i3 == 2) {
                t.LIZIZ(v, top - v.getTop());
            }
            if (this.LJI == null) {
                this.LJI = 08w.LIZ(r7, this.LJIL);
            }
            this.LJIIIIZZ = new WeakReference<>(v);
            this.LJIIIZ = new WeakReference<>(LIZ((View) v));
            return true;
        } catch (Exception e) {
            if (!(this.LJIIJ == null || (dialog = (Dialog) this.LJIIJ.get()) == null || !dialog.isShowing())) {
                dialog.dismiss();
            }
            2kf.LIZ("", e);
            return true;
        }
    }

    public void onStopNestedScroll(1gt r5, V v, View view) {
        int i;
        int i2 = 3;
        if (v.getTop() == this.LIZIZ) {
            LIZ(3);
            return;
        }
        WeakReference<View> weakReference = this.LJIIIZ;
        if (weakReference != null && view == weakReference.get() && this.LJIJI) {
            if (this.LJIJ > 0) {
                i = this.LIZIZ;
            } else {
                if (this.LIZLLL) {
                    this.LJIJJ.computeCurrentVelocity(1000, this.LJIILJJIL);
                    if (LIZ((View) v, this.LJIJJ.getYVelocity(this.LJIIL))) {
                        i = this.LJII;
                        i2 = 5;
                    }
                }
                if (this.LJIJ == 0) {
                    int top = v.getTop();
                    if (Math.abs(top - this.LIZIZ) < Math.abs(top - this.LIZJ)) {
                        i = this.LIZIZ;
                    } else {
                        i = this.LIZJ;
                    }
                } else {
                    i = this.LIZJ;
                }
                i2 = 4;
            }
            if (this.LJI.LIZ(v, v.getLeft(), i)) {
                LIZ(2);
                v.postOnAnimation(new T37(this, v, i2));
            } else {
                LIZ(i2);
            }
            this.LJIJI = false;
        }
    }

    public boolean onStartNestedScroll(1gt r3, V v, View view, View view2, int i) {
        this.LJIJ = 0;
        this.LJIJI = false;
        if ((i & 2) != 0) {
            return true;
        }
        return false;
    }

    public boolean onNestedPreFling(1gt r3, V v, View view, float f, float f2) {
        if (view != this.LJIIIZ.get()) {
            return false;
        }
        if (this.LJFF != 3 || ViewPagerBottomSheetBehavior.super.onNestedPreFling(r3, v, view, f, f2)) {
            return true;
        }
        return false;
    }

    public void onNestedPreScroll(1gt r6, V v, View view, int i, int i2, int[] iArr) {
        if (view == this.LJIIIZ.get()) {
            int top = v.getTop();
            int i3 = top - i2;
            if (i2 > 0) {
                int i4 = this.LIZIZ;
                if (i3 < i4) {
                    iArr[1] = top - i4;
                    t.LIZIZ(v, -iArr[1]);
                    LIZ(3);
                } else {
                    iArr[1] = i2;
                    t.LIZIZ(v, -i2);
                    LIZ(1);
                }
            } else if (i2 < 0 && !view.canScrollVertically(-1)) {
                int i5 = this.LIZJ;
                if (i3 <= i5 || this.LIZLLL) {
                    iArr[1] = i2;
                    t.LIZIZ(v, -i2);
                    LIZ(1);
                } else {
                    iArr[1] = top - i5;
                    t.LIZIZ(v, -iArr[1]);
                    LIZ(4);
                }
            }
            v.getTop();
            LIZ();
            this.LJIJ = i2;
            this.LJIJI = true;
        }
    }
}
