package com.gh.gamecenter.gamecollection.mine

import android.content.Context
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.common.constant.ItemViewType
import com.gh.gamecenter.core.utils.DisplayUtils
import com.gh.gamecenter.common.utils.toBinding
import com.gh.gamecenter.common.viewholder.FooterViewHolder
import com.gh.gamecenter.common.baselist.ListAdapter
import com.gh.gamecenter.entity.GamesCollectionEntity

class MyGameCollectionAdapter(
    context: Context,
    val mViewModel: MyGameCollectionViewModel,
    val entrance: String,
    val path: String
) : ListAdapter<GamesCollectionEntity>(context) {

    override fun getItemViewType(position: Int): Int {
        return if (position == itemCount - 1) {
            ItemViewType.ITEM_FOOTER
        } else {
            ItemViewType.ITEM_BODY
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return when (viewType) {
            ItemViewType.ITEM_BODY -> MyGameCollectionViewHolder(parent.toBinding(), mViewModel, entrance, path)
            ItemViewType.ITEM_FOOTER -> {
                FooterViewHolder(mLayoutInflater.inflate(com.gh.gamecenter.common.R.layout.refresh_footerview, parent, false))
            }
            else -> MyGameCollectionViewHolder(parent.toBinding(), mViewModel, entrance, path)
        }
    }

    override fun areItemsTheSame(oldItem: GamesCollectionEntity?, newItem: GamesCollectionEntity?): Boolean {
        return oldItem == newItem || oldItem?.id == newItem?.id
    }

    override fun areContentsTheSame(oldItem: GamesCollectionEntity?, newItem: GamesCollectionEntity?): Boolean {
        return oldItem?.id == newItem?.id
                && oldItem?.title == newItem?.title
                && oldItem?.intro == newItem?.intro
                && oldItem?.cover == newItem?.cover
                && oldItem?.display == newItem?.display
                && oldItem?.status == newItem?.status
                && oldItem?.count?.game == newItem?.count?.game
                && oldItem?.getTagIds() == newItem?.getTagIds()
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (holder) {
            is MyGameCollectionViewHolder -> {
                val entity = mEntityList[position]
                holder.bindGameCollectionItem(entity)
            }
            is FooterViewHolder -> {
                holder.initItemPadding()
                holder.initFooterViewHolder(mIsLoading, mIsNetworkError, mIsOver)
                holder.hint.setTextColor(ContextCompat.getColor(mContext, com.gh.gamecenter.common.R.color.aaaaaa))
                val lp = holder.itemView.layoutParams as RecyclerView.LayoutParams
                lp.height = DisplayUtils.dip2px(48F)
                lp.width = ViewGroup.LayoutParams.MATCH_PARENT
                holder.itemView.layoutParams = lp
            }
        }
    }

    override fun getItemCount(): Int {
        return if (mEntityList == null || mEntityList.isEmpty()) return 0 else mEntityList.size + 1
    }
}