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.ExposureEvent
import com.gh.common.exposure.ExposureSource
import com.gh.common.exposure.IExposable
import com.gh.common.util.*
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.utils.goneIf
import com.gh.gamecenter.common.utils.toBinding
import com.gh.gamecenter.common.utils.ImageUtils
import com.gh.gamecenter.entity.CommonCollectionContentEntity
import com.gh.gamecenter.entity.GameEntity

class CommonCollectionDetailAdapter(
    context: Context,
    var collectionStyle: String,
    val mViewModel: CommonCollectionDetailViewModel,
    val mBlockId: String,
    val mBlockName: String,
    val mEntrance: String
) : 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 when (collectionStyle) {
            "1-1" -> CommonCollectionDetailOneItemViewHolder(parent.toBinding())
            "1-2" -> CommonCollectionDetailTwoItemViewHolder(parent.toBinding())
            else -> CommonCollectionImageTextItemViewHolder(parent.toBinding())
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val contentEntity = mEntityList[position]
        val linkEntity = mEntityList[position].linkEntity

        val listener: (v: View) -> Unit = {
            DirectUtils.directToLinkPage(mContext, linkEntity, "通用链接合集详情", "")
            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 ?: ""
            )
        }

        if (linkEntity.type == "game") {
            mExposureEventSparseArray.put(
                position,
                ExposureEvent.createEvent(
                    GameEntity(
                        id = linkEntity.link,
                        name = linkEntity.name
                    ).also {
                        it.sequence = position
                    },
                    listOf(
                        ExposureSource(
                            "内容合集",
                            mViewModel.commonCollectionLiveData.value?.name ?: ""
                        ),
                        ExposureSource("合集详情")
                    ),
                )
            )
        }

        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 == null || mEntityList.isEmpty()) 0 else mEntityList.size
    }

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

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