package com.gh.gamecenter.qa.editor

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.view.ViewGroup
import com.gh.gamecenter.common.utils.ImageUtils
import com.gh.gamecenter.R
import com.gh.gamecenter.databinding.EditorInsertDefaultItemBinding
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.qa.entity.EditorInsertDefaultEntity
import com.lightgame.adapter.BaseRecyclerAdapter

class GameDefaultAdapter(
    context: Context,
    private val mDefaultList: List<EditorInsertDefaultEntity>
) : BaseRecyclerAdapter<GameDefaultItemViewHolder>(context) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): GameDefaultItemViewHolder {
        val view = mLayoutInflater.inflate(R.layout.editor_insert_default_item, parent, false)
        return GameDefaultItemViewHolder(EditorInsertDefaultItemBinding.bind(view))
    }

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

    override fun onBindViewHolder(holder: GameDefaultItemViewHolder, position: Int) {
        val entity = mDefaultList[position]
        ImageUtils.display(holder.binding.gameIcon, entity.gameIcon)
        holder.binding.gameName.text = entity.gameName
        holder.binding.type.text = when (entity.type) {
            "upload" -> "上次投稿"
            "played" -> "最近玩过"
            "follow" -> "我关注的"
            else -> ""
        }
        holder.itemView.setOnClickListener {
            if (mContext is Activity) {
                val intent = Intent()
                intent.putExtra(
                    GameEntity::class.java.simpleName,
                    GameEntity(_id = entity.gameId, mIcon = entity.gameIcon, mName = entity.gameName, mCategory = entity.category)
                )
                (mContext as Activity).setResult(Activity.RESULT_OK, intent)
                (mContext as Activity).finish()
            }
        }
    }
}