package com.gh.gamecenter.gamedetail.detail.viewholder

import android.view.ViewGroup
import android.view.ViewGroup.MarginLayoutParams
import androidx.core.view.updateLayoutParams
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.gh.common.util.DirectUtils
import com.gh.common.util.NewFlatLogUtils
import com.gh.gamecenter.R
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.databinding.ItemGameDetailRecyclerViewBinding
import com.gh.gamecenter.databinding.ItemGameDetailVideoBinding
import com.gh.gamecenter.feature.view.DownloadButton
import com.gh.gamecenter.gamedetail.GameDetailViewModel
import com.gh.gamecenter.gamedetail.entity.GameDetailData
import com.gh.gamecenter.gamedetail.entity.GameDetailVideo
import com.gh.gamecenter.video.detail.VideoDetailContainerViewModel

class GameDetailVideoItemViewHolder(
    val binding: ItemGameDetailRecyclerViewBinding,
    downloadBtn: DownloadButton?,
    viewModel: GameDetailViewModel
) : BaseGameDetailItemViewHolder(binding.root, downloadBtn, viewModel) {
    override fun bindView(data: GameDetailData) {
        super.bindView(data)
        val entity = data.linkGameVideo ?: return
        binding.run {
            titleTv.setTextColor(com.gh.gamecenter.common.R.color.text_primary.toColor(context))
            moreTv.setTextColor(com.gh.gamecenter.common.R.color.text_theme.toColor(context))
            moreTv.setDrawableEnd(R.drawable.ic_auxiliary_arrow_right_text_theme_12.toDrawable(context))
            titleTv.text = "游戏视频"
            moreTv.goneIf(entity.count < 3) {
                moreTv.text = "更多"
                moreTv.setOnClickListener {
                    DirectUtils.directToGameVideo(context, viewModel.gameId ?: "", viewModel.entrance, "游戏详情")
                    SensorsBridge.trackGameDetailModuleClick(
                        gameId,
                        gameName,
                        gameType,
                        "右上角",
                        moduleType,
                        "游戏视频",
                        sequence,
                        gameStatus = gameStatus
                    )
                }
            }

            if (recyclerView.adapter !is VideoItemAdapter) {
                recyclerView.isNestedScrollingEnabled = false
                (recyclerView.itemAnimator as DefaultItemAnimator).supportsChangeAnimations = false
                recyclerView.layoutManager = LinearLayoutManager(context, RecyclerView.HORIZONTAL, false)
                recyclerView.adapter = VideoItemAdapter(entity.video)
            } else {
                recyclerView.adapter?.run { notifyItemRangeChanged(0, itemCount) }
            }
        }
    }

    inner class VideoItemAdapter(val dataList: List<GameDetailVideo.Video>) :
        RecyclerView.Adapter<RecyclerView.ViewHolder>() {

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder =
            VideoItemViewHolder(parent.toBinding())

        override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
            val video = dataList.getOrNull(position) ?: return
            if (holder is VideoItemViewHolder) {
                holder.binding.run {
                    root.updateLayoutParams<MarginLayoutParams> {
                        leftMargin = if (position == 0) 16F.dip2px() else 8F.dip2px()
                        rightMargin = if (position == itemCount - 1) 16F.dip2px() else 0
                    }
                    videoTitleTv.setTextColor(com.gh.gamecenter.common.R.color.text_primary.toColor(context))
                    videoTitleTv.text = video.title
                    ImageUtils.display(videoIv, video.poster)
                    root.setOnClickListener {
                        NewFlatLogUtils.logClickGameDetailVideoCategory(
                            "video",
                            video.id,
                            video.user?.id ?: ""
                        )
                        DirectUtils.directToVideoDetail(
                            context, video.id, VideoDetailContainerViewModel.Location.GAME_DETAIL.value,
                            false, viewModel.game?.id ?: "", viewModel.entrance, "游戏详情"
                        )
                        SensorsBridge.trackGameDetailModuleClick(
                            gameId,
                            gameName,
                            gameType,
                            "组件内容",
                            moduleType,
                            "游戏视频",
                            sequence,
                            video.title,
                            position + 1,
                            linkType = "video",
                            linkId = video.id,
                            linkText = video.title,
                            gameStatus = gameStatus
                        )
                    }
                }
            }
        }

        override fun getItemCount(): Int = dataList.size
    }

    inner class VideoItemViewHolder(val binding: ItemGameDetailVideoBinding) :
        RecyclerView.ViewHolder(binding.root)
}