package com.gh.gamecenter.qa.article.detail

import android.content.Context
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.gh.gamecenter.R
import com.gh.gamecenter.common.baselist.MutableListAdapter
import com.gh.gamecenter.common.entity.CommunityEntity
import com.gh.gamecenter.common.utils.ImageUtils
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.utils.toBinding
import com.gh.gamecenter.databinding.ItemArticleDetailRelatedContentBinding
import com.gh.gamecenter.feature.entity.ArticleEntity
import com.gh.gamecenter.qa.questions.newdetail.NewQuestionDetailActivity
import com.gh.gamecenter.qa.video.detail.ForumVideoDetailActivity
import splitties.views.backgroundColor

class ArticleDetailRelatedContentListAdapter(
    context: Context,
    private val entrance: String
) : MutableListAdapter<ArticleEntity>(context) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ArticleDetailRelatedContentViewHolder(parent.toBinding())
    }

    override fun onViewAttachedToWindow(holder: ViewHolder) {
        super.onViewAttachedToWindow(holder)
        // 重新设置一下字体和背景颜色，这是为了解决切换到主题模式下，部分列表项颜色显示异常的问题
        // 貌似是因为RecyclerView里面detached的View，无法感知主题模式的颜色变化
        holder.itemView.backgroundColor = ContextCompat.getColor(
            holder.itemView.context,
            com.gh.gamecenter.common.R.color.ui_surface
        )
        if (holder is ArticleDetailRelatedContentViewHolder) {
            holder.binding.title.setTextColor(
                ContextCompat.getColor(
                    holder.itemView.context,
                    com.gh.gamecenter.common.R.color.text_primary
                )
            )
            holder.binding.subtitle.setTextColor(
                ContextCompat.getColor(
                    holder.itemView.context,
                    com.gh.gamecenter.common.R.color.text_secondary
                )
            )
            holder.binding.voteAndComment.setTextColor(
                ContextCompat.getColor(
                    holder.itemView.context,
                    com.gh.gamecenter.common.R.color.text_tertiary
                )
            )
        }
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val item = mEntityList[position]
        when (holder) {
            is ArticleDetailRelatedContentViewHolder -> {
                ImageUtils.display(holder.binding.cover, item.images.firstOrNull())
                holder.binding.title.text = item.title
                holder.binding.subtitle.text = item.brief
                holder.binding.voteAndComment.text = mContext.getString(
                    R.string.article_detail_related_content_vote_and_comment,
                    item.count.vote,
                    item.count.comment
                )
                if (position == 0) {
                    holder.binding.root.setPadding(16F.dip2px(), 16F.dip2px(), 16F.dip2px(), 0)
                } else {
                    holder.binding.root.setPadding(16F.dip2px(), 0, 16F.dip2px(), 0)
                }
                holder.binding.root.setOnClickListener {
                    when (item.type) {
                        "question" -> {
                            val intent = NewQuestionDetailActivity.getCommentIntent(it.context, item.id, entrance, "")
                            it.context.startActivity(intent)
                        }

                        "answer" -> {
                            val intent = NewQuestionDetailActivity.getCommentIntent(
                                it.context,
                                item.questions.id,
                                item.id,
                                entrance,
                                "",
                                ""
                            )
                            it.context.startActivity(intent)
                        }

                        "video" -> {
                            it.context.startActivity(
                                ForumVideoDetailActivity.getIntent(
                                    it.context,
                                    item.id,
                                    item.community.id,
                                    true
                                )
                            )
                        }

                        else -> {
                            val communityId = item.community.id
                            val intent = ArticleDetailActivity.getIntent(
                                it.context,
                                CommunityEntity(communityId, item.community.name),
                                item.id,
                                entrance, ""
                            )
                            it.context.startActivity(intent)
                        }
                    }
                }
            }
        }
    }

    override fun getItemCount(): Int = mEntityList.size

    inner class ArticleDetailRelatedContentViewHolder(
        val binding: ItemArticleDetailRelatedContentBinding
    ) : ViewHolder(binding.root)
}