package com.gh.gamecenter.personalhome.home

import android.app.Activity
import android.content.Context
import android.text.SpannableStringBuilder
import android.util.SparseBooleanArray
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.facebook.drawee.view.SimpleDraweeView
import com.gh.common.util.DialogUtils
import com.gh.common.util.DirectUtils
import com.gh.common.util.NewsUtils
import com.gh.common.view.ImageContainerView
import com.gh.common.view.ImageContainerView.Companion.toImageContainerData
import com.gh.gamecenter.ImageViewerActivity
import com.gh.gamecenter.R
import com.gh.gamecenter.adapter.viewholder.PersonalHomeRatingViewHolder
import com.gh.gamecenter.common.baselist.ListAdapter
import com.gh.gamecenter.common.callback.ConfirmListener
import com.gh.gamecenter.common.constant.ItemViewType
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.common.viewholder.FooterViewHolder
import com.gh.gamecenter.core.utils.NumberUtils
import com.gh.gamecenter.core.utils.SpanBuilder
import com.gh.gamecenter.databinding.CommunityAnswerItemBinding
import com.gh.gamecenter.databinding.PersonalHomeRatingBinding
import com.gh.gamecenter.entity.PersonalHistoryEntity
import com.gh.gamecenter.feature.entity.AnswerEntity
import com.gh.gamecenter.feature.entity.ForumVideoEntity
import com.gh.gamecenter.forum.home.AnswerArticleVideoViewEventHelper
import com.gh.gamecenter.forum.home.ArticleItemVideoView
import com.gh.gamecenter.forum.home.ArticleItemVideoView.Companion.toArticleVideoData
import com.gh.gamecenter.gamedetail.rating.edit.RatingEditActivity
import com.gh.gamecenter.personalhome.PersonalItemViewHolder
import com.gh.gamecenter.video.detail.VideoDetailContainerViewModel
import com.shuyu.gsyvideoplayer.builder.GSYVideoOptionBuilder
import com.shuyu.gsyvideoplayer.listener.GSYSampleCallBack
import com.shuyu.gsyvideoplayer.utils.OrientationUtils
import java.util.regex.Pattern

class UserHistoryAdapter(
    context: Context,
    private val mListViewModel: UserHistoryViewModel,
    private val mEntrance: String,
    private val itemClickCallback: (historyEntity: PersonalHistoryEntity, position: Int) -> Unit
) : ListAdapter<PersonalHistoryEntity>(context) {

    private var mExpandSparseBooleanArray = SparseBooleanArray()

    override fun getItemViewType(position: Int): Int {
        return when {
            position == itemCount - 1 -> ItemViewType.ITEM_FOOTER
            mEntityList[position].type == "game_comment" -> ItemViewType.RATING_ITEM
            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(com.gh.gamecenter.common.R.layout.refresh_footerview, parent, false)
                FooterViewHolder(view)
            }

            ItemViewType.RATING_ITEM -> {
                view = mLayoutInflater.inflate(R.layout.personal_home_rating, parent, false)
                PersonalHomeRatingViewHolder(PersonalHomeRatingBinding.bind(view))
            }

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

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (holder) {
            is PersonalItemViewHolder -> bindNormalItem(holder, position)
            is PersonalHomeRatingViewHolder -> bindRatingItem(holder, position)
            is FooterViewHolder -> {
                holder.initItemPadding()
                holder.initFooterViewHolder(mListViewModel, mIsLoading, mIsNetworkError, mIsOver)
            }
        }
    }

    private fun bindNormalItem(holder: PersonalItemViewHolder, position: Int) {
        val historyEntity = mEntityList[holder.adapterPosition]

        holder.bindPersonalItem(historyEntity, mEntrance, position)
    }

    private fun bindRatingItem(holder: PersonalHomeRatingViewHolder, position: Int) {
        val historyEntity = mEntityList[holder.adapterPosition]
        holder.binding.comment.text =
            if (historyEntity.count.reply > 0) NumberUtils.transSimpleCount(historyEntity.count.reply) else ""
        holder.binding.vote.isChecked = historyEntity.me.isVoted
        holder.binding.vote.text =
            if (historyEntity.count.vote > 0) NumberUtils.transSimpleCount(historyEntity.count.vote) else ""
        holder.binding.ratingStart.rating = historyEntity.comment.star.toFloat()

        holder.binding.userIcon.display(
            historyEntity.user?.border,
            historyEntity.user?.icon,
            historyEntity.user?.auth?.icon
        )
        val maxDesLines = if (mExpandSparseBooleanArray.get(holder.adapterPosition)) Int.MAX_VALUE else 3
        holder.binding.content.setExpandMaxLines(maxDesLines)
        holder.binding.content.setIsExpanded(Int.MAX_VALUE == maxDesLines)

        val m = Pattern.compile(RatingEditActivity.LABEL_REGEX).matcher(historyEntity.comment.content)
        if (m.find()) {
            val contents =
                TextHelper.getCommentLabelSpannableStringBuilder(historyEntity.comment.content, com.gh.gamecenter.common.R.color.text_theme)
            holder.binding.content.setTextWithHighlightedTextWrappedInsideWrapper(
                text = contents,
                highlightedTextClickListener = TextHelper.DirectToWebViewHighlightedTextClick(mContext, mEntrance)
            )
        } else {
            holder.binding.content.setTextWithHighlightedTextWrappedInsideWrapper(
                text = historyEntity.comment.content,
                highlightedTextClickListener = TextHelper.DirectToWebViewHighlightedTextClick(mContext, mEntrance)
            )
        }
        holder.binding.content.setExpandCallback {
            mExpandSparseBooleanArray.put(holder.adapterPosition, true)
        }

        holder.binding.root.setOnClickListener {
            itemClickCallback.invoke(historyEntity, position)
        }

        holder.binding.vote.setOnClickListener {
            mContext.ifLogin(mEntrance) {
                if (!holder.binding.vote.isChecked) {
                    mListViewModel.voteComment(historyEntity.comment.game.id, historyEntity.comment.id, callback = {
                        val count = historyEntity.count.vote + 1
                        holder.binding.vote.text = count.toString()
                        holder.binding.vote.isChecked = true
                        historyEntity.count.vote = count
                        historyEntity.me.isVoted = true
                    })
                } else {
                    mListViewModel.unVoteComment(historyEntity.comment.game.id, historyEntity.comment.id, callback = {
                        val count = historyEntity.count.vote - 1
                        holder.binding.vote.text = if (count == 0) "" else count.toString()
                        holder.binding.vote.isChecked = false
                        historyEntity.count.vote = count
                        historyEntity.me.isVoted = false
                    })
                }
            }
        }
    }

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