package com.gh.gamecenter.game.commoncollection.detail

import android.content.Context
import android.util.SparseArray
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.gh.common.exposure.IExposable
import com.gh.common.util.DirectUtils
import com.gh.common.util.NewLogUtils
import com.gh.gamecenter.adapter.viewholder.CommonCollectionDetailOneItemViewHolder
import com.gh.gamecenter.adapter.viewholder.CommonCollectionDetailTwoItemViewHolder
import com.gh.gamecenter.adapter.viewholder.CommonCollectionImageTextItemViewHolder
import com.gh.gamecenter.common.baselist.ListAdapter
import com.gh.gamecenter.common.constant.ItemViewType
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.utils.goneIf
import com.gh.gamecenter.common.utils.toBinding
import com.gh.gamecenter.common.viewholder.FooterViewHolder
import com.gh.gamecenter.entity.CommonCollectionContentEntity
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.feature.exposure.ExposureEvent

class CommonCollectionDetailAdapter(
    context: Context,
    var collectionStyle: String,
    val mViewModel: CommonCollectionDetailViewModel,
    val mBlockId: String,
    val mBlockName: String,
    val mLocation: String,
    val mTabIndex: Int,
    val mEntrance: String,
    var basicExposureSource: List<ExposureSource>?
) : ListAdapter<CommonCollectionContentEntity>(context), IExposable {

    private val mExposureEventSparseArray = SparseArray<ExposureEvent>()

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

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return if (viewType == ItemViewType.ITEM_BODY) {
            when (collectionStyle) {
                "1-1" -> CommonCollectionDetailOneItemViewHolder(parent.toBinding())
                "1-2" -> CommonCollectionDetailTwoItemViewHolder(parent.toBinding())
                else -> CommonCollectionImageTextItemViewHolder(parent.toBinding())
            }
        } else {
            FooterViewHolder(mLayoutInflater.inflate(com.gh.gamecenter.common.R.layout.refresh_footerview, parent, false))
        }
    }

    override fun getItemViewType(position: Int): Int {
        return when (position) {
            itemCount - 1 -> ItemViewType.ITEM_FOOTER
            else -> ItemViewType.ITEM_BODY
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is FooterViewHolder) {
            holder.initFooterViewHolder(mViewModel, mIsLoading, mIsNetworkError, mIsOver)
            return
        }

        val contentEntity = mEntityList[position]
        val linkEntity = mEntityList[position].linkEntity

        val listener: (v: View) -> Unit = {
            DirectUtils.directToLinkPage(
                mContext,
                linkEntity,
                "通用链接合集详情",
                "",
                mExposureEventSparseArray.get(position)
            )
            val commonLinkCollection = mViewModel.commonCollectionLiveData.value
            NewLogUtils.logCommonCollectionClick(
                commonLinkCollection?.id ?: "",
                commonLinkCollection?.name ?: "",
                mBlockId,
                mBlockName,
                mEntrance,
                "合集详情",
                linkEntity.title ?: "",
                contentEntity.addedContent1 ?: "",
                contentEntity.addedContent2 ?: "",
                linkEntity.type ?: "",
                linkEntity.text ?: "",
                position + 1
            )
            NewLogUtils.logCommonCategoryDetailContentClick(
                contentEntity?.title ?: "",
                linkEntity.type ?: "",
                linkEntity.link ?: "",
                linkEntity.text ?: "",
                commonLinkCollection?.name ?: "",
                commonLinkCollection?.id ?: ""
            )
            SensorsBridge.trackLinkContentCollectionClick(
                location = mLocation,
                blockName = mBlockName,
                blockId = mBlockId,
                linkContentCollectionId = commonLinkCollection?.id ?: "",
                linkContentCollectionName = commonLinkCollection?.name ?: "",
                position = position,
                linkType = linkEntity.type ?: "",
                linkId = linkEntity.link ?: "",
                linkText = linkEntity.text ?: "",
                text = "通用内容"
            )
        }

        val commonCollectionEntity = mViewModel.commonCollectionLiveData.value
        val gameEntity = if (linkEntity.type == "game") {
            GameEntity(id = linkEntity.link, name = linkEntity.text)
        } else {
            GameEntity()
        }
        mExposureEventSparseArray.put(
            position,
            ExposureEvent.createEventWithSourceConcat(
                gameEntity.also {
                    it.sequence = position
                },
                basicSource = basicExposureSource ?: listOf(),
                listOf(
                    ExposureSource(
                        "通用内容合集",
                        "${commonCollectionEntity?.name ?: ""}+${commonCollectionEntity?.layoutChinese ?: ""}+${commonCollectionEntity?.id ?: ""}"
                    ), ExposureSource("合集详情")
                ),
            ).apply {
                this.payload.outerSequence = mTabIndex
            }
        )

        when (holder) {
            is CommonCollectionDetailOneItemViewHolder -> {
                holder.binding.run {
                    ImageUtils.display(commonCollectionImage, contentEntity.image)
                    maskView.goneIf(contentEntity.title.isEmpty())
                    titleTv.text = contentEntity.title
                    root.setOnClickListener(listener)
                }
            }
            is CommonCollectionDetailTwoItemViewHolder -> {
                holder.binding.run {
                    ImageUtils.display(commonCollectionImage, contentEntity.image)
                    maskView.goneIf(contentEntity.title.isEmpty() && contentEntity.addedContent1.isNullOrEmpty())
                    titleTv.text = contentEntity.title
                    desTv.text = contentEntity.addedContent1
                    root.setOnClickListener(listener)
                }
            }
            is CommonCollectionImageTextItemViewHolder -> {
                holder.binding.run {
                    ImageUtils.display(commonCollectionImage, contentEntity.image)
                    linkTitleTv.text = contentEntity.title
                    linkDes1.text = contentEntity.addedContent1
                    linkDes2.text = contentEntity.addedContent2

                    root.setOnClickListener(listener)
                }
            }
        }
    }

    override fun getItemCount(): Int {
        return if (mEntityList.isNullOrEmpty()) 0 else mEntityList.size + FOOTER_ITEM_COUNT
    }

    override fun getEventByPosition(pos: Int): ExposureEvent? {
        return mExposureEventSparseArray[pos]
    }

    override fun getEventListByPosition(pos: Int): List<ExposureEvent>? = null
}