package com.gh.gamecenter.gamedetail.desc

import android.content.Context
import android.util.SparseArray
import android.view.View
import android.view.ViewGroup
import androidx.core.util.forEach
import androidx.recyclerview.widget.RecyclerView
import com.facebook.drawee.view.SimpleDraweeView
import com.gh.common.util.DataCollectionUtils
import com.gh.common.util.DirectUtils
import com.gh.common.util.NewFlatLogUtils
import com.gh.gamecenter.ImageViewerActivity
import com.gh.gamecenter.R
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.common.view.WrapContentDraweeView
import com.gh.gamecenter.core.utils.MtaHelper
import com.gh.gamecenter.databinding.GalleryVideoItemBinding
import com.gh.gamecenter.databinding.GamedetailScreenshotItemBinding
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.gamedetail.entity.Video
import com.gh.gamecenter.video.detail.VideoDetailContainerViewModel

class GameGalleryAdapter(
    var context: Context,
    private val mVideo: ArrayList<Video>? = null,
    private val mGallery: ArrayList<String>? = null,
    val mGame: GameEntity,
    private val mEntrance: String
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    private val mImageViewArray = SparseArray<SimpleDraweeView>()
    private val mDefaultHorizontalPadding by lazy { R.dimen.game_detail_item_horizontal_padding.toPx() }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return when (viewType) {
            VIDEO -> {
                VideoViewHolder(parent.toBinding())
            }
            IMAGE -> {
                GameGalleryViewHolder(parent.toBinding())
            }
            else -> throw NullPointerException()
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (holder) {
            is GameGalleryViewHolder -> {
                holder.binding.screenshotItemIv.setTag(ImageUtils.TAG_TARGET_WIDTH, 260F.dip2px())
                ImageUtils.display(holder.binding.screenshotItemIv, mGallery?.get(position))
                holder.binding.screenshotItemIv.registerLoadingCallback(object : WrapContentDraweeView.LoadingCallback {
                    override fun loaded() {
                        holder.binding.screenshotItemIv.post {
                            holder.binding.screenshotItemIv.layoutParams.apply {
                                height = holder.binding.screenshotItemIv.height
                                width =
                                    (holder.binding.screenshotItemIv.height * holder.binding.screenshotItemIv.aspectRatio).toInt()
                                holder.binding.screenshotItemIv.layoutParams = this
                            }

                            holder.itemView.requestLayout()
                        }
                    }
                })
                holder.itemView.setDebouncedClickListener {
                    DataCollectionUtils.uploadClick(context, "游戏介绍", "游戏详情")
                    MtaHelper.onEvent("游戏详情_新", "点击游戏截图", mGame.name)

                    val imageViewList = ArrayList<View>()
                    mImageViewArray.forEach { _, value ->
                        imageViewList.add(value)
                    }

                    val intent = ImageViewerActivity.getIntent(
                        context,
                        mGallery ?: arrayListOf(),
                        holder.adapterPosition,
                        imageViewList,
                        mEntrance
                    )
                    context.startActivity(intent)
                }
                mImageViewArray.put(position, holder.binding.screenshotItemIv)
            }
            is VideoViewHolder -> {
                val video = mVideo?.get(position)
                ImageUtils.display(holder.binding.screenshotItemIv, mVideo?.get(position)!!.poster)
                holder.binding.videoTitleTv.setTextColor(R.color.text_title.toColor(holder.binding.videoTitleTv.context))
                holder.binding.usernameTv.text = video?.user?.name
                holder.binding.videoTitleTv.text = video?.title
                holder.binding.voteCountTv.text = video?.vote.toString()
                holder.binding.screenshotItemIv.setOnClickListener {
                    MtaHelper.onEvent("游戏详情_新", "点击视频", "${mGame.name}+${video?.title}")
                    NewFlatLogUtils.logClickGameDetailVideoCategory(
                        "video",
                        video?.videoId ?: "",
                        video?.user?.id ?: ""
                    )
                    DirectUtils.directToVideoDetail(
                        context, video?.videoId
                            ?: "", VideoDetailContainerViewModel.Location.GAME_DETAIL.value,
                        false, mGame.id, mEntrance, "游戏详情"
                    )
                }
            }
        }
        val params = holder.itemView.layoutParams as RecyclerView.LayoutParams

        params.leftMargin = if (position == 0) mDefaultHorizontalPadding else 8F.dip2px()
        params.rightMargin = if (position == itemCount - 1) mDefaultHorizontalPadding else 0F.dip2px()
        holder.itemView.layoutParams = params
    }

    override fun getItemCount(): Int {
        return when {
            mVideo != null -> mVideo.size
            mGallery != null -> mGallery.size
            else -> 0
        }
    }

    override fun getItemViewType(position: Int): Int {
        return when {
            mVideo != null -> VIDEO
            mGallery != null -> IMAGE
            else -> 0
        }
    }

    companion object {
        const val IMAGE = 223
        const val VIDEO = 224
    }

    class GameGalleryViewHolder(val binding: GamedetailScreenshotItemBinding) :
        BaseRecyclerViewHolder<Any>(binding.root)

    class VideoViewHolder(var binding: GalleryVideoItemBinding) : RecyclerView.ViewHolder(binding.root)

}
