package com.gh.gamecenter.game.imageslide

import android.content.Context
import android.graphics.Color
import android.view.View
import android.view.ViewGroup
import com.gh.gamecenter.core.utils.StringUtils
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.utils.toBinding
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.GameDetailActivity
import com.gh.gamecenter.databinding.GameHorizontalSimpleItemBinding
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.game.horizontal.GameHorizontalSimpleItemViewHolder
import com.lightgame.adapter.BaseRecyclerAdapter

class ImageSlideAdapter(
    context: Context,
    var subject: GameEntity,
    val skipListener: View.OnClickListener,
    val entrance: String
) : BaseRecyclerAdapter<GameHorizontalSimpleItemViewHolder>(context) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): GameHorizontalSimpleItemViewHolder {
        val binding: GameHorizontalSimpleItemBinding = parent.toBinding()
        val layoutParam =
            ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        binding.root.layoutParams = layoutParam
        binding.gameName.setTextColor(Color.WHITE)
        return GameHorizontalSimpleItemViewHolder(binding)
    }

    override fun getItemCount(): Int {
        return subject.games!!.size
    }

    override fun onBindViewHolder(holder: GameHorizontalSimpleItemViewHolder, position: Int) {
        val padR = if (position == itemCount - 1) 20F.dip2px() else 0
        val padL = if (position == 0) 180F.dip2px() else 16F.dip2px()
        holder.binding.root.setPadding(padL, 0, padR, 24F.dip2px())
        holder.binding.gameName.setTextColor(com.gh.gamecenter.common.R.color.text_primary.toColor(holder.binding.root.context))

        val gameEntity = subject.games?.get(position)
        gameEntity?.let {
            holder.binding.gameIcon.displayGameIcon(it)
            GameHorizontalSimpleItemViewHolder.setHorizontalNameAndGravity(holder.binding.gameName, it.name)
        }

        holder.itemView.isClickable = false
        holder.binding.gameName.setOnClickListener { skipGameDetail(gameEntity, position) }
        holder.binding.gameIcon.setOnClickListener { skipGameDetail(gameEntity, position) }
        holder.itemView.setOnClickListener(skipListener)
    }

    private fun skipGameDetail(gameEntity: GameEntity?, position: Int) {
        GameDetailActivity.startGameDetailActivity(
            mContext,
            gameEntity,
            StringUtils.buildString("($entrance:", subject.name, "-列表[", (position + 1).toString(), "])"),
            traceEvent = gameEntity?.exposureEvent
        )
    }

    fun checkResetData(subjectEntity: GameEntity) {
        if (subject != subjectEntity) subject = subjectEntity
    }
}