package xyz.stream.view.refresh.hearder;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.ImageView;
import androidx.annotation.ColorInt;
import androidx.annotation.ColorRes;
import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import g0.d;
import xyz.stream.view.R;
import xyz.stream.view.refresh.layout.api.RefreshHeader;
import xyz.stream.view.refresh.layout.api.RefreshKernel;
import xyz.stream.view.refresh.layout.api.RefreshLayout;
import xyz.stream.view.refresh.layout.constant.RefreshState;
import xyz.stream.view.refresh.layout.constant.SpinnerStyle;
import xyz.stream.view.refresh.layout.simple.SimpleComponent;
import xyz.stream.view.refresh.layout.util.AutoUtil;

/* JADX INFO: loaded from: classes3.dex */
public class MaterialHeader extends SimpleComponent implements RefreshHeader {
    protected static final int CIRCLE_BG_LIGHT = -328966;

    @VisibleForTesting
    protected static final int CIRCLE_DIAMETER = 40;

    @VisibleForTesting
    protected static final int CIRCLE_DIAMETER_LARGE = 72;
    protected static final float MAX_PROGRESS_ANGLE = 0.8f;
    public static final int SIZE_DEFAULT = 1;
    public static final int SIZE_LARGE = 0;
    protected Paint mBezierPaint;
    protected Path mBezierPath;
    protected int mCircleDiameter;
    protected ImageView mCircleView;
    protected boolean mFinished;
    protected int mHeadHeight;
    protected int mInitHeight;
    protected RefreshKernel mKernel;
    protected MaterialProgressDrawable mProgress;
    protected boolean mScrollableWhenRefreshing;
    protected boolean mShowBezierWave;
    protected RefreshState mState;
    protected int mWaveHeight;

    /* JADX INFO: renamed from: xyz.stream.view.refresh.hearder.MaterialHeader$2, reason: invalid class name */
    public static /* synthetic */ class AnonymousClass2 {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        public static final /* synthetic */ int[] f36564a;

        static {
            int[] iArr = new int[RefreshState.values().length];
            f36564a = iArr;
            try {
                iArr[RefreshState.None.ordinal()] = 1;
            } catch (NoSuchFieldError unused) {
            }
            try {
                f36564a[RefreshState.ReleaseToRefresh.ordinal()] = 2;
            } catch (NoSuchFieldError unused2) {
            }
            try {
                f36564a[RefreshState.Refreshing.ordinal()] = 3;
            } catch (NoSuchFieldError unused3) {
            }
            try {
                f36564a[RefreshState.PullDownToRefresh.ordinal()] = 4;
            } catch (NoSuchFieldError unused4) {
            }
        }
    }

    public MaterialHeader(Context context) {
        this(context, null);
    }

    @Override // xyz.stream.view.refresh.layout.simple.SimpleComponent, xyz.stream.view.refresh.layout.api.RefreshComponent
    public boolean autoOpen(int i10, float f10, final boolean z10) {
        if (this.mShowBezierWave || this.mKernel == null) {
            return false;
        }
        final ImageView imageView = this.mCircleView;
        imageView.setAlpha(1.0f);
        imageView.setScaleX(0.0f);
        imageView.setScaleY(0.0f);
        imageView.setTranslationY((this.mCircleDiameter / 2.0f) + (this.mInitHeight / 2.0f));
        imageView.animate().scaleX(1.0f).scaleY(1.0f).setDuration(i10).setListener(new AnimatorListenerAdapter() { // from class: xyz.stream.view.refresh.hearder.MaterialHeader.1
            @Override // android.animation.AnimatorListenerAdapter, android.animation.Animator.AnimatorListener
            public void onAnimationEnd(Animator animator) {
                imageView.animate().setListener(null);
                MaterialHeader materialHeader = MaterialHeader.this;
                materialHeader.mProgress.start();
                materialHeader.mKernel.onAutoRefreshAnimationEnd(animator, z10);
            }
        });
        return true;
    }

