package com.gh.gamecenter.gamecollection.square

import android.content.Context
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.gh.common.exposure.*
import com.gh.common.util.DirectUtils
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
import com.gh.gamecenter.common.entity.ExposureEntity
import com.gh.gamecenter.common.utils.ImageUtils
import com.gh.gamecenter.common.utils.safelyGetInRelease
import com.gh.gamecenter.common.utils.toBinding
import com.gh.gamecenter.core.AppExecutor
import com.gh.gamecenter.databinding.GameCollectionBannerItemBinding
import com.gh.gamecenter.entity.AmwayCommentEntity
import com.gh.gamecenter.entity.CarouselEntity
import com.lightgame.adapter.BaseRecyclerAdapter

class GameCollectionBannerAdapter(
    context: Context,
    private val isHome: Boolean = false,
    var mBannerList: List<CarouselEntity> = emptyList(),
    private var mAmwayListItem: List<AmwayCommentEntity>? = null,
    private val mEntrance: String = "",
    private val mBasicExposureSource: List<ExposureSource>
) :
    BaseRecyclerAdapter<RecyclerView.ViewHolder>(context) {

    fun submitList(updateList: List<CarouselEntity>?, amwayList: List<AmwayCommentEntity>? = null) {
        mBannerList = if (updateList.isNullOrEmpty()) emptyList() else ArrayList(updateList)
        mAmwayListItem = if (amwayList.isNullOrEmpty()) emptyList() else ArrayList(amwayList)
        AppExecutor.uiExecutor.execute {
            notifyDataSetChanged()
        }
    }

    private fun shouldShowAmway(position: Int) = isHome && getActualPosition(position) == getActualSize() - 1

    override fun getItemViewType(position: Int) = if (shouldShowAmway(position)) ITEM_AMWAY else ITEM_BANNER

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
        if (viewType == ITEM_AMWAY) GameCollectionAmwayViewHolder(parent.toBinding()) else GameCollectionBannerViewHolder(
            parent.toBinding()
        )

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is GameCollectionBannerViewHolder && mBannerList.isNotEmpty()) {
            val entity = mBannerList.safelyGetInRelease(getActualPosition(position))
            if (entity != null) {
                holder.bindBanner(entity, getActualPosition(position))
            }
        } else if (holder is GameCollectionAmwayViewHolder) {
            mAmwayListItem?.let { holder.bindAmway(it) }
        }
    }

    override fun getItemCount() = if (getActualSize() <= 1) getActualSize() else Int.MAX_VALUE

    fun getActualPosition(position: Int): Int {
        var actualPosition = position
        val actualSize = getActualSize()
        if (actualSize in 1..position) {
            actualPosition = position % actualSize
        }
        return actualPosition
    }

    fun getActualSize() = if (isHome) mBannerList.size + 1 else mBannerList.size

    fun getActualFirstPositionInCenter(): Int {
        var index = itemCount / 2
        while (getActualPosition(index) != 0) {
            index++
        }
        return index
    }

    companion object {
        const val ITEM_AMWAY = 100
        const val ITEM_BANNER = 101
    }

    inner class GameCollectionBannerViewHolder(val binding: GameCollectionBannerItemBinding) :
        BaseRecyclerViewHolder<Any>(binding.root) {
        fun bindBanner(entity: CarouselEntity, position: Int) {
            ImageUtils.display(binding.bannerIv, entity.url)
            binding.root.setOnClickListener {
                DirectUtils.directToLinkPage(binding.root.context, entity, mEntrance, "轮播图")
                ExposureManager.log(
                    ExposureEvent(
                        payload = ExposureEntity(
                            carouselId = entity.id,
                            sequence = position
                        ),
                        source = ArrayList(mBasicExposureSource).apply {
                            add(ExposureSource(k = "轮播图"))
                        },
                        event = ExposureType.CLICK
                    )
                )
            }
        }
    }
}