package com.gh.gamecenter.game.columncollection.detail

import android.content.Context
import android.util.SparseArray
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.gh.common.exposure.IExposable
import com.gh.common.util.DirectUtils
import com.gh.common.util.NewLogUtils
import com.gh.gamecenter.R
import com.gh.gamecenter.adapter.viewholder.GameColumnCollectionItemViewHolder
import com.gh.gamecenter.common.baselist.ListAdapter
import com.gh.gamecenter.common.constant.ItemViewType
import com.gh.gamecenter.common.entity.LinkEntity
import com.gh.gamecenter.common.exposure.ExposureSource
import com.gh.gamecenter.common.utils.ImageUtils
import com.gh.gamecenter.common.utils.SensorsBridge
import com.gh.gamecenter.common.viewholder.FooterViewHolder
import com.gh.gamecenter.core.utils.MtaHelper
import com.gh.gamecenter.core.utils.PageSwitchDataHelper
import com.gh.gamecenter.databinding.GameColumnCollectionItemBinding
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.feature.exposure.ExposureConstants
import com.gh.gamecenter.feature.exposure.ExposureEvent

class ColumnCollectionDetailAdapter(
    context: Context,
    private val mViewModel: ColumnCollectionDetailViewModel,
    private var mTabIndex: Int,
    private val mBasicExposureSourceList: ArrayList<ExposureSource>?,
    private val mEntrance: String,
    private val mBlockId: String,
    private val mBlockName: String,
    private val mStyle: String
) : ListAdapter<LinkEntity>(context), IExposable {

    private var mExposureSparseArray = SparseArray<ExposureEvent>()

    override fun areItemsTheSame(oldItem: LinkEntity?, newItem: LinkEntity?): Boolean {
        return oldItem == newItem
    }

    override fun getItemViewType(position: Int): Int {
        if (position == itemCount - 1) {
            return ItemViewType.ITEM_FOOTER
        }
        return ItemViewType.ITEM_BODY
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val view: View
        return when (viewType) {
            ItemViewType.ITEM_FOOTER -> {
                view = mLayoutInflater.inflate(com.gh.gamecenter.common.R.layout.refresh_footerview, parent, false)
                FooterViewHolder(view)
            }
            else -> {
                view = mLayoutInflater.inflate(R.layout.game_column_collection_item, parent, false)
                GameColumnCollectionItemViewHolder(GameColumnCollectionItemBinding.bind(view))
            }
        }
    }

    override fun getItemCount(): Int {
        return if (mEntityList == null || mEntityList.isEmpty()) 0 else mEntityList.size + FOOTER_ITEM_COUNT
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is GameColumnCollectionItemViewHolder) {
            val data = mEntityList[position]
            ImageUtils.display(holder.binding.columnCollectionImage, data.image)
            holder.binding.columnCollectionImage.setOnClickListener {
                MtaHelper.onEvent("游戏专题合集", "详情图片", data.name)
                PageSwitchDataHelper.pushCurrentPageData(
                    hashMapOf(
                        Pair(PageSwitchDataHelper.PAGE_BUSINESS_TYPE, "专题合集"),
                        Pair(PageSwitchDataHelper.PAGE_BUSINESS_ID, mViewModel.collectionId),
                        Pair(PageSwitchDataHelper.PAGE_BUSINESS_NAME, mViewModel.columnCollection.value?.name ?: "")
                    )
                )
                DirectUtils.directToLinkPage(mContext, data, mEntrance, "专题合集", mExposureSparseArray.get(position))
                NewLogUtils.logColumnCategoryDetailContentClick(
                    data.name ?: "", data?.link ?: "",
                    mViewModel.columnCollection.value?.name ?: "", mViewModel.collectionId
                )
                SensorsBridge.trackColumnCollectionClick(
                    location = "合集详情",
                    blockName = mBlockName,
                    blockId = mBlockId,
                    columnCollectionName = mViewModel.columnCollection.value?.name ?: "",
                    columnCollectionId = mViewModel.collectionId,
                    position = position,
                    gameColumnName = data.name ?: "",
                    gameColumnId = data.link ?: "",
                    text = "游戏专题"
                )
            }

            val fakeGameEntity = GameEntity(_id = ExposureConstants.COLUMN_COLLECTION_DETAIL)
            fakeGameEntity.subPageCode = ExposureConstants.COLUMN_COLLECTION_DETAIL
            fakeGameEntity.subPageId = mViewModel.collectionId

            val exposureEvent = ExposureEvent.createEventWithSourceConcat(
                fakeGameEntity,
                mBasicExposureSourceList ?: arrayListOf(),
                arrayListOf(
                    ExposureSource("合集详情", ""),
                    ExposureSource("专题", "${data.name}+${mStyle}+${data.link}")
                )
            ).apply {
                payload.outerSequence = mTabIndex
                payload.sequence = position
            }
            mExposureSparseArray.put(position, exposureEvent)
        } else if (holder is FooterViewHolder) {
            holder.initFooterViewHolder(mViewModel, mIsLoading, mIsNetworkError, mIsOver)
            if (holder.itemView.layoutParams is StaggeredGridLayoutManager.LayoutParams) {
                (holder.itemView.layoutParams as StaggeredGridLayoutManager.LayoutParams).isFullSpan = true
            }
        }
    }

    override fun getEventByPosition(pos: Int): ExposureEvent? {
        return mExposureSparseArray.get(pos)
    }

    override fun getEventListByPosition(pos: Int) = null
}