    @Override // android.view.ViewGroup, android.view.View
    public void dispatchDraw(@NonNull Canvas canvas) {
        if (this.mShowBezierWave) {
            this.mBezierPath.reset();
            this.mBezierPath.lineTo(0.0f, this.mHeadHeight);
            this.mBezierPath.quadTo(getMeasuredWidth() / 2.0f, (this.mWaveHeight * 1.9f) + this.mHeadHeight, getMeasuredWidth(), this.mHeadHeight);
            this.mBezierPath.lineTo(getMeasuredWidth(), 0.0f);
            canvas.drawPath(this.mBezierPath, this.mBezierPaint);
        }
        super.dispatchDraw(canvas);
    }

    @Override // xyz.stream.view.refresh.layout.simple.SimpleComponent, xyz.stream.view.refresh.layout.api.RefreshComponent
    public int onFinish(@NonNull RefreshLayout refreshLayout, boolean z10) {
        ImageView imageView = this.mCircleView;
        this.mProgress.stop();
        imageView.animate().scaleX(0.0f).scaleY(0.0f);
        this.mFinished = true;
        return 0;
    }

    @Override // xyz.stream.view.refresh.layout.simple.SimpleComponent, xyz.stream.view.refresh.layout.api.RefreshComponent
    public void onInitialized(@NonNull RefreshKernel refreshKernel, int i10, int i11) {
        if (!this.mShowBezierWave) {
            refreshKernel.requestDefaultTranslationContentFor(this, false);
        }
        if (isInEditMode()) {
            int i12 = i10 / 2;
            this.mHeadHeight = i12;
            this.mWaveHeight = i12;
        }
        this.mKernel = refreshKernel;
        this.mInitHeight = i10;
    }

    @Override // android.widget.RelativeLayout, android.view.ViewGroup, android.view.View
    public void onLayout(boolean z10, int i10, int i11, int i12, int i13) {
        int i14;
        if (getChildCount() == 0) {
            return;
        }
        ImageView imageView = this.mCircleView;
        int measuredWidth = getMeasuredWidth();
        int measuredWidth2 = imageView.getMeasuredWidth();
        int measuredHeight = imageView.getMeasuredHeight();
        if (!isInEditMode() || (i14 = this.mHeadHeight) <= 0) {
            int i15 = measuredWidth / 2;
            int i16 = measuredWidth2 / 2;
            imageView.layout(i15 - i16, -measuredHeight, i15 + i16, 0);
            return;
        }
        int i17 = i14 - (measuredHeight / 2);
        int i18 = measuredWidth / 2;
        int i19 = measuredWidth2 / 2;
        imageView.layout(i18 - i19, i17, i18 + i19, measuredHeight + i17);
        this.mProgress.showArrow(true);
        this.mProgress.setStartEndTrim(0.0f, MAX_PROGRESS_ANGLE);
        this.mProgress.setArrowScale(1.0f);
        imageView.setAlpha(1.0f);
        imageView.setVisibility(0);
    }

    @Override // android.widget.RelativeLayout, android.view.View
    public void onMeasure(int i10, int i11) {
        super.setMeasuredDimension(View.MeasureSpec.getSize(i10), View.MeasureSpec.getSize(i11));
        this.mCircleView.measure(View.MeasureSpec.makeMeasureSpec(this.mCircleDiameter, 1073741824), View.MeasureSpec.makeMeasureSpec(this.mCircleDiameter, 1073741824));
    }

