package com.gh.vspace.installexternalgames

import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.common.utils.goneIf
import com.gh.gamecenter.common.utils.toBinding

class ExternalGameAdapter(private val games: List<ExternalGameUiState>, private val onItemClickListener: OnItemClickListener) :
    RecyclerView.Adapter<ExternalGameViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ExternalGameViewHolder {
        return ExternalGameViewHolder(parent.toBinding())
    }

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

    override fun onBindViewHolder(holder: ExternalGameViewHolder, position: Int) {
        val item = games[position]
        holder.apkInfo.text = item.externalGameEntity.let { item ->
            """
            应用程序名：${item.appName}
            版本号：${item.apkVersion}
            包名: ${item.apkPackageName}
            路径:${item.apkPath}
            """.trimIndent()
        }

        holder.update.setOnClickListener {
            onItemClickListener.onItemClick(item, OnItemClickListener.ClickType.CLICK_INSTALL)
        }

        holder.install.goneIf(item.isInstalled) {
            holder.install.setOnClickListener {
                onItemClickListener.onItemClick(item, OnItemClickListener.ClickType.CLICK_INSTALL)
            }
        }
        holder.uninstall.goneIf(!item.isInstalled) {
            holder.uninstall.setOnClickListener {
                onItemClickListener.onItemClick(
                    item,
                    OnItemClickListener.ClickType.CLICK_UNINSTALL
                )
            }
        }
        holder.start.goneIf(!item.isInstalled) {
            holder.start.setOnClickListener {
                onItemClickListener.onItemClick(item, OnItemClickListener.ClickType.CLICK_START)
            }
        }
    }
}