package net.pokeranalytics.android.ui.view

import android.content.Context
import android.graphics.PointF
import android.util.DisplayMetrics
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.LinearSmoothScroller
import androidx.recyclerview.widget.RecyclerView

/**
 * SmoothScrollLinearLayoutManager
 */
class SmoothScrollLinearLayoutManager(context: Context) :
	LinearLayoutManager(context, RecyclerView.VERTICAL, false) {

	companion object {
		private const val MILLISECONDS_PER_INCH = 100f
	}

	override fun smoothScrollToPosition(
		recyclerView: RecyclerView, state: RecyclerView.State?,
		position: Int
	) {
		val smoothScroller = TopSnappedSmoothScroller(recyclerView.context)
		smoothScroller.targetPosition = position
		startSmoothScroll(smoothScroller)
	}

	private inner class TopSnappedSmoothScroller(context: Context) : LinearSmoothScroller(context) {

		override fun computeScrollVectorForPosition(targetPosition: Int): PointF? {
			return this@SmoothScrollLinearLayoutManager
				.computeScrollVectorForPosition(targetPosition)
		}

		override fun getVerticalSnapPreference(): Int {
			return SNAP_TO_START
		}

		override fun calculateSpeedPerPixel(displayMetrics: DisplayMetrics): Float {
			return MILLISECONDS_PER_INCH / displayMetrics.densityDpi
		}
	}


}
