package com.gh.gamecenter.catalog

import android.content.Context
import android.view.ViewGroup
import com.gh.gamecenter.R
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.databinding.SubCatalogItemBinding
import com.gh.gamecenter.entity.CatalogEntity
import com.lightgame.adapter.BaseRecyclerAdapter

class SubCatalogAdapter(
    context: Context,
    private val mCatalogViewModel: CatalogViewModel,
    private val mPrimaryCatalog: CatalogEntity,
    private var mList: List<CatalogEntity.SubCatalogEntity>
) : BaseRecyclerAdapter<SubCatalogAdapter.SubCatalogItemViewHolder>(context) {

    private val mTypes = listOf("专题", "标签")

    override fun getItemCount() = mList.size

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

    override fun onBindViewHolder(holder: SubCatalogItemViewHolder, position: Int) {
        holder.binding.run {
            val catalogEntity = mList[position]
            ImageUtils.display(catalogIcon, catalogEntity.icon)
            catalogName.text = catalogEntity.name
            catalogName.setTextColor(R.color.text_title.toColor(mContext))
            recommendTag.goneIf(!catalogEntity.recommended)
            root.setOnClickListener {
                mCatalogViewModel.logSubCatalogContentClick(catalogEntity.name, position)
                if (mTypes.contains(catalogEntity.type)) {
                    root.context.startActivity(
                        NewCatalogListActivity.getIntent(
                            mContext,
                            mCatalogViewModel.catalogId,
                            mCatalogViewModel.catalogTitle,
                            catalogEntity.name,
                            mPrimaryCatalog,
                            catalogEntity.name
                        )
                    )
                } else {
                    DialogHelper.showUpgradeDialog(mContext)
                }
            }
        }
    }

    class SubCatalogItemViewHolder(val binding: SubCatalogItemBinding) : BaseRecyclerViewHolder<Any>(binding.root)
}