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.core.utils.ToastUtils
import com.gh.gamecenter.common.utils.goneIf
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.R
import com.gh.gamecenter.common.utils.toDrawable
import com.gh.gamecenter.databinding.SubCategoryItemBinding
import com.gh.gamecenter.entity.CategoryEntity
import com.lightgame.adapter.BaseRecyclerAdapter

class SubCategoryAdapter(
    context: Context,
    private val mViewModel: CategoryV2ViewModel,
    private val mList: List<CategoryEntity>,
    private val mPrimaryIndex: Int
) : BaseRecyclerAdapter<SubCategoryAdapter.SubCategoryItemViewHolder>(context) {

    override fun getItemCount() = mList.size

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
        SubCategoryItemViewHolder(SubCategoryItemBinding.inflate(mLayoutInflater))

    override fun onBindViewHolder(holder: SubCategoryItemViewHolder, position: Int) {
        holder.binding.run {
            val categoryEntity = mList[position]
            name.text = categoryEntity.name
            recommendIv.goneIf(categoryEntity.recommend == false)

            if (categoryEntity.selected) {
                selectedIv.visibility = View.VISIBLE
                container.background = R.drawable.bg_category_selected.toDrawable(mContext)
                name.setTextColor(R.color.theme_font.toColor(mContext))
            } else {
                selectedIv.visibility = View.GONE
                container.background = R.drawable.bg_shape_space_radius_8.toDrawable(mContext)
                name.setTextColor(R.color.text_title.toColor(mContext))
            }

            root.setOnClickListener {
                when {
                    mViewModel.selectedCount >= 5 && !categoryEntity.selected -> ToastUtils.toast("最多只能选择5个类别")

                    categoryEntity.selected -> {
                        categoryEntity.selected = false
                        selectedIv.visibility = View.GONE
                        container.background = R.drawable.bg_shape_space_radius_8.toDrawable(mContext)
                        name.setTextColor(R.color.text_title.toColor(mContext))
                        mViewModel.run {
                            if (selectedCount > 0) {
                                selectedCount--
                                postSelectedCount()
                                postCategoryPosition(mPrimaryIndex, position)
                                logClickClassificationDelete(mPrimaryIndex, categoryEntity.name ?: "", "全部类别")
                            }
                        }
                    }

                    !categoryEntity.selected -> {
                        categoryEntity.selected = true
                        categoryEntity.primaryIndex = mPrimaryIndex
                        selectedIv.visibility = View.VISIBLE
                        container.background = R.drawable.bg_category_selected.toDrawable(mContext)
                        name.setTextColor(R.color.theme_font.toColor(mContext))
                        mViewModel.run {
                            if (selectedCount < 5) {
                                selectedCount++
                                logClickClassification(mPrimaryIndex, categoryEntity.name ?: "", position)
                                postSelectedCount()
                                postCategoryPosition(mPrimaryIndex, position)
                            }
                        }
                    }
                }
            }
        }
    }

    class SubCategoryItemViewHolder(val binding: SubCategoryItemBinding) : BaseRecyclerViewHolder<Any>(binding.root)
}