package com.gh.gamecenter.gamedetail.fuli.kaifu

import android.content.Context
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.gh.common.util.NewFlatLogUtils
import com.gh.gamecenter.GameDetailActivity
import com.gh.gamecenter.common.baselist.ListAdapter
import com.gh.gamecenter.common.exposure.ExposureSource
import com.gh.gamecenter.common.utils.layoutInflater
import com.gh.gamecenter.core.utils.MtaHelper
import com.gh.gamecenter.databinding.ItemServersSubscribedGameBinding
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.feature.exposure.ExposureEvent
import com.gh.gamecenter.feature.game.GameItemViewHolder

class ServersSubscribedGameListAdapter(context: Context) : ListAdapter<GameEntity>(context) {

    private var onUnsubscribeClickListener: OnUnsubscribeClickListener? = null

    fun setOnUnsubscribeClickListener(onUnsubscribeClickListener: OnUnsubscribeClickListener) {
        this.onUnsubscribeClickListener = onUnsubscribeClickListener
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ServersCalendarSubscriptionViewHolder(
            ItemServersSubscribedGameBinding.inflate(
                parent.layoutInflater,
                parent,
                false
            )
        )
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        if (holder is ServersCalendarSubscriptionViewHolder) {
            val game = mEntityList[position]
            holder.binding.icon.displayGameIcon(game)
            holder.binding.gameName.text = game.name
            GameItemViewHolder.initGameSubtitleAndAdLabel(game, holder.binding.subtitle)

            val exposureEvent = ExposureEvent.createEvent(
                game,
                listOf(ExposureSource("开服管理", "游戏订阅"))
            )

            holder.binding.subscribe.setOnClickListener {
                onUnsubscribeClickListener?.onUnsubscribeClick(game, position)
            }

            holder.binding.root.setOnClickListener {
                GameDetailActivity.startGameDetailActivity(mContext, game.id, "开服管理-游戏订阅", exposureEvent)
            }
        }
    }

    override fun getItemCount(): Int = mEntityList.size

    class ServersCalendarSubscriptionViewHolder(val binding: ItemServersSubscribedGameBinding) : ViewHolder(binding.root)

    interface OnUnsubscribeClickListener {
        fun onUnsubscribeClick(gameEntity: GameEntity, position: Int)
    }
}