package com.gh.gamecenter.home.amway

import android.content.Context
import android.view.View
import com.gh.common.databind.BindingAdapters
import com.gh.gamecenter.R
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.common.view.AsyncCell
import com.gh.gamecenter.databinding.HomeAmwayItemBinding
import com.gh.gamecenter.entity.AmwayCommentEntity
import com.gh.gamecenter.feature.game.GameItemViewHolder
import com.gh.gamecenter.gamedetail.rating.edit.RatingEditActivity
import java.util.regex.Pattern

class HomeAmwayItemViewHolder(val binding: HomeAmwayItemBinding) : BaseRecyclerViewHolder<Any>(binding.root) {

    fun bindAmway(amway: AmwayCommentEntity) {
        val gameEntity = amway.game.toGameEntity()

        binding.gameIcon.displayGameIcon(gameEntity)
        binding.gameName.text = amway.game.name
        binding.amwayScoreNumber.text = amway.game.star.toString()
        BindingAdapters.setGameTags(binding.gameTags, amway.game.tag, 2)

        ImageUtils.display(binding.userIcon, amway.comment.user.icon)
        ImageUtils.display(binding.userBadge, amway.comment.user.auth?.icon)
        binding.userName.text = amway.comment.user.name
        binding.ratingStart.rating = amway.comment.star.toFloat()

        // 强制更改标签颜色
        val tags = amway.game.tag
        if (!tags.isNullOrEmpty()) {
            for (tag in tags) tag.color = "999999"
        }

        val m = Pattern.compile(RatingEditActivity.LABEL_REGEX).matcher(amway.comment.content)
        if (m.find()) {
            val contents = TextHelper.getCommentLabelSpannableStringBuilder(amway.comment.content, R.color.theme_font)
            binding.content.text = contents
        } else {
            binding.content.text = amway.comment.content
        }
        binding.run {
            amwayBackground.background = R.drawable.home_amway_selector.toDrawable(root.context)
            ratingBlock.background = R.drawable.home_amway_rating_selector.toDrawable(root.context)
            all.background = R.drawable.home_amway_selector.toDrawable(root.context)
            gameName.setTextColor(R.color.text_title.toColor(root.context))
            content.setTextColor(R.color.text_title.toColor(root.context))
            userName.setTextColor(R.color.text_subtitle.toColor(root.context))
            ratingName.setTextColor(R.color.text_subtitleDesc.toColor(root.context))
            all.setTextColor(R.color.text_title.toColor(root.context))
        }
        GameItemViewHolder.initGameSubtitleAndAdLabel(gameEntity, binding.gameSubtitleTv)
    }
}

class HomeAmwayAsyncCell(context: Context) : AsyncCell(context) {
    var binding: HomeAmwayItemBinding? = null

    override val layoutId = R.layout.home_amway_item
    override fun createDataBindingView(view: View): View? {
        binding = HomeAmwayItemBinding.bind(view)
        return view.rootView
    }
}