package com.gh.gamecenter.gamedetail.rating.logs

import android.content.Context
import android.graphics.Color
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.style.BackgroundColorSpan
import android.text.style.ForegroundColorSpan
import android.text.style.StrikethroughSpan
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
import com.gh.gamecenter.common.constant.ItemViewType
import com.gh.gamecenter.common.utils.ImageUtils
import com.gh.common.util.NewsUtils
import com.gh.gamecenter.R
import com.gh.gamecenter.common.viewholder.FooterViewHolder
import com.gh.gamecenter.common.baselist.ListAdapter
import com.gh.gamecenter.common.baselist.NormalListViewModel
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.databinding.GameCommentLogItemBinding
import com.gh.gamecenter.entity.RatingComment


class CommentLogsAdapter(
    context: Context,
    val viewModel: NormalListViewModel<RatingComment>
) : ListAdapter<RatingComment>(context) {
    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.game_comment_log_item, parent, false)
                CommentLogItemViewHolder(GameCommentLogItemBinding.bind(view))
            }
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (holder) {
            is CommentLogItemViewHolder -> {
                val entity = mEntityList[position]
                ImageUtils.display(holder.binding.userIcon, entity.user.icon)
                ImageUtils.display(holder.binding.userBadge, entity.user.auth?.icon)
                holder.binding.commentItem.setBackgroundColor(R.color.background_white.toColor(mContext))
                holder.binding.userName.text = entity.user.name
                holder.binding.userName.setTextColor(R.color.text_black.toColor(mContext))
                holder.binding.ratingStart.rating = entity.star.toFloat()
                holder.binding.time.text = NewsUtils.getFormattedTime(entity.time)

                holder.binding.content.setTextColor(R.color.text_title.toColor(mContext))
                holder.binding.content.text = getContentSpan(entity.content)
            }
            is FooterViewHolder -> holder.initFooterViewHolder(viewModel, mIsLoading, isNetworkError, mIsOver)
        }
    }

    // <background>: 蓝底白字, <strike>:灰色+删除线
    private fun getContentSpan(content: String): SpannableStringBuilder {
        var text = content
        val list = ArrayList<SpanType>()
        var index = 0
        do {
            index++
            val sIndex = text.indexOf("<strike>")
            val bIndex = text.indexOf("<background>")
            if (bIndex == -1 || (sIndex != -1 && sIndex < bIndex)) {
                val split1 = text.split("<strike>".toRegex(), 2).toTypedArray()
                if (split1.size == 2) {
                    val splitText = split1[1]
                    val split2 = splitText.split("</strike>".toRegex(), 2).toTypedArray()
                    if (split2.size == 2) {
                        text = split2[1]
                        val one = split1[0]
                        val two = split2[0]
                        if (one.isNotEmpty()) {
                            list.add(SpanType(0, one))
                        }
                        if (two.isNotEmpty()) {
                            list.add(SpanType(1, two))
                        }
                    }
                }
            } else {
                val split1 = text.split("<background>".toRegex(), 2).toTypedArray()
                if (split1.size == 2) {
                    val splitText = split1[1]
                    val split2 = splitText.split("</background>".toRegex(), 2).toTypedArray()
                    if (split2.size == 2) {
                        text = split2[1]
                        val one = split1[0]
                        val two = split2[0]
                        if (one.isNotEmpty()) {
                            list.add(SpanType(0, one))
                        }
                        if (two.isNotEmpty()) {
                            list.add(SpanType(2, two))
                        }
                    }
                }
            }
        } while ((text.contains("<strike>") && text.contains("</strike>") ||
                    text.contains("<background>") && text.contains("</background>"))
            && index < 50
        )

        if (text.isNotEmpty()) list.add(SpanType(0, text))

        val msp = SpannableStringBuilder()
        if (list.isNotEmpty()) {
            for (spanType in list) {
                val value = spanType.content
                msp.append(value)
                if (spanType.type == 1) {
                    msp.setSpan(
                        ForegroundColorSpan(R.color.hint.toColor(mContext)),
                        (msp.length - value.length),
                        msp.length,
                        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
                    )
                    msp.setSpan(
                        StrikethroughSpan(),
                        (msp.length - value.length),
                        msp.length,
                        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
                    )
                } else if (spanType.type == 2) {
                    msp.setSpan(
                        ForegroundColorSpan(R.color.text_white.toColor(mContext)),
                        (msp.length - value.length),
                        msp.length,
                        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
                    )
                    msp.setSpan(
                        BackgroundColorSpan(R.color.theme_font.toColor(mContext)),
                        (msp.length - value.length),
                        msp.length,
                        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
                    )
                }
            }
        } else {
            msp.append(content)
        }
        return msp
    }

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

    class SpanType(val type: Int, val content: String)

    inner class CommentLogItemViewHolder(val binding: GameCommentLogItemBinding) :
        BaseRecyclerViewHolder<Any>(binding.root)
}