    @Override // xyz.stream.view.refresh.layout.simple.SimpleComponent, xyz.stream.view.refresh.layout.api.RefreshComponent
    public void onMoving(boolean z10, float f10, int i10, int i11, int i12) {
        RefreshState refreshState = this.mState;
        RefreshState refreshState2 = RefreshState.Refreshing;
        if (refreshState == refreshState2) {
            return;
        }
        if (this.mShowBezierWave) {
            this.mHeadHeight = Math.min(i10, i11);
            this.mWaveHeight = Math.max(0, i10 - i11);
            postInvalidate();
        }
        if (z10 || !(this.mProgress.isRunning() || this.mFinished)) {
            if (this.mState != refreshState2) {
                float f11 = i11;
                float fMax = (((float) Math.max(((double) Math.min(1.0f, Math.abs((i10 * 1.0f) / f11))) - 0.4d, 0.0d)) * 5.0f) / 3.0f;
                double dMax = Math.max(0.0f, Math.min(Math.abs(i10) - i11, f11 * 2.0f) / f11) / 4.0f;
                float fPow = ((float) (dMax - Math.pow(dMax, 2.0d))) * 2.0f;
                float f12 = fMax * MAX_PROGRESS_ANGLE;
                this.mProgress.showArrow(true);
                this.mProgress.setStartEndTrim(0.0f, Math.min(MAX_PROGRESS_ANGLE, f12));
                this.mProgress.setArrowScale(Math.min(1.0f, fMax));
                this.mProgress.setProgressRotation(((fPow * 2.0f) + ((fMax * 0.4f) - 0.25f)) * 0.5f);
            }
            ImageView imageView = this.mCircleView;
            float f13 = i10;
            imageView.setTranslationY(Math.min(f13, (this.mCircleDiameter / 2.0f) + (f13 / 2.0f)));
            imageView.setAlpha(Math.min(1.0f, (f13 * 4.0f) / this.mCircleDiameter));
        }
    }

    @Override // xyz.stream.view.refresh.layout.simple.SimpleComponent, xyz.stream.view.refresh.layout.api.RefreshComponent
    public void onStartAnimator(@NonNull RefreshLayout refreshLayout, int i10, int i11) {
        this.mProgress.start();
    }

    @Override // xyz.stream.view.refresh.layout.simple.SimpleComponent, xyz.stream.view.refresh.layout.listener.OnStateChangedListener
    public void onStateChanged(@NonNull RefreshLayout refreshLayout, @NonNull RefreshState refreshState, @NonNull RefreshState refreshState2) {
        ImageView imageView = this.mCircleView;
        this.mState = refreshState2;
        if (AnonymousClass2.f36564a[refreshState2.ordinal()] != 4) {
            return;
        }
        this.mFinished = false;
        imageView.setVisibility(0);
        imageView.setTranslationY(0.0f);
        imageView.setScaleX(1.0f);
        imageView.setScaleY(1.0f);
    }

    public MaterialHeader setColorSchemeColors(@ColorInt int... iArr) {
        this.mProgress.setColorSchemeColors(iArr);
        return this;
    }

    public MaterialHeader setColorSchemeResources(@ColorRes int... iArr) {
        Context context = getContext();
        int[] iArr2 = new int[iArr.length];
        for (int i10 = 0; i10 < iArr.length; i10++) {
            int i11 = iArr[i10];
            Object obj = d.f26125a;
            iArr2[i10] = context.getColor(i11);
        }
        return setColorSchemeColors(iArr2);
    }

    @Override // xyz.stream.view.refresh.layout.simple.SimpleComponent, xyz.stream.view.refresh.layout.api.RefreshComponent
    @Deprecated
    public void setPrimaryColors(@ColorInt int... iArr) {
        if (iArr.length > 0) {
            this.mBezierPaint.setColor(iArr[0]);
        }
    }

    public MaterialHeader setProgressBackgroundColorSchemeColor(@ColorInt int i10) {
        this.mCircleView.setBackgroundColor(i10);
        return this;
    }

    public MaterialHeader setProgressBackgroundColorSchemeResource(@ColorRes int i10) {
        Context context = getContext();
        Object obj = d.f26125a;
        setProgressBackgroundColorSchemeColor(context.getColor(i10));
        return this;
    }

    public MaterialHeader setScrollableWhenRefreshing(boolean z10) {
        this.mScrollableWhenRefreshing = z10;
        return this;
    }

    public MaterialHeader setShowBezierWave(boolean z10) {
        this.mShowBezierWave = z10;
        return this;
    }

