package com.cooleshow.base.widgets;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import com.airbnb.lottie.LottieAnimationView;
import com.cooleshow.base.R;
import com.cooleshow.base.utils.LogUtils;
import com.scwang.smart.refresh.layout.api.RefreshHeader;
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 androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * Author by pq, Date on 2022/7/29.
 */
public class CustomRefreshHeader extends FrameLayout implements RefreshHeader {

    private LottieAnimationView mViewAnim;

    public CustomRefreshHeader(@NonNull Context context) {
        this(context, null);
    }

    public CustomRefreshHeader(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public CustomRefreshHeader(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        LayoutInflater.from(getContext()).inflate(R.layout.view_custom_refresh_header_layout, this);
        mViewAnim = findViewById(R.id.view_anim);
        mViewAnim.setAnimation("lottie/refresh_anim.json");
        mViewAnim.loop(true);
        mViewAnim.setVisibility(View.GONE);
    }

    @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) {
        LogUtils.i("pq", "onMoving isDragging:" + isDragging + "\npercent:" + percent + "\noffset:" + offset
                + "\nheight:" + height + "\nmaxDragHeight:" + maxDragHeight);
    }

    @Override
    public void onReleased(@NonNull RefreshLayout refreshLayout, int height, int maxDragHeight) {
        LogUtils.i("pq", "onReleased height:" + height + "\nmaxDragHeight:" + maxDragHeight);
    }

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

    }

    @Override
    public int onFinish(@NonNull RefreshLayout refreshLayout, boolean success) {
        if (mViewAnim != null) {
            mViewAnim.cancelAnimation();
            mViewAnim.clearAnimation();
        }
        LogUtils.i("pq", "onFinish");
        return 500;//延迟500毫秒之后再弹回;
    }

    @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) {
        switch (newState) {
            case None:
                LogUtils.i("pq", "None");
                mViewAnim.setFrame(0);
                mViewAnim.setProgress(0);
                break;
            case PullDownToRefresh:
                LogUtils.i("pq", "PullDownToRefresh");
                playAnim();
                mViewAnim.setVisibility(View.VISIBLE);
                break;
            case PullDownCanceled:
                if (mViewAnim != null) {
                    clearAnim();
                }
                LogUtils.i("pq", "PullDownCanceled");
                break;
            case ReleaseToRefresh:
                LogUtils.i("pq", "ReleaseToRefresh");
                mViewAnim.setVisibility(View.VISIBLE);
                break;
            case Refreshing:
                LogUtils.i("pq", "Refreshing");
                break;
            case RefreshReleased:
                LogUtils.i("pq", "RefreshReleased");
                break;
            case RefreshFinish:
                LogUtils.i("pq", "RefreshFinish");
                break;
        }
    }

    private void playAnim() {
        if (mViewAnim != null) {
            mViewAnim.playAnimation();
        }
    }

    private void clearAnim() {
        if (mViewAnim != null) {
            mViewAnim.cancelAnimation();
            mViewAnim.clearAnimation();
        }
    }
}
