package com.gh.gamecenter.home.custom.adapter

import android.content.Context
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.common.utils.SensorsBridge
import com.gh.gamecenter.common.utils.toBinding
import com.gh.gamecenter.databinding.ItemHomeVgameBinding
import com.gh.gamecenter.feature.entity.AcctRecord
import com.gh.gamecenter.gamedetail.accelerator.GameDetailAcceleratorUiHelper.Companion.SCENE_TYPE_NO_GUIDE_LAYER
import com.gh.gamecenter.gamedetail.accelerator.GameDetailAcceleratorUiHelper.Companion.SOURCE_ENTRANCE_RECENTLY_PLAYED
import com.gh.gamecenter.gamedetail.accelerator.chain.AcceleratorClient
import com.gh.gamecenter.gamedetail.accelerator.chain.AcceleratorValidator
import com.gh.gamecenter.gamedetail.accelerator.dialog.StartingAcceleratorDialogFragment
import com.halo.assistant.accelerator.repository.AcceleratorDataHolder

class CustomRecentAcceleratorAdapter(
    context: Context
) : CustomBaseChildAdapter<AcctRecord, CustomRecentAcceleratorAdapter.AcceleratorViewHolder>(context) {

    private val acceleratorDataHolder = AcceleratorDataHolder.instance

    override fun getKey(t: AcctRecord): String {
        return t.gameId
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AcceleratorViewHolder =
        AcceleratorViewHolder(acceleratorDataHolder, parent.toBinding())

    override fun onBindViewHolder(holder: AcceleratorViewHolder, position: Int) {
        holder.bindView(getItem(position))
    }

    class AcceleratorViewHolder(
        private val acceleratorDataHolder: AcceleratorDataHolder,
        private var mBinding: ItemHomeVgameBinding
    ) :
        RecyclerView.ViewHolder(mBinding.root) {

        fun bindView(acctRecord: AcctRecord) {
            val game = acctRecord.game
            mBinding.gameIconIv.displayGameIcon(game)

            mBinding.maskView.visibility = View.GONE
            mBinding.controlTv.visibility = View.GONE
            mBinding.progressBar.visibility = View.GONE
            mBinding.dotView.visibility = View.GONE
            mBinding.updateHintIv.visibility = View.GONE
            mBinding.controlTv.visibility = View.GONE
            mBinding.progressBar.visibility = View.GONE

            val context = mBinding.root.context
            mBinding.root.setOnClickListener {
                SensorsBridge.trackNetworkAccelerationButtonClick(
                    game.getUniquePackageName() ?: "",
                    game.id,
                    game.name ?: "",
                    AcceleratorDataHolder.instance.memberType,
                    acctRecord.zoneInfo.cnName ?: "",
                    SCENE_TYPE_NO_GUIDE_LAYER,
                    SOURCE_ENTRANCE_RECENTLY_PLAYED
                )
                val request = AcceleratorValidator.Request(game, SOURCE_ENTRANCE_RECENTLY_PLAYED)
                AcceleratorClient.newInstance()
                    .execute(context, request, object : AcceleratorValidator.ValidateListener {
                        override fun finished(context: Context) {
                            StartingAcceleratorDialogFragment.show(
                                context,
                                acctRecord.zoneInfo,
                                game,
                                true,
                                acctRecord.hasMultiZone,
                                SOURCE_ENTRANCE_RECENTLY_PLAYED
                            )
                        }

                    })

            }
        }
    }
}