package com.gh.gamecenter.game.gallery

import android.content.Context
import android.view.ViewGroup
import androidx.activity.ComponentActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
import com.gh.gamecenter.feature.exposure.ExposureEvent
import com.gh.gamecenter.feature.exposure.ExposureSource
import com.gh.gamecenter.core.runOnIoThread
import com.gh.gamecenter.common.utils.toBinding
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.GameDetailActivity
import com.gh.gamecenter.R
import com.gh.gamecenter.databinding.GameGallerySlideItemBinding
import com.gh.gamecenter.databinding.ItemWithinGameGallerySlideBinding
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.entity.SubjectEntity
import com.lightgame.adapter.BaseRecyclerAdapter

class GameGallerySlideViewHolder(val binding: GameGallerySlideItemBinding) : BaseRecyclerViewHolder<Any>(binding.root) {

    private var mCachedSubject: SubjectEntity? = null
    private var mRecyclerViewList: ArrayList<RecyclerView>? = null

    private var mEntrance = ""
    private var mExposureClosure: ((ExposureEvent) -> Unit)? = null
    private var mBasicExposureSource: List<ExposureSource>? = null

    fun bindView(
        subjectEntity: SubjectEntity,
        basicExposureSource: List<ExposureSource>,
        entrance: String,
        exposureClosure: (ExposureEvent) -> Unit
    ) {
        if (subjectEntity.data == null) return

        resumeScrolling()

        if (subjectEntity == mCachedSubject) return

        mEntrance = entrance
        mExposureClosure = exposureClosure
        mBasicExposureSource = basicExposureSource

        mCachedSubject = subjectEntity
        mRecyclerViewList =
            arrayListOf(binding.firstRecyclerView, binding.secondRecyclerView, binding.thirdRecyclerView)

        for ((index, recyclerView) in mRecyclerViewList!!.withIndex()) {
            recyclerView.layoutManager = LinearLayoutManager(binding.root.context, RecyclerView.HORIZONTAL, false)
            recyclerView.adapter = GameGallerySlideAdapter(binding.root.context, getDividedGameList()[index])
            recyclerView.isNestedScrollingEnabled = false
        }

        binding.container.resumeScrolling()
        binding.container.setLifeCycleOwner((binding.root.context as ComponentActivity).lifecycle)
        binding.cardView.setCardBackgroundColor(R.color.text_FAFAFA.toColor(binding.root.context))
    }

    private fun getDividedGameList(): ArrayList<ArrayList<GameEntity>> {
        val containerList = arrayListOf<ArrayList<GameEntity>>()

        val bestNumber = mCachedSubject!!.data!!.size / 3

        containerList.add(ArrayList(mCachedSubject!!.data!!.subList(0, bestNumber - 1)))
        containerList.add(ArrayList(mCachedSubject!!.data!!.subList(bestNumber, bestNumber * 2 - 1)))
        containerList.add(ArrayList(mCachedSubject!!.data!!.subList(bestNumber * 2, mCachedSubject!!.data!!.size - 1)))
        return containerList
    }

    fun resumeScrolling() {
        binding.container.resumeScrolling()
    }

    fun pauseScrolling() {
        binding.container.pauseScrolling()
    }

    inner class GameGallerySlideAdapter(context: Context, var gameList: ArrayList<GameEntity>) :
        BaseRecyclerAdapter<GameGallerySlideAdapter.GameGallerySlideItemViewHolder>(context) {

        override fun getItemCount() = Int.MAX_VALUE

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): GameGallerySlideItemViewHolder {
            return GameGallerySlideItemViewHolder(parent.toBinding())
        }

        override fun onBindViewHolder(holder: GameGallerySlideItemViewHolder, position: Int) {
            val gameEntity = gameList[position % gameList.size]

            runOnIoThread(true) {
                val exposureEvent = ExposureEvent.createEventWithSourceConcat(
                    gameEntity,
                    mBasicExposureSource!!,
                    listOf(ExposureSource("专题", "${mCachedSubject?.name}-图集滚动"))
                )

                mExposureClosure?.invoke(exposureEvent)

                gameEntity.exposureEvent = exposureEvent
            }
            holder.bindView(gameEntity)
        }

        inner class GameGallerySlideItemViewHolder(val binding: ItemWithinGameGallerySlideBinding) :
            BaseRecyclerViewHolder<Any>(binding.root) {
            fun bindView(gameEntity: GameEntity) {
                binding.iconIv.displayGameIcon(gameEntity)
                binding.iconIv.setOnClickListener {
                    GameDetailActivity.startGameDetailActivity(
                        binding.root.context,
                        gameEntity.id,
                        mEntrance,
                        gameEntity.exposureEvent
                    )
                }
            }
        }

    }

}