package com.gh.gamecenter.qa.editor

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.common.constant.ItemViewType
import com.gh.common.util.*
import com.gh.gamecenter.R
import com.gh.gamecenter.common.viewholder.FooterViewHolder
import com.gh.gamecenter.common.baselist.ListAdapter
import com.gh.gamecenter.collection.CollectionCommunityArticleViewHolder
import com.gh.gamecenter.common.callback.ConfirmListener
import com.gh.gamecenter.common.utils.goneIf
import com.gh.gamecenter.common.utils.safelyGetInRelease
import com.gh.gamecenter.common.utils.ImageUtils
import com.gh.gamecenter.core.utils.MtaHelper
import com.gh.gamecenter.databinding.CollectionComunityArticleItemBinding
import com.gh.gamecenter.feature.entity.ArticleEntity

class ArticleAdapter(context: Context, private val mEntrance: String?) : ListAdapter<ArticleEntity>(context) {

    override fun getItemViewType(position: Int): Int {
        return if (position == itemCount - 1) ItemViewType.ITEM_FOOTER else 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)
            }
            ItemViewType.ITEM_BODY -> {
                view = mLayoutInflater.inflate(R.layout.collection_comunity_article_item, parent, false)
                CollectionCommunityArticleViewHolder(CollectionComunityArticleItemBinding.bind(view))
            }
            else -> throw NullPointerException()
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (getItemViewType(position)) {
            ItemViewType.ITEM_BODY -> {
                val articleHolder = holder as CollectionCommunityArticleViewHolder
                val entity = mEntityList[position]
                articleHolder.binding.title.text = entity.title
                ImageUtils.display(articleHolder.binding.userIcon, entity.user.icon)
                articleHolder.binding.userBadge.goneIf(entity.user.auth == null)
                ImageUtils.display(articleHolder.binding.userBadge, entity.user.auth?.icon)
                articleHolder.binding.userName.text = entity.user.name
                articleHolder.binding.content.text = entity.brief
                articleHolder.binding.image.goneIf(
                    entity.images.isNullOrEmpty() && entity.getPassVideos().isNullOrEmpty()
                )
                articleHolder.binding.videoPlay.goneIf(entity.getPassVideos().isNullOrEmpty())
                articleHolder.binding.voteCount.text =
                    "${entity.count.comment} 评论 · ${entity.count.vote} 点赞 · ${NewsUtils.getFormattedTime(entity.time?.create ?: 0)}"
                articleHolder.binding.communityName.text = entity.community.name
                if (entity.images.isNotEmpty()) {
                    ImageUtils.display(articleHolder.binding.image, entity.images.safelyGetInRelease(0))
                } else if (entity.getPassVideos().isNotEmpty()) {
                    ImageUtils.display(
                        articleHolder.binding.image,
                        entity.getPassVideos().safelyGetInRelease(0)?.poster
                    )
                }

                if (entity.user.badge != null) {
                    articleHolder.binding.sdvUserBadge.visibility = View.VISIBLE
                    articleHolder.binding.tvBadgeName.visibility = View.VISIBLE
                    ImageUtils.display(articleHolder.binding.sdvUserBadge, entity.user.badge!!.icon)
                    articleHolder.binding.tvBadgeName.text = entity.user.badge!!.name
                } else {
                    articleHolder.binding.sdvUserBadge.visibility = View.GONE
                    articleHolder.binding.tvBadgeName.visibility = View.GONE
                }
                articleHolder.binding.sdvUserBadge.setOnClickListener { v ->
                    DialogUtils.showViewBadgeDialog(mContext, entity.user.badge, object : ConfirmListener {
                        override fun onConfirm() {
                            MtaHelper.onEvent(
                                "进入徽章墙_用户记录",
                                "插入文章-收藏文章列表",
                                entity.user.name + "（" + entity.user.id + "）"
                            )
                            MtaHelper.onEvent("徽章中心", "进入徽章中心", "插入文章-收藏文章列表")
                            DirectUtils.directToBadgeWall(mContext, entity.user.id, entity.user.name, entity.user.icon)
                        }
                    })
                }
                articleHolder.binding.tvBadgeName.setOnClickListener { v -> articleHolder.binding.sdvUserBadge.performClick() }

                articleHolder.itemView.setOnClickListener {
                    if (mContext is Activity) {
                        val intent = Intent()
                        intent.putExtra(ArticleEntity::class.java.simpleName, entity)
                        (mContext as Activity).setResult(Activity.RESULT_OK, intent)
                        (mContext as Activity).finish()
                    }
                }
            }
            ItemViewType.ITEM_FOOTER -> {
                val footerViewHolder = holder as FooterViewHolder
                footerViewHolder.initItemPadding()
                footerViewHolder.initFooterViewHolder(mIsLoading, mIsNetworkError, mIsOver)
            }
        }
    }

    override fun getItemCount(): Int {
        return if (mEntityList == null || mEntityList.isEmpty()) 0 else mEntityList.size + ListAdapter.FOOTER_ITEM_COUNT
    }
}
