package com.gh.gamecenter.game.columncollection

import android.content.Context
import android.view.ViewGroup
import com.gh.gamecenter.R
import com.gh.gamecenter.adapter.viewholder.GameColumnCollectionItemViewHolder
import com.gh.gamecenter.common.utils.ImageUtils
import com.gh.gamecenter.common.view.WrapContentDraweeView
import com.gh.gamecenter.databinding.GameColumnCollectionItemBinding
import com.gh.gamecenter.entity.SubjectEntity
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.feature.exposure.ExposureEvent
import com.gh.gamecenter.common.exposure.ExposureSource
import com.lightgame.adapter.BaseRecyclerAdapter

class GameColumnCollectionAdapter(
    context: Context,
    private var mSubjectEntity: SubjectEntity,
    private var mBasicExposureSourceList: ArrayList<ExposureSource>,
    private var mClickClosure: (position: Int, gameEntity: GameEntity) -> Unit,
    private var mExposureClosure: (exposureEvent: ExposureEvent) -> Unit
) : BaseRecyclerAdapter<GameColumnCollectionItemViewHolder>(context) {

    private var mCountAndKey: Pair<Int, String>? = null
    var columnCollectionHeight = 0

    init {
        var dataIds = ""
        mSubjectEntity.data?.forEach {
            dataIds += it.id
        }
        if (dataIds.isNotEmpty()) mCountAndKey = Pair(mSubjectEntity.data?.size ?: 0, dataIds)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): GameColumnCollectionItemViewHolder {
        val view = mLayoutInflater.inflate(R.layout.game_column_collection_item, parent, false)
        return GameColumnCollectionItemViewHolder(GameColumnCollectionItemBinding.bind(view))
    }

    override fun getItemCount(): Int {
        return mSubjectEntity.data!!.size
    }

    override fun onBindViewHolder(holder: GameColumnCollectionItemViewHolder, position: Int) {

        if (columnCollectionHeight == 0) {
            columnCollectionHeight = mContext.resources.getDimension(com.gh.gamecenter.common.R.dimen.home_column_collection_height).toInt()
        }

        val data = mSubjectEntity.data!![position]
        ImageUtils.display(holder.binding.columnCollectionImage, data.image)
        holder.binding.columnCollectionImage.registerLoadingCallback(object : WrapContentDraweeView.LoadingCallback {
            override fun loaded() {
                holder.binding.columnCollectionImage.layoutParams =
                    holder.binding.columnCollectionImage.layoutParams.apply {
                        height = columnCollectionHeight
                        width = (holder.binding.columnCollectionImage.aspectRatio * columnCollectionHeight).toInt()
                    }
            }
        })

        val exposureEvent = ExposureEvent.createEventWithSourceConcat(
            data,
            mBasicExposureSourceList,
            arrayListOf(ExposureSource("游戏专题合集","${mSubjectEntity.name}+${mSubjectEntity.id}"))
        ).apply {
            payload.sequence = position
        }

        data.exposureEvent = exposureEvent
        mExposureClosure.invoke(exposureEvent)

        holder.binding.columnCollectionImage.setOnClickListener {
            mClickClosure(position, data)
        }
    }

    fun checkResetData(subjectEntity: SubjectEntity) {
        var dataIds = ""
        subjectEntity.data?.forEach {
            dataIds += it.id
        }

        mSubjectEntity = subjectEntity
        if (mCountAndKey?.first == subjectEntity.data?.size && mCountAndKey?.second != dataIds) { // 数量不变,内容发生改变
            notifyItemRangeChanged(0, itemCount)
        } else if (mCountAndKey?.first != subjectEntity.data?.size) { // 数量发生改变
            notifyDataSetChanged()
        }

        // 重新刷新数据标识
        mCountAndKey = Pair(subjectEntity.data?.size ?: 0, dataIds)
    }
}