package com.medicdigital.jjpodcasts.presentation.utils

import androidx.recyclerview.widget.OrientationHelper
import androidx.recyclerview.widget.PagerSnapHelper
import androidx.recyclerview.widget.RecyclerView
import android.view.View

class DoublePagerSnapHelper : androidx.recyclerview.widget.PagerSnapHelper() {

    //     Orientation helpers are lazily created per LayoutManager.
    private var mVerticalHelper: androidx.recyclerview.widget.OrientationHelper? = null
    private var mHorizontalHelper: androidx.recyclerview.widget.OrientationHelper? = null

    override fun calculateDistanceToFinalSnap(
        layoutManager: androidx.recyclerview.widget.RecyclerView.LayoutManager,
        targetView: View
    ): IntArray? {

        val out = IntArray(2)
        if (layoutManager.canScrollHorizontally()) {
            out[0] = distanceToCenter(
                layoutManager, targetView,
                getHorizontalHelper(layoutManager)
            )
        } else {
            out[0] = 0
        }

        if (layoutManager.canScrollVertically()) {
            out[1] = distanceToCenter(
                layoutManager, targetView,
                getVerticalHelper(layoutManager)
            )
        } else {
            out[1] = 0
        }
        return out
    }


    override fun findSnapView(layoutManager: androidx.recyclerview.widget.RecyclerView.LayoutManager): View? {
        if (layoutManager.canScrollVertically()) {
            return findCenterView(layoutManager, getVerticalHelper(layoutManager))
        } else if (layoutManager.canScrollHorizontally()) {
            return findCenterView(layoutManager, getHorizontalHelper(layoutManager))
        }
        return null
    }

    private fun findCenterView(
        layoutManager: androidx.recyclerview.widget.RecyclerView.LayoutManager,
        helper: androidx.recyclerview.widget.OrientationHelper
    ): View? {
        val childCount = layoutManager.childCount
        if (childCount == 0) {
            return null
        }

        val center: Int = if (layoutManager.clipToPadding) {
            helper.startAfterPadding + helper.totalSpace / 4
        } else {
            helper.end / 4
        }
        return (0 until childCount)
            .asSequence()
            .map { layoutManager.getChildAt(it) }
            .minBy {
                val childCenter =
                    helper.getDecoratedStart(it) + helper.getDecoratedMeasurement(it) / 2
                Math.abs(childCenter - center)
            }
    }

    private fun distanceToCenter(
        layoutManager: androidx.recyclerview.widget.RecyclerView.LayoutManager,
        targetView: View, helper: androidx.recyclerview.widget.OrientationHelper
    ): Int {

        val childSize = helper.getDecoratedMeasurement(targetView)
        val childCenter = helper.getDecoratedStart(targetView) + childSize / 2
        val containerCenter: Int = if (layoutManager.clipToPadding) {
            helper.startAfterPadding + helper.totalSpace / 2
        } else {
            helper.end / 2
        }
//        if (childCenter < childSize / 4) {
        if (childCenter < 0) {
            return childCenter - containerCenter + childSize * 3 / 2
        } else {
            return childCenter - containerCenter + childSize / 2
        }
    }

    private fun getVerticalHelper(layoutManager: androidx.recyclerview.widget.RecyclerView.LayoutManager): androidx.recyclerview.widget.OrientationHelper {
        if (mVerticalHelper == null || mVerticalHelper?.layoutManager !== layoutManager) {
            mVerticalHelper = androidx.recyclerview.widget.OrientationHelper.createVerticalHelper(layoutManager)
        }
        return mVerticalHelper!!
    }

    private fun getHorizontalHelper(layoutManager: androidx.recyclerview.widget.RecyclerView.LayoutManager): androidx.recyclerview.widget.OrientationHelper {
        if (mHorizontalHelper == null || mHorizontalHelper?.layoutManager !== layoutManager) {
            mHorizontalHelper = androidx.recyclerview.widget.OrientationHelper.createHorizontalHelper(layoutManager)
        }
        return mHorizontalHelper!!
    }

}