package com.remax.visualnovel.app.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.airbnb.lottie.LottieAnimationView;
import com.remax.visualnovel.R;
import com.scwang.smart.refresh.layout.api.RefreshKernel;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.constant.RefreshState;
import com.scwang.smart.refresh.layout.constant.SpinnerStyle;
import com.scwang.smart.refresh.layout.util.SmartUtil;

public class RefreshHeader extends LinearLayout implements com.scwang.smart.refresh.layout.api.RefreshHeader {
    private final LottieAnimationView mProgressView;//刷新动画视图

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

    public RefreshHeader(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs, 0);
        setGravity(Gravity.CENTER);
        mProgressView = new LottieAnimationView(context);
        mProgressView.setAnimation(R.raw.single_ring);
        mProgressView.setSafeMode(true);
        mProgressView.setRepeatCount(-1);
        addView(mProgressView, SmartUtil.dp2px(26), SmartUtil.dp2px(26));
        setMinimumHeight(SmartUtil.dp2px(30));
    }

    @NonNull
    @Override
    public View getView() {
        return this;
    }

    @NonNull
    @Override
    public SpinnerStyle getSpinnerStyle() {
        return SpinnerStyle.Translate;
    }

    @Override
    public void setPrimaryColors(int... colors) {

    }

    @Override
    public void onInitialized(@NonNull RefreshKernel kernel, int height, int maxDragHeight) {

    }

    @Override
    public void onMoving(boolean isDragging, float percent, int offset, int height, int maxDragHeight) {
        if (isDragging){
            float currPer = percent / 3.0f;
            if (currPer > 1.0f) currPer = 1.0f;
            float frameF = mProgressView.getMaxFrame() * currPer;
            int frame = (int) (frameF);
            mProgressView.setFrame(frame);
            mProgressView.invalidate();
        }
    }

    @Override
    public void onReleased(@NonNull RefreshLayout refreshLayout, int height, int maxDragHeight) {
        mProgressView.setRepeatCount(-1);
        mProgressView.setProgress(0f);
        mProgressView.playAnimation();
    }

    @Override
    public void onStartAnimator(@NonNull RefreshLayout refreshLayout, int height, int maxDragHeight) {

    }

    @Override
    public int onFinish(@NonNull RefreshLayout refreshLayout, boolean success) {
        mProgressView.postDelayed(new Runnable() {
            @Override
            public void run() {
                mProgressView.cancelAnimation();
            }
        },1100);
        return 1000;
    }

    @Override
    public void onHorizontalDrag(float percentX, int offsetX, int offsetMax) {

    }

    @Override
    public boolean isSupportHorizontalDrag() {
        return false;
    }

    @Override
    public void onStateChanged(@NonNull RefreshLayout refreshLayout, @NonNull RefreshState oldState, @NonNull RefreshState newState) {

    }
}
