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

import android.content.Context
import android.graphics.Color
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.gh.gamecenter.R
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.databinding.CommunityAnswerItemBinding
import com.gh.gamecenter.feature.entity.ArticleEntity
import com.gh.gamecenter.forum.home.ForumArticleAskItemViewHolder
import com.gh.gamecenter.forum.search.CommunitySearchEventListener
import com.gh.gamecenter.forum.search.CommunitySearchEventListener.Companion.SEARCH_BUTTON_COMMENT
import com.gh.gamecenter.qa.article.detail.ArticleDetailActivity
import com.gh.gamecenter.qa.questions.newdetail.NewQuestionDetailActivity
import com.gh.gamecenter.qa.video.detail.ForumVideoDetailActivity

class FollowPostCardViewHolder(
    val binding: CommunityAnswerItemBinding,
    val entrance: String,
    val path: String
) : ViewHolder(binding.root) {

    private val context: Context
        get() = itemView.context

    private val viewHolder = ForumArticleAskItemViewHolder(binding, null)

    fun bind(data: ArticleEntity, position: Int) {
        val articleEntity = data

        viewHolder.binding.run {
            root.layoutParams = (root.layoutParams as ViewGroup.MarginLayoutParams).apply {
                topMargin = if (position == 0) 8F.dip2px() else 0
            }
            if (position == 0) {
                root.background = com.gh.gamecenter.common.R.drawable.background_shape_white_radius_12_top_only.toDrawable(context)
            } else {
                root.setBackgroundColor(com.gh.gamecenter.common.R.color.ui_surface.toColor(context))
            }
            rightContainer.setBackgroundColor(Color.TRANSPARENT)
            topLine.goneIf(position == 0)
        }
        // 关注-动态列表一定是已关注的数据
        articleEntity.me.isFollower = true
        articleEntity.brief = articleEntity.content
        viewHolder.bindForumArticleItem(articleEntity, entrance, path, position)

        if (articleEntity.type == "question") {
            if (articleEntity.count.answer > 0) {
                viewHolder.commentCount.text = articleEntity.count.answer.toString()
            } else {
                viewHolder.commentCount.text = "回答"
            }
            viewHolder.commentCount.setDrawableStart(R.drawable.community_comment_count)
            viewHolder.voteCountContainer.visibility = View.GONE
        } else {
            viewHolder.voteCountContainer.visibility = View.VISIBLE
        }

        itemView.setOnClickListener {
            SensorsBridge.trackFollowPageContentClick("帖子", articleEntity.title)

            when (articleEntity.type) {
                "community_article" -> {
                    context.startActivity(
                        ArticleDetailActivity.getRecommendIntent(
                            context,
                            articleEntity.community,
                            articleEntity.id,
                            articleEntity.recommendId,
                            "",
                            path,
                            "社区-关注"
                        )
                    )
                }

                "video" -> {

                    context.startActivity(
                        ForumVideoDetailActivity.getRecommendIntent(
                            context,
                            articleEntity.id,
                            articleEntity.community.id,
                            articleEntity.recommendId,
                            "社区-关注"
                        )
                    )
                }

                "question" -> {
                    context.startActivity(
                        NewQuestionDetailActivity.getRecommendIntent(
                            context,
                            articleEntity.id,
                            "",
                            articleEntity.recommendId,
                            "",
                            path,
                            sourceEntrance = "社区-关注"
                        )
                    )
                }

                "answer" -> {
                    context.startActivity(
                        NewQuestionDetailActivity.getRecommendIntent(
                            context,
                            articleEntity.questions.id,
                            articleEntity.id,
                            articleEntity.recommendId,
                            "",
                            path,
                            true,
                            sourceEntrance = "社区-关注"
                        )
                    )
                }
            }
        }
    }

}