package com.gh.gamecenter.qa.article.detail

import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import androidx.core.view.NestedScrollingParent2
import androidx.core.view.ViewCompat
import androidx.recyclerview.widget.RecyclerView
import com.lightgame.utils.Utils

/**
 * 资讯详情-外层列表控件
 * 1. 处理RecyclerView嵌套的滑动冲突问题
 * 2. 实现类似CoordinateLayout的滑动效果
 */
class ArticleDetailRecyclerView @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null,
    defStyle: Int = 0
) : RecyclerView(context, attributeSet, defStyle), NestedScrollingParent2 {

    private var targetNestedScrollingView: View? = null

    override fun onInterceptTouchEvent(e: MotionEvent?): Boolean {
        val handled = super.onInterceptTouchEvent(e)
        targetNestedScrollingView?.let {
            // 执行外层RecyclerView的触摸事件时，停止内层RecyclerView的Fling滚动
            // 因为内层RecyclerView的Fling滚动有可能被外层RecyclerView消费
            // 如果这时外层RecyclerView正在执行Fling滚动，那么外层RecyclerView的Fling滚动会被覆盖掉，应避免这种情况发生
            ViewCompat.stopNestedScroll(it, ViewCompat.TYPE_NON_TOUCH)
        }
        return handled
    }

    override fun onStartNestedScroll(child: View, target: View, axes: Int, type: Int): Boolean {
        Utils.log("ArticleDetailRv->onStartNestedScroll->child: $child, $target, axes: $axes, type: $type")
        val handleNestedScroll = (axes and View.SCROLL_AXIS_VERTICAL != 0)
        if (handleNestedScroll) {
            if (type == ViewCompat.TYPE_TOUCH) {
                targetNestedScrollingView = target
            }
            child.parent.requestDisallowInterceptTouchEvent(true)
        }
        return handleNestedScroll
    }

    override fun onNestedScrollAccepted(child: View, target: View, axes: Int, type: Int) {
        Utils.log("ArticleDetailRv->onNestedScrollAccepted->child: $child, target: $target, axes: $axes, type: $type")
        onNestedScrollAccepted(child, target, axes)
    }

    override fun onStopNestedScroll(target: View, type: Int) {
        Utils.log("ArticleDetailRv->onStopNestedScroll->target: $target, type: $type")
        onStopNestedScroll(target)
        if (type == ViewCompat.TYPE_NON_TOUCH) {
            targetNestedScrollingView = null
        }
    }

    override fun onNestedPreScroll(target: View, dx: Int, dy: Int, consumed: IntArray, type: Int) {
        Utils.log("ArticleDetailRv->onNestedPreScroll->$target, dx: $dx, dy: $dy, consumed: $consumed, type: $type")
        // 该回调发生在内层RecyclerView滚动之前
        // 如果是向下滚动并且外层RecyclerView未滚动到底部，由外层RecyclerView进行滚动
        if (dy > 0 && canScrollVertically(1)) {
            Utils.log("ArticleDetailRv->onNestedPreScroll1->$target, dx: $dx, dy: $dy, consumed: $consumed, type: $type")
            scrollBy(0, dy)
            consumed[1] = dy
        } else {
            Utils.log("ArticleDetailRv->onNestedPreScroll2->$target, dx: $dx, dy: $dy, consumed: $consumed, type: $type")
            onNestedPreScroll(target, dx, dy, consumed)
        }
    }

    override fun onNestedScroll(
        target: View,
        dxConsumed: Int,
        dyConsumed: Int,
        dxUnconsumed: Int,
        dyUnconsumed: Int,
        type: Int
    ) {
        Utils.log("ArticleDetailRv->onNestedScroll->$target, dxConsumed: $dxConsumed, dyConsumed: $dyConsumed, dxUnconsumed: $dxUnconsumed, dyUnconsumed: $dyUnconsumed, type: $type")
        // 该回调发生在内层RecyclerView滚动之后
        // 如果是向上滚动并且内层RecyclerView滚动到顶部，由外层RecyclerView进行滚动
        if (dyConsumed == 0 && dyUnconsumed < 0) {
            Utils.log("ArticleDetailRv->onNestedScroll1->$target, dxConsumed: $dxConsumed, dyConsumed: $dyConsumed, dxUnconsumed: $dxUnconsumed, dyUnconsumed: $dyUnconsumed, type: $type")
            scrollBy(0, dyUnconsumed)
        } else {
            Utils.log("ArticleDetailRv->onNestedScroll2->$target, dxConsumed: $dxConsumed, dyConsumed: $dyConsumed, dxUnconsumed: $dxUnconsumed, dyUnconsumed: $dyUnconsumed, type: $type")
            onNestedScroll(target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed)
        }
    }

    override fun onNestedPreFling(target: View, velocityX: Float, velocityY: Float): Boolean {
        Utils.log("ArticleDetailRv->onNestedPreFling->$target, velocityX: $velocityX, velocityY: $velocityY")
        return super.onNestedPreFling(target, velocityX, velocityY)
    }

    override fun onNestedFling(target: View, velocityX: Float, velocityY: Float, consumed: Boolean): Boolean {
        Utils.log("ArticleDetailRv->onNestedFling->$target, velocityX: $velocityX, velocityY: $velocityY, consumed:$consumed")
        return super.onNestedFling(target, velocityX, velocityY, consumed)
    }
}