package com.gh.gamecenter.tag

import android.content.Context
import android.graphics.Color
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
import com.gh.gamecenter.R
import com.gh.gamecenter.databinding.ItemTagBinding
import com.gh.gamecenter.entity.TagEntity
import com.lightgame.adapter.BaseRecyclerAdapter

class TagsHorizontalAdapter(
    context: Context,
    private val mViewModel: TagsListViewModel,
    private val mTagList: List<TagEntity>,
    private val mSmoothScrollAction: (position: Int) -> Unit
) : BaseRecyclerAdapter<TagsViewHolder>(context) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TagsViewHolder {
        return TagsViewHolder(ItemTagBinding.inflate(mLayoutInflater, parent, false))
    }

    override fun onBindViewHolder(holder: TagsViewHolder, position: Int) {
        val tagEntity = mTagList[position]

        if (mViewModel.selectedTag.name == tagEntity.name) {
            holder.binding.tagTv.background = ContextCompat.getDrawable(mContext, R.drawable.bg_tag_text)
            holder.binding.tagTv.setTextColor(Color.WHITE)
        } else {
            holder.binding.tagTv.background = null
            holder.binding.tagTv.setTextColor(ContextCompat.getColor(mContext, R.color.text_757575))
        }

        holder.binding.tagTv.text = tagEntity.name
        holder.binding.tagTv.setOnClickListener {
            mViewModel.changeSelectedTag(tagEntity)
            notifyDataSetChanged()

            mSmoothScrollAction.invoke(position)
        }
    }

    override fun getItemCount(): Int {
        return mTagList.size
    }

}

class TagsViewHolder(val binding: ItemTagBinding) : BaseRecyclerViewHolder<Any>(binding.root)

