package com.gh.gamecenter.home.gamecollection

import android.os.Handler
import android.os.Looper
import android.os.Message
import android.view.MotionEvent
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
import com.gh.gamecenter.core.utils.DisplayUtils
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.view.stacklayoutmanager.StackLayoutManager
import com.gh.gamecenter.databinding.HomeGameCollectionItemBinding
import com.gh.gamecenter.gamecollection.square.GameCollectionListItemData
import java.lang.ref.WeakReference

class HomeGameCollectionViewHolder(val binding: HomeGameCollectionItemBinding) :
    BaseRecyclerViewHolder<Any>(binding.root) {

    private var mAdapter: HomeGameCollectionAdapter? = null
    private var mLayoutManager: StackLayoutManager? = null
    private val mLooperHandle = LooperHandle(this)
    private val mSlideLooperKey = 222

    fun bindGameCollectionList(gameCollectionItemDataList: List<GameCollectionListItemData>, entrance: String) {
        if (gameCollectionItemDataList == mAdapter?.gameCollectionItemDataList) {
            return
        } else if (binding.recyclerView.adapter is HomeGameCollectionAdapter) {
            mAdapter?.gameCollectionItemDataList = gameCollectionItemDataList
            mAdapter?.notifyDataSetChanged()
            binding.recyclerView.scrollToPosition(0)
            startAutoPlay()
            return
        }
        mAdapter = HomeGameCollectionAdapter(binding.root.context, entrance, gameCollectionItemDataList)
        mLayoutManager = StackLayoutManager(
            StackLayoutManager.ScrollOrientation.RIGHT_TO_LEFT,
            3,
            GameCollectionStackAnimation::class.java,
            GameCollectionStackLayout::class.java
        ).apply { setPagerFlingVelocity(DisplayUtils.getScreenWidth() / 2) }
        mLayoutManager?.setItemOffset(4F.dip2px())
        binding.recyclerView.run {
            layoutManager = mLayoutManager
            adapter = mAdapter
            isNestedScrollingEnabled = false
            addOnScrollListener(object : RecyclerView.OnScrollListener() {
                override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                    super.onScrollStateChanged(recyclerView, newState)
                    if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                        binding.recyclerView.post {
                            binding.recyclerView.suppressLayout(false)
                        }
                    } else if (newState == RecyclerView.SCROLL_STATE_SETTLING) {
                        binding.recyclerView.suppressLayout(true)
                    }
                }
            })
            addOnItemTouchListener(object : RecyclerView.SimpleOnItemTouchListener() {
                override fun onInterceptTouchEvent(rv: RecyclerView, e: MotionEvent): Boolean {
                    val isStop = e.action == MotionEvent.ACTION_DOWN || e.action == MotionEvent.ACTION_MOVE
                    if (isStop) stopAutoPlay() else startAutoPlay()
                    return false
                }
            })
        }
        startAutoPlay()
    }

    private fun scrollToNextPage() {
        mLayoutManager?.run {
            binding.recyclerView.smoothScrollToPosition(getFirstVisibleItemPosition() + 1)
        }
    }

    private fun startAutoPlay() {
        if (mAdapter?.itemCount ?: 0 <= 1) return
        stopAutoPlay()
        mLooperHandle.sendEmptyMessageDelayed(mSlideLooperKey, HOME_GAME_COLLECTION_LOOP_TIME)
    }

    private fun stopAutoPlay() {
        mLooperHandle.removeMessages(mSlideLooperKey)
    }

    companion object {
        const val HOME_GAME_COLLECTION_LOOP_TIME = 5000L
    }

    class LooperHandle(viewHolder: HomeGameCollectionViewHolder) : Handler(
        Looper.getMainLooper()
    ) {
        private val mWeakReference: WeakReference<HomeGameCollectionViewHolder> = WeakReference(viewHolder)

        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)
            val viewHolder = mWeakReference.get()
            if (viewHolder != null && msg.what == viewHolder.mSlideLooperKey) {
                viewHolder.scrollToNextPage()
                viewHolder.startAutoPlay()
            }
        }
    }
}