package mtp.polymer.com.autowidget.recyclerview

import androidx.recyclerview.widget.RecyclerView
import cn.muchinfo.rma.view.autoWidget.autoSize
import android.view.View



class CardScaleHelper {
    private var mRecyclerView: RecyclerView? = null

    private var mScale = 0.85f // 两边视图scale
    private var mPagePadding = autoSize(15) // 卡片的padding, 卡片间的距离等于2倍的mPagePadding
    private var mShowLeftCardWidth = autoSize(15)   // 左边卡片显示大小

    private var mCardWidth: Int = 0 // 卡片宽度
    private var mOnePageWidth: Int = 0 // 滑动一页的距离
    private var mCardGalleryWidth: Int = 0

    var currentItemPos: Int = 0
    private var mCurrentItemOffset: Int = 0

    private val mLinearSnapHelper = CardLinearSnapHelper()

    fun attachToRecyclerView(mRecyclerView: RecyclerView) {
        // 开启log会影响滑动体验, 调试时才开启
        //        LogUtils.mLogEnable = false;
        this.mRecyclerView = mRecyclerView
        mRecyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                if (recyclerView != null) {
                    super.onScrollStateChanged(recyclerView, newState)
                }
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    mLinearSnapHelper.mNoNeedToScroll = mCurrentItemOffset == 0 || mCurrentItemOffset == getDestItemOffset(mRecyclerView.adapter?.itemCount ?: 1 - 1)
                } else {
                    mLinearSnapHelper.mNoNeedToScroll = false
                }
            }

            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                if (recyclerView != null) {
                    super.onScrolled(recyclerView, dx, dy)
                }
                // dx>0则表示右滑, dx<0表示左滑, dy<0表示上滑, dy>0表示下滑
                mCurrentItemOffset += dx
                computeCurrentItemPos()
                //                LogUtils.v(String.format("dx=%s, dy=%s, mScrolledX=%s", dx, dy, mCurrentItemOffset));
                onScrolledChangedCallback()
            }
        })

        initWidth()
        mLinearSnapHelper.attachToRecyclerView(mRecyclerView)
    }

    /**
     * 初始化卡片宽度
     */
    private fun initWidth() {
        mRecyclerView?.apply {
            post {
                mCardGalleryWidth = width
                mCardWidth = mCardGalleryWidth - (2 * (mPagePadding + mShowLeftCardWidth))
                mOnePageWidth = mCardWidth
                mRecyclerView!!.smoothScrollToPosition(currentItemPos)
                onScrolledChangedCallback()
            }
        }
    }

    private fun getDestItemOffset(destPos: Int): Int {
        return mOnePageWidth * destPos
    }

    /**
     * 计算mCurrentItemOffset
     */
    private fun computeCurrentItemPos() {
        if (mOnePageWidth <= 0) return
        var pageChanged = false
        // 滑动超过一页说明已翻页
        if (Math.abs(mCurrentItemOffset - currentItemPos * mOnePageWidth) >= mOnePageWidth) {
            pageChanged = true
        }
        if (pageChanged) {
            val tempPos = currentItemPos
            currentItemPos = mCurrentItemOffset / mOnePageWidth
            //            LogUtils.d(String.format("=======onCurrentItemPos Changed======= tempPos=%s, mCurrentItemPos=%s", tempPos, mCurrentItemPos));
        }
    }

    /**
     * RecyclerView位移事件监听, view大小随位移事件变化
     */
    private fun onScrolledChangedCallback() {
        val offset = mCurrentItemOffset - currentItemPos * mOnePageWidth
        val percent = Math.max(Math.abs(offset) * 1.0 / mOnePageWidth, 0.0001).toFloat()
        //        LogUtils.d(String.format("offset=%s, percent=%s", offset, percent));
        val leftView: View? = currentItemPos.takeIf { it > 0 }?.let { mRecyclerView?.layoutManager?.findViewByPosition(currentItemPos - 1) }
        val currentView: View? = mRecyclerView?.layoutManager?.findViewByPosition(currentItemPos)
        val rightView: View? = mRecyclerView?.also { it.takeIf { currentItemPos < (it.adapter?.itemCount
            ?: 1) - 1 } }?.let { it.layoutManager?.findViewByPosition(currentItemPos + 1) }
        // y = (1 - mScale)x + mScale
        val scale = if (mRecyclerView?.adapter?.itemCount == 1) 1f else mScale
        leftView?.scaleY = (1 - scale) * percent + scale
        // y = (mScale - 1)x + 1
        currentView?.scaleY = (scale - 1) * percent + 1
        // y = (1 - mScale)x + mScale
        rightView?.scaleY = (1 - scale) * percent + scale
    }

    fun setScale(scale: Float) {
        mScale = scale
    }

    fun setPagePadding(pagePadding: Int) {
        mPagePadding = pagePadding
    }

    fun setShowLeftCardWidth(showLeftCardWidth: Int) {
        mShowLeftCardWidth = showLeftCardWidth
    }
}
