package com.gh.gamecenter.common.view

import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import androidx.recyclerview.widget.RecyclerView
import kotlin.math.abs

class StackRecyclerView : RecyclerView {

    private var posX: Float = 0.0F
    private var posY: Float = 0.0F

    constructor(context: Context) : super(context)

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs)

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    )

    override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
        parent.requestDisallowInterceptTouchEvent(true)
        if (ev.action == MotionEvent.ACTION_DOWN) {
            posX = ev.x
            posY = ev.y
        } else if (ev.action == MotionEvent.ACTION_MOVE) {
            if (posY - ev.y > abs(posX - ev.x) || ev.y - posY > abs(posX - ev.x)) {
                parent.requestDisallowInterceptTouchEvent(false)
            } else {
                parent.requestDisallowInterceptTouchEvent(true)
            }
        }
        return super.dispatchTouchEvent(ev)
    }
}
