package com.gh.gamecenter.common.view

import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View

class VerticalViewPager @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) :
    androidx.viewpager.widget.ViewPager(context, attrs) {

    init {
        // The majority of the magic happens here
        setPageTransformer(true, VerticalPageTransformer())
        // The easiest way to get rid of the overscroll drawing that happens on the left and right
        overScrollMode = View.OVER_SCROLL_NEVER
    }

    private inner class VerticalPageTransformer : androidx.viewpager.widget.ViewPager.PageTransformer {
        override fun transformPage(view: View, position: Float) {
            if (position < -1) { // [-Infinity,-1)
                // This page is way off-screen to the left.
                view.alpha = 0F

            } else if (position <= 1) { // [-1,1]
                view.alpha = 1F

                // Counteract the default slide transition
                view.translationX = view.width * -position

                //set Y position to swipe in from top
                val yPosition = position * view.height
                view.translationY = yPosition

            } else { // (1,+Infinity]
                // This page is way off-screen to the right.
                view.alpha = 0F
            }
        }
    }

    /**
     * Swaps the X and Y coordinates of your touch event.
     */
    private fun swapXY(ev: MotionEvent): MotionEvent {
        val width = width.toFloat()
        val height = height.toFloat()

        val newX = ev.y / height * width
        val newY = ev.x / width * height

        ev.setLocation(newX, newY)

        return ev
    }

    override fun onInterceptTouchEvent(ev: MotionEvent): Boolean {
//        val intercepted = super.onInterceptTouchEvent(swapXY(ev))
        // return touch coordinates to original reference frame for any child views
//        swapXY(ev)
        // we do not need it view pager to intercept touch event
        return false
    }
}