package com.webtoonviewer.ui.component

import android.content.Context
import android.view.View
import androidx.recyclerview.widget.LinearSmoothScroller

class SmoothScroller(
    context: Context,
    private val offset: Int,
    private val speedFactor: String
    ) : LinearSmoothScroller(context) {

    override fun calculateDxToMakeVisible(view: View, snapPreference: Int): Int {
        val dx = super.calculateDxToMakeVisible(view, snapPreference)
        return dx + offset // 添加偏移量
    }

    override fun calculateDyToMakeVisible(view: View, snapPreference: Int): Int {
        val dy = super.calculateDyToMakeVisible(view, snapPreference)
        return dy + offset // 添加偏移量
    }

    // 控制滚动速度
    override fun calculateSpeedPerPixel(displayMetrics: android.util.DisplayMetrics): Float {
        return generateSpeed(speedFactor) / displayMetrics.densityDpi
    }

    override fun getVerticalSnapPreference(): Int {
        return SNAP_TO_START
    }

    private fun generateSpeed(speedFactor: String):Float {
        return when (speedFactor) {
            "1x" -> {
                2000f
            }
            "2x" -> {
                1600f
            }
            "3x" -> {
                1200f
            }
            "4x" -> {
                800f
            }
            "5x" -> {
                400f
            }
            else -> {
                2000f
            }
        }
    }
}