    public MaterialHeader setSize(int i10) {
        if (i10 != 0 && i10 != 1) {
            return this;
        }
        DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
        if (i10 == 0) {
            this.mCircleDiameter = (int) (displayMetrics.density * 72.0f);
        } else {
            this.mCircleDiameter = (int) (displayMetrics.density * 40.0f);
        }
        this.mCircleView.setImageDrawable(null);
        this.mProgress.updateSizes(i10);
        this.mCircleView.setImageDrawable(this.mProgress);
        return this;
    }

    public MaterialHeader(Context context, AttributeSet attributeSet) {
        super(context, attributeSet, 0);
        this.mShowBezierWave = false;
        this.mScrollableWhenRefreshing = true;
        this.mSpinnerStyle = SpinnerStyle.MatchLayout;
        setMinimumHeight(AutoUtil.dp2px(100.0f));
        MaterialProgressDrawable materialProgressDrawable = new MaterialProgressDrawable(this);
        this.mProgress = materialProgressDrawable;
        materialProgressDrawable.setColorSchemeColors(-16737844, -48060, -10053376, -5609780, -30720);
        CircleImageView circleImageView = new CircleImageView(context, CIRCLE_BG_LIGHT);
        this.mCircleView = circleImageView;
        circleImageView.setImageDrawable(this.mProgress);
        this.mCircleView.setAlpha(0.0f);
        addView(this.mCircleView);
        this.mCircleDiameter = (int) (getResources().getDisplayMetrics().density * 40.0f);
        this.mBezierPath = new Path();
        Paint paint = new Paint();
        this.mBezierPaint = paint;
        paint.setAntiAlias(true);
        this.mBezierPaint.setStyle(Paint.Style.FILL);
        TypedArray typedArrayObtainStyledAttributes = context.obtainStyledAttributes(attributeSet, R.styleable.MaterialHeader);
        this.mShowBezierWave = typedArrayObtainStyledAttributes.getBoolean(R.styleable.MaterialHeader_srlShowBezierWave, this.mShowBezierWave);
        this.mScrollableWhenRefreshing = typedArrayObtainStyledAttributes.getBoolean(R.styleable.MaterialHeader_srlScrollableWhenRefreshing, this.mScrollableWhenRefreshing);
        int i10 = R.styleable.MaterialHeader_srlShadowRadius;
        if (typedArrayObtainStyledAttributes.hasValue(i10)) {
            this.mBezierPaint.setShadowLayer(typedArrayObtainStyledAttributes.getDimensionPixelOffset(i10, 0), 0.0f, 0.0f, typedArrayObtainStyledAttributes.getColor(R.styleable.MaterialHeader_mhShadowColor, -16777216));
            setLayerType(1, null);
        }
        this.mShowBezierWave = typedArrayObtainStyledAttributes.getBoolean(R.styleable.MaterialHeader_mhShowBezierWave, this.mShowBezierWave);
        this.mScrollableWhenRefreshing = typedArrayObtainStyledAttributes.getBoolean(R.styleable.MaterialHeader_mhScrollableWhenRefreshing, this.mScrollableWhenRefreshing);
        int i11 = R.styleable.MaterialHeader_mhPrimaryColor;
        if (typedArrayObtainStyledAttributes.hasValue(i11)) {
            this.mBezierPaint.setColor(typedArrayObtainStyledAttributes.getColor(i11, -15614977));
        }
        int i12 = R.styleable.MaterialHeader_mhShadowRadius;
        if (typedArrayObtainStyledAttributes.hasValue(i12)) {
            this.mBezierPaint.setShadowLayer(typedArrayObtainStyledAttributes.getDimensionPixelOffset(i12, 0), 0.0f, 0.0f, typedArrayObtainStyledAttributes.getColor(R.styleable.MaterialHeader_mhShadowColor, -16777216));
            setLayerType(1, null);
        }
        typedArrayObtainStyledAttributes.recycle();
    }
}
