package com.gh.gamecenter.gamedetail.myrating

import android.content.Context
import android.util.SparseBooleanArray
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.common.utils.copyTextAndToast
import com.gh.gamecenter.common.utils.goneIf
import com.gh.gamecenter.common.utils.ifLogin
import com.gh.gamecenter.common.utils.setTextWithHighlightedTextWrappedInsideWrapper
import com.gh.gamecenter.common.utils.TextHelper
import com.gh.gamecenter.core.utils.*
import com.gh.gamecenter.databinding.ItemMyGameRatingBinding
import com.gh.gamecenter.feature.entity.TagStyleEntity
import com.gh.gamecenter.gamedetail.rating.RatingReplyActivity
import com.gh.gamecenter.gamedetail.rating.edit.RatingEditActivity
import com.gh.gamecenter.personalhome.rating.MyRating
import java.util.regex.Pattern

class MyRatingAdapter(
    context: Context,
    val mEntrance: String,
    val mListViewModel: MyRatingViewModel
) : ListAdapter<MyRating>(context) {

    private var mExpandSparseBooleanArray = SparseBooleanArray()

    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.item_my_game_rating, parent, false)
                MyRatingViewHolder(ItemMyGameRatingBinding.bind(view))
            }
            else -> throw NullPointerException()
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (holder) {
            is MyRatingViewHolder -> {
                var isChildLongClick = false
                val rating = mEntityList[position]
                holder.binding.gameName.text = rating.game.name
                holder.binding.scoreTv.text = if (rating.game.star > 0) rating.game.star.toString() else "评分过少"
                holder.binding.scoreTv.textSize = if (rating.game.star > 0) 18f else 8f
                holder.binding.ratingBar.rating = rating.star.toFloat()
                holder.binding.comment.text = if (rating.reply > 0) NumberUtils.transSimpleCount(rating.reply) else ""
                holder.binding.vote.isChecked = rating.me.isVoted
                holder.binding.vote.text = if (rating.vote > 0) NumberUtils.transSimpleCount(rating.vote) else ""
                val maxDesLines = if (mExpandSparseBooleanArray.get(holder.adapterPosition)) Int.MAX_VALUE else 3
                holder.binding.tvComment.setExpandMaxLines(maxDesLines)
                holder.binding.tvComment.setIsExpanded(Int.MAX_VALUE == maxDesLines)

                holder.binding.gameIcon.displayGameIcon(
                    rating.game.getRawIconIfExisted(),
                    rating.game.iconSubscript,
                    rating.game.iconFloat
                )

                val m = Pattern.compile(RatingEditActivity.LABEL_REGEX).matcher(rating.content)
                if (m.find()) {
                    val contents = TextHelper.getCommentLabelSpannableStringBuilder(rating.content, R.color.theme_font)
                    holder.binding.tvComment.setTextWithHighlightedTextWrappedInsideWrapper(
                        text = contents,
                        copyClickedText = true
                    )
                } else {
                    holder.binding.tvComment.setTextWithHighlightedTextWrappedInsideWrapper(
                        text = rating.content,
                        copyClickedText = true
                    )
                }
                holder.binding.tvComment.setExpandCallback {
                    mExpandSparseBooleanArray.put(holder.adapterPosition, true)
                }
                GameViewUtils.setLabelList(mContext, holder.binding.labelList, rating.game.tag as List<TagStyleEntity>)
                holder.binding.labelList.goneIf(holder.binding.labelList.childCount == 0)

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

                holder.binding.apply {
                    gameInfo.setOnClickListener {
                        MtaHelper.onEvent("我的光环_新", "我的游戏评论", "游戏详情")
                        DirectUtils.directToGameDetail(mContext, rating.game.id, "我的游戏评论")
                    }
                    commentInfo.setOnClickListener {
                        if (isChildLongClick) {
                            isChildLongClick = false
                            return@setOnClickListener
                        }
                        MtaHelper.onEvent("我的光环_新", "我的游戏评论", "评论详情")
                        val intent =
                            RatingReplyActivity.getIntent(mContext, rating.game.id, rating.id, false, "我的游戏评论", "")
                        /*  if(!rating.active){
                              intent = RatingReplyActivity.getIntent(mContext, rating.transformGameEntity(), rating.transformCommentEntity(), "我的游戏评论", "")
                          }else if(!rating.game.active){
                              intent = RatingReplyActivity.getIntent(mContext, rating.game.id, rating.id, "我的游戏评论", "")
                          }else if(!rating.active&&!rating.game.active){
                              intent = RatingReplyActivity.getIntent(mContext, rating.transformGameEntity(), rating.transformCommentEntity(), "我的游戏评论", "")
                          }*/

                        SyncDataBetweenPageHelper.startActivityForResult(mContext, intent, 100, position)
                    }
                }
                holder.binding.tvComment.setOnLongClickListener(View.OnLongClickListener {
                    isChildLongClick = true
                    rating.content.replace(RatingEditActivity.LABEL_REPLACE_REGEX.toRegex(), "").copyTextAndToast()
                    return@OnLongClickListener true
                })
            }
            is FooterViewHolder -> {
                holder.initItemPadding()
                holder.initFooterViewHolder(mIsLoading, mIsNetworkError, mIsOver)
            }
        }
    }

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

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