package xn.xxp.widget;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class AutoScrollRecyclerView extends RecyclerView {
    private static final int SCROLL_DELAY_MS = 20;
    private static final int SCROLL_PIXELS = 5;
    private boolean isScrollingForward = true;
    private final Handler scrollHandler = new Handler(Looper.getMainLooper());
    private final Runnable scrollRunnable = new Runnable() {
        @Override
        public void run() {
            if (getLayoutManager() == null || getAdapter() == null) {
                return;
            }

            LinearLayoutManager layoutManager = (LinearLayoutManager) getLayoutManager();
            int firstVisiblePosition = layoutManager.findFirstVisibleItemPosition();
            int lastVisiblePosition = layoutManager.findLastVisibleItemPosition();
            int itemCount = getAdapter().getItemCount();

            // 检查是否到达边界
            if (isScrollingForward && lastVisiblePosition == itemCount - 1) {
                View lastView = layoutManager.findViewByPosition(lastVisiblePosition);
                if (lastView != null && layoutManager.getDecoratedRight(lastView) <= getWidth()) {
                    isScrollingForward = false;
                }
            } else if (!isScrollingForward && firstVisiblePosition == 0) {
                View firstView = layoutManager.findViewByPosition(0);
                if (firstView != null && layoutManager.getDecoratedLeft(firstView) >= 0) {
                    isScrollingForward = true;
                }
            }

            // 根据方向滚动
            if (isScrollingForward) {
                smoothScrollBy(SCROLL_PIXELS, 0);
            } else {
                smoothScrollBy(-SCROLL_PIXELS, 0);
            }

            scrollHandler.postDelayed(this, SCROLL_DELAY_MS);
        }
    };

    public AutoScrollRecyclerView(@NonNull Context context) {
        super(context);
        init();
    }

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

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

    private void init() {
        setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
    }

    @Override
    public boolean onTouchEvent(MotionEvent e) {
        // 禁用用户操作
        return false;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent e) {
        // 禁用用户操作
        return false;
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        // 开始自动滚动
        startAutoScroll();
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        // 停止滚动，防止内存泄漏
        stopAutoScroll();
    }

    public void startAutoScroll() {
        stopAutoScroll(); // 先停止之前的滚动
        scrollHandler.postDelayed(scrollRunnable, SCROLL_DELAY_MS);
    }

    public void stopAutoScroll() {
        scrollHandler.removeCallbacks(scrollRunnable);
    }
}