package com.gh.gamecenter.forum.home

import android.content.Context
import android.graphics.Color
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.gh.common.util.NewLogUtils
import com.gh.gamecenter.R
import com.gh.gamecenter.common.base.activity.BaseActivity
import com.gh.gamecenter.common.baselist.ListAdapter
import com.gh.gamecenter.common.constant.ItemViewType
import com.gh.gamecenter.common.entity.CommunityEntity
import com.gh.gamecenter.common.syncpage.ISyncAdapterHandler
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.common.viewholder.FooterViewHolder
import com.gh.gamecenter.core.utils.MtaHelper
import com.gh.gamecenter.databinding.CommunityAnswerItemBinding
import com.gh.gamecenter.qa.article.detail.ArticleDetailActivity
import com.gh.gamecenter.qa.entity.ArticleEntity
import com.gh.gamecenter.qa.questions.newdetail.NewQuestionDetailActivity
import com.gh.gamecenter.qa.video.detail.ForumVideoDetailActivity

class ForumArticleListAdapter(
    context: Context,
    val mEntrance: String,
    val path: String,
    val viewModel: ForumArticleListViewModel
) : ListAdapter<ArticleEntity>(context), ISyncAdapterHandler {

    override fun areItemsTheSame(oldItem: ArticleEntity?, newItem: ArticleEntity?): Boolean {
        return oldItem?.id == newItem?.id
    }

    override fun getItemViewType(position: Int): Int {
        if (position == itemCount - 1) return ItemViewType.ITEM_FOOTER
        return ItemViewType.ITEM_BODY
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val view: View
        return when (viewType) {
            ItemViewType.ITEM_FOOTER -> {
                view = mLayoutInflater.inflate(R.layout.refresh_footerview, parent, false)
                FooterViewHolder(view)
            }

            else -> {
                view = mLayoutInflater.inflate(R.layout.community_answer_item, parent, false)
                ForumArticleAskItemViewHolder(CommunityAnswerItemBinding.bind(view))
            }
        }
    }

    override fun getItemCount(): Int {
        return if (mEntityList.isNotEmpty()) mEntityList.size + FOOTER_ITEM_COUNT else 0
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (getItemViewType(position)) {
            ItemViewType.ITEM_BODY -> {
                val viewHolder = holder as ForumArticleAskItemViewHolder
                val articleEntity = mEntityList[position]
                articleEntity.community = CommunityEntity(articleEntity.bbs.id, articleEntity.bbs.name)

                if (articleEntity.type == "bbs_article") articleEntity.type = "community_article"
                if (articleEntity.type == "bbs_question") articleEntity.type = "question"
                if (articleEntity.type == "bbs_video") articleEntity.type = "video"

                viewHolder.binding.run {
                    root.layoutParams = (root.layoutParams as ViewGroup.MarginLayoutParams).apply {
                        topMargin = if (position == 0) 8F.dip2px() else 0
                    }
                    if (position == 0) {
                        root.background = R.drawable.background_shape_white_radius_12_top_only.toDrawable(mContext)
                    } else {
                        root.setBackgroundColor(R.color.background_white.toColor(mContext))
                    }
                    rightContainer.setBackgroundColor(Color.TRANSPARENT)
                    topLine.goneIf(position == 0)
                }
                viewHolder.bindForumArticleItem(articleEntity, mEntrance, 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
                }

                viewHolder.itemView.setOnClickListener {
                    articleEntity.run {
                        val contentType = when (articleEntity.type) {
                            "community_article" -> "帖子"
                            "video" -> "视频帖"
                            "question" -> "提问帖"
                            else -> "提问帖评论"
                        }
                        val bbsType = if (bbs.type == "official_bbs") "综合论坛" else "游戏论坛"
                        NewLogUtils.logRecommendFeedContentClick(
                            "click_for_you_content",
                            contentType,
                            id,
                            position + 1,
                            bbs.id,
                            bbsType,
                            user.id ?: ""
                        )
                    }

                    when (articleEntity.type) {
                        "community_article" -> {
                            MtaHelper.onEvent(
                                "论坛首页",
                                viewHolder.getKey(
                                    BaseActivity.mergeEntranceAndPath(
                                        mEntrance,
                                        path
                                    )
                                ),
                                "${articleEntity.title}（${articleEntity.id}）"
                            )
                            mContext.startActivity(
                                ArticleDetailActivity.getRecommendIntent(
                                    mContext,
                                    articleEntity.community,
                                    articleEntity.id,
                                    articleEntity.recommendId,
                                    "",
                                    path
                                )
                            )
                        }
                        "video" -> {
                            MtaHelper.onEvent(
                                "论坛首页",
                                viewHolder.getKey(
                                    BaseActivity.mergeEntranceAndPath(
                                        mEntrance,
                                        path
                                    )
                                ),
                                "${articleEntity.title}（${articleEntity.id}）"
                            )
                            mContext.startActivity(
                                ForumVideoDetailActivity.getRecommendIntent(
                                    mContext,
                                    articleEntity.id,
                                    articleEntity.community.id,
                                    articleEntity.recommendId
                                )
                            )
                        }
                        "question" -> {
                            MtaHelper.onEvent(
                                "论坛首页",
                                viewHolder.getKey(
                                    BaseActivity.mergeEntranceAndPath(
                                        mEntrance,
                                        path
                                    )
                                ),
                                "${articleEntity.title}（${articleEntity.id}）"
                            )
                            mContext.startActivity(
                                NewQuestionDetailActivity.getRecommendIntent(
                                    mContext,
                                    articleEntity.id,
                                    "",
                                    articleEntity.recommendId,
                                    mEntrance,
                                    path
                                )
                            )
                        }
                        "answer" -> {
                            MtaHelper.onEvent(
                                "论坛首页",
                                viewHolder.getKey(
                                    BaseActivity.mergeEntranceAndPath(
                                        mEntrance,
                                        path
                                    )
                                ),
                                "${articleEntity.title}（${articleEntity.id}）"
                            )
                            mContext.startActivity(
                                NewQuestionDetailActivity.getRecommendIntent(
                                    mContext,
                                    articleEntity.questions.id,
                                    articleEntity.id,
                                    articleEntity.recommendId,
                                    mEntrance,
                                    path,
                                    true
                                )
                            )
                        }
                    }
                }
            }

            ItemViewType.ITEM_FOOTER -> {
                val footerViewHolder = holder as FooterViewHolder
                footerViewHolder.initItemPadding()
                footerViewHolder.initFooterViewHolder(viewModel, mIsLoading, mIsNetworkError, mIsOver)
                footerViewHolder.hint.textSize = 12f
                footerViewHolder.hint.setTextColor(ContextCompat.getColor(mContext, R.color.aaaaaa))
            }
        }
    }

    override fun getSyncData(position: Int): Pair<String, Any>? {
        if (position >= mEntityList.size) return null
        val entity = mEntityList[position]
        return Pair(entity.id, entity)
    }
}