package com.gh.gamecenter.gamecollection.square

import android.content.Context
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.common.utils.TextHelper
import com.gh.gamecenter.core.AppExecutor
import com.gh.gamecenter.databinding.GameCollectionAmwayContentItemBinding
import com.gh.gamecenter.entity.AmwayCommentEntity
import com.lightgame.adapter.BaseRecyclerAdapter

class GameCollectionAmwayAdapter(context: Context) :
    BaseRecyclerAdapter<RecyclerView.ViewHolder>(context) {

    private var mAmwayList = listOf<AmwayCommentEntity>()

    fun setAmwayList(amwayList: List<AmwayCommentEntity>) {
        mAmwayList = amwayList
        AppExecutor.uiExecutor.execute {
            notifyDataSetChanged()
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
        GameCollectionAmwayContentItemViewHolder(
            GameCollectionAmwayContentItemBinding.inflate(
                mLayoutInflater,
                parent,
                false
            )
        )

    override fun getItemCount() = Int.MAX_VALUE

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is GameCollectionAmwayContentItemViewHolder && getRealCount() != 0) {
            val entity = mAmwayList[getRealPosition(position)]
            holder.binding.gameIcon.displayGameIcon(entity.game.toGameEntity())
            holder.binding.amwayTv.text =
                TextHelper.getCommentLabelSpannableStringBuilder(entity.comment.content, com.gh.gamecenter.common.R.color.white_alpha_80)
        }
    }

    class GameCollectionAmwayContentItemViewHolder(var binding: GameCollectionAmwayContentItemBinding) :
        RecyclerView.ViewHolder(binding.root)

    fun getRealCount() = mAmwayList.size

    fun getRealPosition(position: Int) = position % getRealCount()
}