package com.gh.gamecenter.message.view.concern

import android.text.Html
import androidx.viewbinding.ViewBinding
import com.therouter.TheRouter
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
import com.gh.gamecenter.common.constant.RouteConsts
import com.gh.gamecenter.core.provider.IConcernArticleUtilsProvider
import com.gh.gamecenter.feature.entity.ConcernEntity
import com.gh.gamecenter.feature.provider.ICommentUtilsProvider
import com.gh.gamecenter.feature.view.GameIconView

class ConcernArticleViewHolder(
    private val binding: ViewBinding,
    private val provider: IConcernArticleUtilsProvider,
    private val listener: OnConcernContentEventListener
) : BaseRecyclerViewHolder<ConcernEntity>(binding.root) {

    fun bind(item: ConcernEntity, position: Int) {
        binding.root.setOnClickListener {
            listener.onArticleClick(position, item)
        }
        arrayOf(provider.getIvIcon(binding), provider.getTvGameName(binding)).forEach {
            it.setOnClickListener {
                listener.onGameCLick(position, item.game?.toGameEntity())
            }
        }
        provider.getTvComment(binding).setOnClickListener {
            listener.onComment(position, item)
        }
        provider.getTvShare(binding).setOnClickListener {
            listener.onShare(position, item)
        }

        provider.initArticleStyle(binding)

        val game = item.game
        if (game != null) {
            (provider.getIvIcon(binding) as? GameIconView)?.displayGameIcon(
                game.getIcon(),
                game.iconSubscript,
                game.iconFloat
            )
        } else {
            (provider.getIvIcon(binding) as? GameIconView)?.displayGameIcon(item.gameIcon, null, null)
        }
        provider.getTvGameName(binding).text = item.getGameName()

        val commentUtils = TheRouter.get(ICommentUtilsProvider::class.java)
        commentUtils?.setCommentTime(provider.getTvTime(binding), item.time)

        provider.getTvTitle(binding).text = item.title

        val tvDescription = provider.getTvDescription(binding)
        if (item.brief != null) {
            tvDescription.text = Html.fromHtml(item.brief)
            tvDescription.maxLines = 100
        } else {
            tvDescription.text = Html.fromHtml(item.content)
            tvDescription.maxLines = 5
        }

        provider.bindImgs(binding, item.img ?: listOf<String>())

    }

}
