package com.gh.gamecenter.qa.editor

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.view.View
import android.view.ViewGroup
import com.gh.gamecenter.R
import com.gh.gamecenter.common.baselist.ListAdapter
import com.gh.gamecenter.feature.databinding.GameItemBinding
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.feature.game.GameItemViewHolder

open class GameAdapter(context: Context) : ListAdapter<GameEntity>(context) {

    public override fun setListData(updateData: MutableList<GameEntity>?) {
        super.setListData(updateData)
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): androidx.recyclerview.widget.RecyclerView.ViewHolder {
        return GameItemViewHolder(GameItemBinding.bind(mLayoutInflater.inflate(R.layout.game_item, parent, false)))
    }

    override fun getItemCount(): Int {
        return mEntityList.size
    }

    override fun onBindViewHolder(holder: androidx.recyclerview.widget.RecyclerView.ViewHolder, position: Int) {
        if (holder is GameItemViewHolder) {
            val entity = mEntityList[position]
            holder.bindGameItem(entity)
            holder.binding.downloadBtn.visibility = View.GONE
            val layoutParams = holder.binding.gameName.layoutParams
            layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT
            holder.binding.gameName.layoutParams = layoutParams

            holder.itemView.setOnClickListener {
                if (mContext is Activity) {
                    val intent = Intent()
                    intent.putExtra(GameEntity::class.java.simpleName, entity)
                    (mContext as Activity).setResult(Activity.RESULT_OK, intent)
                    (mContext as Activity).finish()
                }
            }
        }
    }

}