package com.gh.gamecenter.forum.home.follow.viewholder

import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.facebook.drawee.view.SimpleDraweeView
import com.gh.common.util.NewsUtils
import com.gh.common.view.ImageContainerView
import com.gh.gamecenter.ImageViewerActivity
import com.gh.gamecenter.R
import com.gh.gamecenter.common.utils.debounceActionWithInterval
import com.gh.gamecenter.common.utils.toArrayList
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.databinding.RecyclerGameArticleBinding
import com.gh.gamecenter.entity.FollowDynamicEntity
import com.gh.gamecenter.feature.entity.ArticleEntity
import com.gh.gamecenter.feature.entity.ConcernEntity
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.feature.entity.SimpleGame

class GameArticleViewHolder(
    val binding: RecyclerGameArticleBinding,
    private val listener: OnChildEventListener
) : ViewHolder(binding.root) {

    fun bind(article: FollowDynamicEntity.Article) {
        val game = article.game
        binding.tvGameName.text = game.name
        binding.ivIcon.displayGameIcon(game)
        binding.tvTime.text = NewsUtils.getFormattedTime(article.time)
        binding.tvTitle.text = article.title
        binding.tvDescription.text = article.content

        val images = article.img.take(1).map {
            ImageContainerView.ImageContainerData.ImageInfo(it, 3, 2)
        }
        val imageContainerData = ImageContainerView.ImageContainerData("", false, images, show = images.isNotEmpty())

        binding.ivImagesContainer.bindData(imageContainerData,
            object : ImageContainerView.OnImageContainerEventListener {
                override fun onImageClick(
                    images: List<String>,
                    position: Int,
                    imageViewList: ArrayList<SimpleDraweeView>
                ) {
                    val checkIntent = ImageViewerActivity.getIntent(
                        binding.root.context,
                        images.toArrayList(),
                        position,
                        imageViewList,
                        ""
                    )
                    binding.root.context.startActivity(checkIntent)
                }

                override fun onVideoCLick(videoId: String) = Unit

            })

        arrayOf(binding.ivIcon, binding.tvTitle).forEach {
            it.setOnClickListener {
                listener.onGameCLick(game)
            }
        }

        itemView.setOnClickListener {
            listener.onArticleClick(article)
        }

        binding.tvComment.setOnClickListener {
            listener.onComment(article)
        }

        binding.tvShare.setOnClickListener {
            debounceActionWithInterval(800) {
                listener.onShare(toConcernEntity(article))
            }

        }
    }

    private fun toConcernEntity(article: FollowDynamicEntity.Article): ConcernEntity {
        val simpleGame = SimpleGame().apply {
            mName = article.game.name
        }
        return ConcernEntity().apply {
            id = article.id
            title = article.title
            brief = article.content
            game = simpleGame
            gameIcon = article.game.icon
            img = article.img
            shortId = article.shortId
        }
    }

    interface OnChildEventListener {

        fun onGameCLick(game: GameEntity)

        fun onArticleClick(article: FollowDynamicEntity.Article)

        fun onComment(article: FollowDynamicEntity.Article)

        fun onShare(concernEntity: ConcernEntity)
    }
}