package com.gh.gamecenter.category2

import android.content.Context
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.view.GridSpacingItemColorDecoration
import com.gh.gamecenter.R
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.databinding.CategoryDirectoryItemBinding
import com.gh.gamecenter.entity.CategoryEntity
import com.lightgame.adapter.BaseRecyclerAdapter

class CategoryDirectoryAdapter(
    context: Context,
    private val mViewModel: CategoryV2ViewModel,
    private var mList: List<CategoryEntity>
) : BaseRecyclerAdapter<CategoryDirectoryAdapter.CategoryDirectoryItemViewHolder>(context) {

    val width = mContext.resources.displayMetrics.widthPixels * 260 / 360

    fun setListData(list: List<CategoryEntity>) {
        mList = list
        notifyDataSetChanged()
    }

    override fun getItemCount() = mList.size

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
        CategoryDirectoryItemViewHolder(CategoryDirectoryItemBinding.inflate(mLayoutInflater))

    override fun onBindViewHolder(holder: CategoryDirectoryItemViewHolder, position: Int) {
        holder.binding.run {
            root.layoutParams = root.layoutParams?.apply {
                width = mContext.resources.displayMetrics.widthPixels * 260 / 360
            } ?: RecyclerView.LayoutParams(width, RecyclerView.LayoutParams.WRAP_CONTENT)

            val padTop = if (position == 0) 16F.dip2px() else 24F.dip2px()
            root.setPadding(16F.dip2px(), padTop, 16F.dip2px(), 0)

            val entity = mList[position]
            title.text = entity.name
            title.setTextColor(R.color.text_title.toColor(mContext))

            subCategoryRv.run {
                if (adapter is SubCategoryAdapter) {
                    layoutManager = GridLayoutManager(mContext, 3)
                    adapter = entity.data?.let {
                        SubCategoryAdapter(
                            mContext,
                            mViewModel,
                            it,
                            position
                        )
                    }
                } else {
                    layoutManager = GridLayoutManager(mContext, 3)
                    adapter = entity.data?.let {
                        SubCategoryAdapter(
                            mContext,
                            mViewModel,
                            it,
                            position
                        )
                    }
                    addItemDecoration(
                        GridSpacingItemColorDecoration(
                            mContext,
                            6,
                            6,
                            R.color.transparent
                        )
                    )
                }
            }
        }
    }

    class CategoryDirectoryItemViewHolder(val binding: CategoryDirectoryItemBinding) :
        BaseRecyclerViewHolder<Any>(binding.root)
}