package com.gh.gamecenter.home.custom.viewholder

import android.graphics.drawable.ColorDrawable
import android.view.View
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.constraintlayout.widget.ConstraintSet
import androidx.recyclerview.widget.RecyclerView
import com.gh.common.databind.BindingAdapters
import com.gh.common.util.DownloadItemUtils
import com.gh.gamecenter.adapter.viewholder.GameViewHolder
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.core.utils.RandomUtils
import com.gh.gamecenter.databinding.ItemHomeHorizontalSlideVideoCustomBinding
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.feature.exposure.ExposureEvent
import com.gh.gamecenter.feature.exposure.IExposureProvider
import com.gh.gamecenter.feature.game.GameItemViewHolder
import com.gh.gamecenter.feature.minigame.MiniGameItemHelper
import com.gh.gamecenter.home.custom.eventlistener.SubjectEventHelper
import com.shuyu.gsyvideoplayer.builder.GSYVideoOptionBuilder
import splitties.views.backgroundColor

class CustomHomeHorizontalSlideVideoItemViewHolder(
    private val eventHelper: SubjectEventHelper,
    val binding: ItemHomeHorizontalSlideVideoCustomBinding
) :
    BaseRecyclerViewHolder<Any>(binding.root), IExposureProvider {

    private var mBoundedGameEntity: GameEntity? = null

    fun bindAttachGame(
        childPosition: Int,
        gameEntity: GameEntity,
        adapter: RecyclerView.Adapter<out RecyclerView.ViewHolder>,
        entrance: String,
        showSubscript: Boolean,
        showSubTitle: Boolean
    ) {
        binding.clRoot.backgroundColor = com.gh.gamecenter.common.R.color.ui_surface.toColor(itemView.context)
        bindGameInfo(
            childPosition,
            gameEntity,
            adapter,
            entrance,
            showSubscript,
            showSubTitle
        )
        binding.gameImage.setTag(ImageUtils.TAG_TARGET_WIDTH, 280F.dip2px())
        binding.autoVideoView.thumbImage.setTag(ImageUtils.TAG_TARGET_WIDTH, 280F.dip2px())
        binding.gameImage.goneIf(!gameEntity.showImage) {
            ImageUtils.display(binding.gameImage, gameEntity.homeSetting.image)
            val hierarchy = binding.gameImage.hierarchy
            try {
                hierarchy.setPlaceholderImage(ColorDrawable(gameEntity.homeSetting.placeholderColor.hexStringToIntColor()))
            } catch (ignore: Throwable) {
                hierarchy.setPlaceholderImage(RandomUtils.getRandomPlaceholderColor())
            }
        }
        binding.autoVideoView.goneIf(gameEntity.showImage || gameEntity.topVideo == null) {
            val videoUrl = gameEntity.topVideo?.url ?: ""
            if (!binding.autoVideoView.isInPlayingState || binding.autoVideoView.getUrl() != videoUrl) {
                GSYVideoOptionBuilder()
                    .setIsTouchWiget(false)
                    .setUrl(videoUrl)
                    .setRotateViewAuto(false)
                    .setCacheWithPlay(true)
                    .setRotateWithSystem(false)
                    .setReleaseWhenLossAudio(true)
                    .setLooping(false)
                    .setShowFullAnimation(false)
                    .build(binding.autoVideoView)
                binding.autoVideoView.updateThumb(gameEntity.topVideo?.poster ?: "")
                binding.autoVideoView.setParamsData(gameEntity, gameEntity.homeSetting.placeholderColor)
                binding.autoVideoView.setOnVideoClickListener { itemView.performClick() }
            }
        }

        itemView.setOnClickListener {
            eventHelper.navigateToGameDetailPage(childPosition, gameEntity)
        }
    }

    private fun bindGameInfo(
        childPosition: Int,
        gameEntity: GameEntity,
        adapter: RecyclerView.Adapter<out RecyclerView.ViewHolder>,
        entrance: String,
        showSubscript: Boolean,
        showSubTitle: Boolean
    ) {
        mBoundedGameEntity = gameEntity
        binding.gameIcon.displayGameIconWithIfShowSubscript(gameEntity, showSubscript)
        binding.gameName.text = gameEntity.name
        binding.gameName.setTextColor(com.gh.gamecenter.common.R.color.text_primary.toColor(binding.root.context))
        BindingAdapters.setGameTags(binding.gameTags, gameEntity, "")
        GameItemViewHolder.initGameSubtitleAndAdLabelOnGameSubject(
            gameEntity,
            binding.gameSubtitleTv,
            canShowTitle = showSubTitle
        )

        MiniGameItemHelper.setMiniGameUsage(binding.gamePlayCount, gameEntity)

        DownloadItemUtils.setOnClickListener(
            binding.root.context,
            binding.downloadBtn,
            gameEntity,
            childPosition,
            adapter,
            entrance,
            location = "",
            traceEvent = null
        ) {
            eventHelper.onDownloadButtonClick(childPosition, gameEntity)
        }

        DownloadItemUtils.updateItem(
            binding.root.context,
            gameEntity,
            GameViewHolder(binding.root).apply {
                gameDownloadBtn = binding.downloadBtn
                multiVersionDownloadTv = binding.multiVersionDownloadTv
                gameDownloadTips = binding.downloadTipsLottie
            }
        )

        ConstraintSet().apply {
            clone(binding.gameNameContainer.parent as ConstraintLayout)
            clear(binding.gameNameContainer.id, ConstraintSet.END)
            if (binding.downloadBtn.visibility == View.VISIBLE) {
                connect(binding.gameNameContainer.id, ConstraintSet.END, binding.downloadBtn.id, ConstraintSet.START)
            } else {
                connect(binding.gameNameContainer.id, ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END)
            }
            setMargin(binding.gameNameContainer.id, ConstraintSet.END, 16F.dip2px())
            applyTo(binding.gameNameContainer.parent as ConstraintLayout)
        }

    }

    override fun provideExposureData(): ExposureEvent? {
        return mBoundedGameEntity?.exposureEvent?.getFreshExposureEvent()
    }
}