package com.gh.gamecenter.gamedetail.detail.viewholder

import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.view.forEach
import androidx.core.view.isEmpty
import androidx.core.view.isVisible
import com.facebook.drawee.view.SimpleDraweeView
import com.gh.common.util.DirectUtils
import com.gh.gamecenter.R
import com.gh.gamecenter.common.constant.Constants
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.databinding.ItemGameDetailBriefAwardBinding
import com.gh.gamecenter.databinding.ItemGameDetailBriefBinding
import com.gh.gamecenter.databinding.ItemGameDetailBriefTagBinding
import com.gh.gamecenter.feature.view.DownloadButton
import com.gh.gamecenter.gamedetail.GameDetailViewModel
import com.gh.gamecenter.gamedetail.dialog.GameDetailScrollableTextDialogFragment
import com.gh.gamecenter.gamedetail.entity.GameBrief
import com.gh.gamecenter.gamedetail.entity.GameDetailData
import com.gh.gamecenter.tag.TagsActivity
import splitties.systemservices.layoutInflater

class GameDetailBriefItemViewHolder(
    val binding: ItemGameDetailBriefBinding,
    downloadBtn: DownloadButton?,
    viewModel: GameDetailViewModel
) : BaseGameDetailItemViewHolder(binding.root, downloadBtn, viewModel) {
    override fun bindView(data: GameDetailData) {
        super.bindView(data)
        val entity = data.linkGameBrief ?: return
        binding.run {
            titleTv.setTextColor(com.gh.gamecenter.common.R.color.text_primary.toColor(context))
            briefTv.setTextColor(com.gh.gamecenter.common.R.color.text_secondary.toColor(context))
            divider.setBackgroundColor(com.gh.gamecenter.common.R.color.ui_divider.toColor(context))
            expandTv.background = R.drawable.bg_ui_surface_expand_gradient.toDrawable(context)
            expandTv.setTextColor(com.gh.gamecenter.common.R.color.text_theme.toColor(context))
            titleTv.text = entity.name
            tagScrollView.goneIf(entity.gameTags.isNullOrEmpty()) {
                bindGamesTags(entity.gameTags)
            }
            briefTv.text = TextHelper.getHighlightedSpannableStringThatIsWrappedInsideWrapper(
                context,
                entity.des,
                Constants.DEFAULT_TEXT_WRAPPER_2,
                highlightedTextClickListener = TextHelper.CopyToClipboardHighlightedTextClick()
            )
            briefTv.post {
                expandTv.isVisible = briefTv.lineCount == 3 && (briefTv.layout?.getEllipsisCount(2) ?: 0) > 0
            }
            expandTv.setOnClickListener {
                SensorsBridge.trackGameDetailModuleClick(
                    gameId,
                    gameName,
                    gameType,
                    "组件内容",
                    moduleType,
                    entity.name,
                    sequence,
                    "简介文案",
                    gameStatus = gameStatus
                )
                GameDetailScrollableTextDialogFragment.show(
                    context,
                    "游戏信息",
                    entity.des
                )
            }
            divider.goneIf(entity.awardData.isEmpty())
            awardScrollView.goneIf(entity.awardData.isEmpty()) {
                bindAwards(entity.awardData)
            }
        }
    }

    private fun bindGamesTags(gameTags: List<GameBrief.GameTag>) {
        if (binding.tagContainer.isEmpty()) {
            gameTags.forEachIndexed { index, gameTag ->
                val tagBinding = ItemGameDetailBriefTagBinding.inflate(context.layoutInflater)
                tagBinding.root.text = gameTag.name
                tagBinding.root.setOnClickListener {
                    SensorsBridge.trackGameDetailModuleClick(
                        gameId,
                        gameName,
                        gameType,
                        "组件内容",
                        moduleType,
                        itemData?.linkGameBrief?.name,
                        sequence,
                        "游戏标签",
                        index + 1,
                        linkText = gameTag.name,
                        gameStatus = gameStatus
                    )
                    context.startActivity(
                        TagsActivity.getIntent(
                            context,
                            gameTag.name,
                            gameTag.name,
                            "游戏详情-游戏简介",
                            ""
                        )
                    )
                }
                binding.tagContainer.addView(
                    tagBinding.root,
                    LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, 24F.dip2px()).apply {
                        leftMargin = if (index == 0) 0 else 4F.dip2px()
                    })
            }
        } else {
            binding.tagContainer.forEach {
                (it as? TextView)?.run {
                    setTextColor(com.gh.gamecenter.common.R.color.text_tertiary.toColor(context))
                    background = com.gh.gamecenter.common.R.drawable.bg_shape_ui_container_1_stroke_radius_6.toDrawable(context)
                }
            }
        }
    }

    private fun bindAwards(awardList: List<GameBrief.AwardData>) {
        if (binding.awardContainer.isEmpty()) {
            awardList.forEachIndexed { index, awardData ->
                val awardBinding = ItemGameDetailBriefAwardBinding.inflate(context.layoutInflater)
                awardBinding.iconIv.display(awardData.icon)
                awardBinding.nameTv.text = awardData.award
                awardBinding.typeTv.text = awardData.awardType
                awardBinding.root.setOnClickListener {
                    awardData.link?.let { link ->
                        DirectUtils.directToLinkPage(context, link, "游戏详情-游戏简介", "")
                        SensorsBridge.trackGameDetailModuleClick(
                            gameId,
                            gameName,
                            gameType,
                            "组件内容",
                            moduleType,
                            itemData?.linkGameBrief?.name,
                            sequence,
                            awardData.award,
                            index + 1,
                            link.type,
                            link.link,
                            link.text,
                            gameStatus
                        )
                    }
                }
                binding.awardContainer.addView(
                    awardBinding.root,
                    LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, 26F.dip2px()).apply {
                        leftMargin = if (index == 0) 0 else 24F.dip2px()
                    })
            }
        } else {
            binding.awardContainer.forEach {
                it.findViewById<SimpleDraweeView>(R.id.iconIv)?.updateRoundingParams {
                    setOverlayColor(com.gh.gamecenter.common.R.color.ui_surface.toColor(context))
                }
                it.findViewById<TextView>(R.id.nameTv)?.setTextColor(com.gh.gamecenter.common.R.color.text_tertiary.toColor(context))
                it.findViewById<TextView>(R.id.typeTv)?.setTextColor(com.gh.gamecenter.common.R.color.text_secondary.toColor(context))
            }
        }
    }
}