package com.gh.gamecenter.qa.article

import android.content.Context
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.common.constant.ItemViewType
import com.gh.gamecenter.common.syncpage.ISyncAdapterHandler
import com.gh.gamecenter.R
import com.gh.gamecenter.common.viewholder.FooterViewHolder
import com.gh.gamecenter.common.baselist.ListAdapter
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.databinding.CommunityAnswerItemBinding
import com.gh.gamecenter.forum.home.ForumArticleAskItemViewHolder
import com.gh.gamecenter.qa.article.detail.ArticleDetailActivity
import com.gh.gamecenter.qa.entity.ArticleEntity
import com.gh.gamecenter.qa.questions.newdetail.NewQuestionDetailActivity

class MyArticleAdapter(
    context: Context,
    private val mEntrance: String,
    private val mPath: String
) : ListAdapter<ArticleEntity>(context), ISyncAdapterHandler {

    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.size == 0) 0 else mEntityList.size + 1
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is ForumArticleAskItemViewHolder) {
            val entity = mEntityList[position]
            holder.bindForumArticleItem(entity, mEntrance, mPath, position)
            holder.binding.run {
                userIcon.visibility = View.GONE
                userName.visibility = View.GONE
                userBadgeIcon.visibility = View.GONE
                userBadgeName.visibility = View.GONE
                concernBtn.visibility = View.GONE
                timeContainer.visibility = View.GONE
                title.layoutParams = (title.layoutParams as ViewGroup.MarginLayoutParams).apply {
                    topMargin = 20F.dip2px()
                }
            }

            holder.itemView.setOnClickListener {
                if (entity.type == "question") {
                    mContext.startActivity(NewQuestionDetailActivity.getIntent(mContext, entity.id, mEntrance, mPath))
                } else {
                    mContext.startActivity(
                        ArticleDetailActivity.getIntent(
                            mContext,
                            entity.bbs,
                            entity.id,
                            mEntrance,
                            mPath
                        )
                    )
                }
            }
        } else if (holder is FooterViewHolder) {
            holder.initFooterViewHolder(mIsLoading, mIsNetworkError, mIsOver)
            holder.initItemPadding()
        }
    }

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

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

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