package com.gh.gamecenter.home.horizontalslidevideo

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.GameDetailActivity
import com.gh.gamecenter.R
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.ItemHomeHorizontalSlideVideoBinding
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.feature.exposure.ExposureEvent
import com.gh.gamecenter.feature.game.GameItemViewHolder
import com.shuyu.gsyvideoplayer.builder.GSYVideoOptionBuilder

class HomeHorizontalSlideVideoItemViewHolder(val binding: ItemHomeHorizontalSlideVideoBinding) :
    BaseRecyclerViewHolder<Any>(binding.root) {

    fun bindAttachGame(
        gameEntity: GameEntity,
        adapter: RecyclerView.Adapter<out RecyclerView.ViewHolder>,
        entrance: String,
        exposureEvent: ExposureEvent
    ) {
        bindGameInfo(gameEntity, adapter, exposureEvent, entrance)
        binding.gameImage.goneIf(gameEntity.topVideo != null || gameEntity.homeSetting.image.isEmpty()) {
            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.topVideo == null) {
            if (!binding.autoVideoView.isInPlayingState) {
                GSYVideoOptionBuilder()
                    .setIsTouchWiget(false)
                    .setUrl(gameEntity.topVideo?.url ?: "")
                    .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 {
            GameDetailActivity.startGameDetailActivity(
                binding.root.context,
                gameEntity.id,
                "(${entrance}-游戏[" + gameEntity.name + "])",
                exposureEvent
            )
        }
    }

    private fun bindGameInfo(
        gameEntity: GameEntity,
        adapter: RecyclerView.Adapter<out RecyclerView.ViewHolder>,
        exposureEvent: ExposureEvent?,
        entrance: String
    ) {
        binding.gameIcon.displayGameIcon(gameEntity)
        binding.gameName.text = gameEntity.name
        binding.gameName.setTextColor(R.color.text_title.toColor(binding.root.context))
        BindingAdapters.setGameTags(binding.gameTags, gameEntity)
        GameItemViewHolder.initGameSubtitleAndAdLabel(gameEntity, binding.gameSubtitleTv)

        DownloadItemUtils.setOnClickListener(
            binding.root.context,
            binding.downloadBtn,
            gameEntity,
            position,
            adapter,
            entrance,
            "",
            exposureEvent
        )
        DownloadItemUtils.updateItem(
            binding.root.context,
            gameEntity,
            GameViewHolder(binding.root).apply {
                gameDownloadBtn = binding.downloadBtn
                multiVersionDownloadTv = binding.multiVersionDownloadTv
                gameDownloadTips = binding.downloadTipsLottie
            },
            true
        )

        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)
        }

    }
}