package com.gh.gamecenter.feedback.view.help

import android.content.Context
import android.view.ViewGroup
import android.view.ViewGroup.MarginLayoutParams
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.common.databinding.ItemIconTabBinding
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.feedback.R
import com.gh.gamecenter.feedback.entity.HelpCategoryEntity
import com.lightgame.adapter.BaseRecyclerAdapter

class HelpCategoryAdapter(
    context: Context,
    private val mViewModel: HelpAndFeedbackViewModel
) : BaseRecyclerAdapter<RecyclerView.ViewHolder>(context) {
    private var mCategoryList = arrayListOf<HelpCategoryEntity>()

    fun submitList(categoryList: List<HelpCategoryEntity>) {
        mCategoryList = ArrayList(categoryList)
        notifyDataSetChanged()
    }

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

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is HelpCategoryItemViewHolder) {
            val categoryEntity = mCategoryList.safelyGetInRelease(position) ?: return
            val isSelected = categoryEntity.id == mViewModel.selectedCategory.id
            holder.binding.root.layoutParams = holder.binding.root.layoutParams.apply { height = 28F.dip2px() }
            holder.binding.iconIv.layoutParams = (holder.binding.iconIv.layoutParams as MarginLayoutParams).apply {
                rightMargin = 2F.dip2px()
            }
            holder.binding.iconIv.goneIf(position != 0) {
                holder.binding.iconIv.setImageResource(if (isSelected) R.drawable.icon_my_selected else R.drawable.icon_my_unselected)
            }
            holder.binding.titleTv.run {
                text = categoryEntity.name
                setTextColor(
                    if (isSelected) com.gh.gamecenter.common.R.color.text_theme.toColor(mContext) else com.gh.gamecenter.common.R.color.text_secondary.toColor(
                        mContext
                    )
                )
                setOnClickListener {
                    mViewModel.setCategory(categoryEntity)
                    notifyItemRangeChanged(0, itemCount)
                }
            }
            holder.binding.root.run {
                layoutParams = (layoutParams as MarginLayoutParams).apply {
                    leftMargin = if (position == 0) 16F.dip2px() else 0
                    rightMargin = 8F.dip2px()
                }
                background =
                    if (isSelected) com.gh.gamecenter.common.R.drawable.button_round_theme_alpha_10.toDrawable(mContext) else com.gh.gamecenter.feature.R.drawable.button_round_gray_light.toDrawable(
                        mContext
                    )
            }
        }
    }

    override fun getItemCount(): Int = mCategoryList.size

    class HelpCategoryItemViewHolder(val binding: ItemIconTabBinding) : RecyclerView.ViewHolder(binding.root)
}