package com.gh.gamecenter.category2

import android.content.Context
import android.view.View
import android.view.ViewGroup
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
import com.gh.gamecenter.common.utils.goneIf
import com.gh.gamecenter.common.utils.toBinding
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.R
import com.gh.gamecenter.databinding.CategoryV2ItemBinding
import com.gh.gamecenter.entity.SidebarsEntity
import com.lightgame.adapter.BaseRecyclerAdapter

class CategoryV2Adapter(
    context: Context,
    private val mFragment: CategoryV2Fragment,
    private val mViewModel: CategoryV2ViewModel,
    private val mList: List<SidebarsEntity.SidebarEntity>
) : BaseRecyclerAdapter<CategoryV2Adapter.CategoryV2ItemViewHolder>(context) {

    override fun getItemCount() = mList.size

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
        CategoryV2ItemViewHolder(parent.toBinding())

    override fun onBindViewHolder(holder: CategoryV2ItemViewHolder, position: Int) {
        holder.binding.run {
            val catalogEntity = mList[position]
            catalogName.text = catalogEntity.name
            recommendTag.goneIf(!catalogEntity.recommended)
            if (catalogEntity.name == mViewModel.selectedCategoryName) {
                selectedTag.visibility = View.VISIBLE
                catalogName.setTextColor(R.color.theme_font.toColor(mContext))
                root.setBackgroundColor(R.color.background_white.toColor(mContext))
            } else {
                selectedTag.visibility = View.GONE
                catalogName.setTextColor(R.color.text_title.toColor(mContext))
                root.setBackgroundColor(R.color.background.toColor(mContext))
            }
            root.setOnClickListener {
                if (catalogEntity.name != mViewModel.selectedCategoryName) {
                    mViewModel.selectedCategoryName = catalogEntity.name
                    mFragment.changeCategory(position)
                    mViewModel.logClickSide()
                    notifyDataSetChanged()
                }
            }
        }
    }

    class CategoryV2ItemViewHolder(val binding: CategoryV2ItemBinding) : BaseRecyclerViewHolder<Any>(binding.root)
}