package com.gh.gamecenter.gamedetail.dialog

import android.content.Context
import android.view.ViewGroup
import android.view.ViewGroup.MarginLayoutParams
import androidx.core.view.updateLayoutParams
import androidx.recyclerview.widget.RecyclerView
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.utils.toColor
import com.gh.gamecenter.databinding.ItemGameDetailFunctionBinding
import com.gh.gamecenter.gamedetail.entity.GameDetailInfoTag
import com.lightgame.adapter.BaseRecyclerAdapter

class GameFunctionAdapter(
    context: Context,
    private val infoTags: List<GameDetailInfoTag.InfoTag>
) : BaseRecyclerAdapter<RecyclerView.ViewHolder>(context) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return GameFunctionItemViewHolder(parent.toBinding())
    }

    override fun getItemCount(): Int = infoTags.size

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is GameFunctionItemViewHolder) {
            holder.binding.root.updateLayoutParams<MarginLayoutParams> {
                setMargins(0, if (position == 0) 8F.dip2px() else 20F.dip2px(), 0, 0)
            }

            val infoTag = infoTags.getOrNull(position)
            ImageUtils.display(holder.binding.iconIv, infoTag?.icon)
            holder.binding.nameTv.setTextColor(com.gh.gamecenter.common.R.color.text_primary.toColor(mContext))
            holder.binding.desTv.setTextColor(com.gh.gamecenter.common.R.color.text_secondary.toColor(mContext))
            holder.binding.nameTv.text = infoTag?.name
            holder.binding.desTv.text = infoTag?.des
        }
    }

    class GameFunctionItemViewHolder(var binding: ItemGameDetailFunctionBinding) :
        RecyclerView.ViewHolder(binding.root)
}
