package com.gh.gamecenter.qa.dialog

import android.content.Context
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.R
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.databinding.ItemChooseSectionDialogBinding
import com.gh.gamecenter.entity.ForumDetailEntity
import com.lightgame.adapter.BaseRecyclerAdapter

class ChooseSectionAdapter(
    context: Context,
    var selectedSection: ForumDetailEntity.Section = ForumDetailEntity.Section(),
    val onClick: () -> Unit
) :
    BaseRecyclerAdapter<ChooseSectionAdapter.ChooseSectionItemViewHolder>(context) {
    private var mSectionList = listOf<ForumDetailEntity.Section>()

    fun submitList(sectionList: List<ForumDetailEntity.Section>) {
        mSectionList = sectionList
        notifyItemRangeChanged(0, itemCount)
    }

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

    override fun getItemCount(): Int = mSectionList.size

    override fun onBindViewHolder(holder: ChooseSectionItemViewHolder, position: Int) {
        val entity = mSectionList[position]
        holder.binding.selectedIv.goneIf(entity.id != selectedSection.id)
        holder.binding.sectionName.run {
            text = entity.name
            setTextColor(
                if (entity.id == selectedSection.id) R.color.text_title.toColor(mContext) else R.color.text_subtitleDesc.toColor(
                    mContext
                )
            )
        }
        holder.binding.root.setOnClickListener {
            selectedSection = entity
            notifyItemRangeChanged(0, itemCount)
            onClick.invoke()
        }
    }

    class ChooseSectionItemViewHolder(val binding: ItemChooseSectionDialogBinding) :
        RecyclerView.ViewHolder(binding.root)
}