package com.gh.gamecenter.home.custom.viewholder

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.databinding.HomeAmwayItemCustomBinding
import com.gh.gamecenter.entity.AmwayCommentEntity
import com.gh.gamecenter.feature.exposure.ExposureEvent
import com.gh.gamecenter.feature.exposure.IExposureProvider
import com.gh.gamecenter.feature.game.GameItemViewHolder
import com.gh.gamecenter.gamedetail.rating.edit.RatingEditActivity
import java.util.regex.Pattern

class CustomHomeAmwayItemViewHolder(val binding: HomeAmwayItemCustomBinding) :
    BaseRecyclerViewHolder<Any>(binding.root), IExposureProvider {

    private var boundedGame: AmwayCommentEntity.Game? = null

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

        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,
                com.gh.gamecenter.common.R.color.text_theme
            )
            binding.content.setTextWithHighlightedTextWrappedInsideWrapper(
                contents,
                highlightedTextClickListener = TextHelper.DirectToWebViewHighlightedTextClick(
                    binding.root.context,
                    "自定义页-热门评论"
                )
            )
        } else {
            binding.content.setTextWithHighlightedTextWrappedInsideWrapper(
                amway.comment.content,
                highlightedTextClickListener = TextHelper.DirectToWebViewHighlightedTextClick(
                    binding.root.context,
                    "自定义页-热门评论"
                )
            )
        }
        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(com.gh.gamecenter.common.R.color.text_primary.toColor(root.context))
            content.setTextColor(com.gh.gamecenter.common.R.color.text_primary.toColor(root.context))
            userName.setTextColor(com.gh.gamecenter.common.R.color.text_secondary.toColor(root.context))
            ratingName.setTextColor(com.gh.gamecenter.common.R.color.text_tertiary.toColor(root.context))
            all.setTextColor(com.gh.gamecenter.common.R.color.text_primary.toColor(root.context))
        }
        GameItemViewHolder.initGameSubtitleAndAdLabel(gameEntity, binding.gameSubtitleTv)
    }

    override fun provideExposureData(): ExposureEvent? {
        return boundedGame?.exposureEvent?.getFreshExposureEvent()
    }
}

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
    }
}