package com.gh.gamecenter.gamedetail.dialog

import android.content.Context
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.common.utils.toBinding
import com.gh.gamecenter.databinding.ItemDialogGameInfoOtherBinding
import com.gh.gamecenter.gamedetail.GameDetailViewModel
import com.gh.gamecenter.gamedetail.detail.viewholder.GameDetailInfoItemViewHolder
import com.gh.gamecenter.gamedetail.entity.GameDetailInfo
import com.gh.gamecenter.gamedetail.entity.GameDetailInfo.InfoItem
import com.lightgame.adapter.BaseRecyclerAdapter

class GameInfoAdapter(
    context: Context,
    private val data: GameDetailInfo?,
    private val infoList: List<InfoItem>,
    private val viewModel: GameDetailViewModel
) : BaseRecyclerAdapter<RecyclerView.ViewHolder>(context) {
    override fun getItemViewType(position: Int): Int = if (position == itemCount - 1) TYPE_OTHER else TYPE_INFO

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return if (viewType == TYPE_INFO) {
            GameDetailInfoItemViewHolder.InfoItemViewHolder(parent.toBinding())
        } else {
            GameInfoDialogOtherViewHolder(parent.toBinding())
        }
    }

    override fun getItemCount(): Int = infoList.size + 1

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is GameDetailInfoItemViewHolder.InfoItemViewHolder) {
            val info = infoList.getOrNull(position) ?: return
            holder.bindInfoItem(mContext, info)
        }
        if (holder is GameInfoDialogOtherViewHolder) {
            val gameDetailInfo = data ?: return
            holder.binding.run {
                GameDetailInfoItemViewHolder.bindStaticInfo(
                    mContext,
                    gameDetailInfo,
                    viewModel,
                    privacyPolicyTv,
                    permissionsTv,
                    requestUpdateTv,
                    desTv
                )
            }
        }
    }

    class GameInfoDialogOtherViewHolder(val binding: ItemDialogGameInfoOtherBinding): RecyclerView.ViewHolder(binding.root)

    companion object {
        const val TYPE_INFO = 0
        const val TYPE_OTHER = 1
    }

}