package com.gh.gamecenter.category

import android.content.Context
import android.view.ViewGroup
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
import com.gh.gamecenter.common.utils.toBinding
import com.gh.gamecenter.R
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.common.utils.toDrawable
import com.gh.gamecenter.databinding.ItemTagBinding
import com.gh.gamecenter.entity.CategoryEntity
import com.lightgame.adapter.BaseRecyclerAdapter

class NewCategoryHorizontalAdapter(
    context: Context,
    private val mViewModel: NewCategoryListViewModel,
    private val mCategoryList: List<CategoryEntity>,
    private val mSmoothScrollAction: (position: Int) -> Unit
) : BaseRecyclerAdapter<TagsViewHolder>(context) {

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

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

        if (mViewModel.selectedCategory.name == categoryEntity.name) {
            holder.binding.tagTv.background = R.drawable.bg_tag_text.toDrawable()
            holder.binding.tagTv.setTextColor(R.color.text_white.toColor(mContext))
        } else {
            holder.binding.tagTv.background = null
            holder.binding.tagTv.setTextColor(R.color.text_757575.toColor(mContext))
        }

        holder.binding.tagTv.text = categoryEntity.name
        holder.binding.tagTv.setOnClickListener {
            mViewModel.changeSelectedCategory(categoryEntity)
            notifyDataSetChanged()

            mSmoothScrollAction.invoke(position)
        }
    }

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

}

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