package com.gh.gamecenter.catalog

import android.content.Context
import android.view.ViewGroup
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
import com.gh.common.util.DirectUtils
import com.gh.gamecenter.common.utils.ImageUtils
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.utils.toBinding
import com.gh.gamecenter.common.view.WrapContentDraweeView
import com.gh.gamecenter.databinding.CatalogSubjectCollectionListItemBinding
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.common.entity.LinkEntity
import com.lightgame.adapter.BaseRecyclerAdapter

class SpecialCatalogSubjectCollectionAdapter(
    context: Context,
    private val mCatalogViewModel: SpecialCatalogViewModel,
    private var mList: List<GameEntity>
) : BaseRecyclerAdapter<SpecialCatalogSubjectCollectionAdapter.CatalogSubjectCollectionListItemViewHolder>(context) {

    private var countAndKey: Pair<Int, String>? = null
    private var mPosition: Int = -1

    init {
        var dataIds = ""
        mList.forEach {
            dataIds += it.id
        }
        if (dataIds.isNotEmpty()) countAndKey = Pair(mList.size, dataIds)
    }

    override fun getItemCount() = mList.size

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
        CatalogSubjectCollectionListItemViewHolder(parent.toBinding())

    override fun onBindViewHolder(holder: CatalogSubjectCollectionListItemViewHolder, position: Int) {
        holder.binding.run {
            root.layoutParams = (root.layoutParams as ViewGroup.MarginLayoutParams).apply {
                leftMargin = if (position == 0) 17F.dip2px() else 0
            }
            val entity = mList[position]
            ImageUtils.display(subjectCollectionImage, entity.image)
            subjectCollectionImage.registerLoadingCallback(object : WrapContentDraweeView.LoadingCallback {
                override fun loaded() {
                    subjectCollectionImage.layoutParams = subjectCollectionImage.layoutParams.apply {
                        height = 96F.dip2px()
                        width = (subjectCollectionImage.aspectRatio * 96F.dip2px()).toInt()
                    }
                }
            })
            root.setOnClickListener {
                DirectUtils.directToLinkPage(
                    mContext,
                    LinkEntity(link = entity.link, type = entity.type),
                    "精选分类",
                    "专题合集"
                )
                mCatalogViewModel.logSpecialCatalogSpecificContentClick(
                    "专题合集",
                    entity.subjectName ?: "",
                    entity.name ?: "",
                    mPosition,
                    position
                )
            }
        }
    }

    fun checkResetData(update: List<GameEntity>) {
        var dataIds = ""
        update.forEach { dataIds += it.id }

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

        // 重新刷新数据标识
        countAndKey = Pair(update.size, dataIds)
    }

    fun updatePosition(position: Int) {
        mPosition = position
    }

    class CatalogSubjectCollectionListItemViewHolder(val binding: CatalogSubjectCollectionListItemBinding) :
        BaseRecyclerViewHolder<Any>(binding.root)
}