package com.gh.gamecenter.gamedetail.dialog

import android.content.Context
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.TextPaint
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.gh.common.util.DirectUtils
import com.gh.common.util.NewFlatLogUtils
import com.gh.gamecenter.common.base.GlobalActivityManager.getCurrentPageEntity
import com.gh.gamecenter.common.base.GlobalActivityManager.getLastPageEntity
import com.gh.gamecenter.common.utils.SensorsBridge
import com.gh.gamecenter.common.utils.toBinding
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.common.viewholder.FooterViewHolder
import com.gh.gamecenter.core.utils.TimeUtils
import com.gh.gamecenter.databinding.ItemGameDetailBigEventBinding
import com.gh.gamecenter.feature.entity.BigEvent
import com.gh.gamecenter.feature.entity.GameEntity
import com.lightgame.adapter.BaseRecyclerAdapter

class GameBigEventAdapter(
    context: Context,
    private val gameEntity: GameEntity?,
    private val bigEvents: List<BigEvent>
) : BaseRecyclerAdapter<RecyclerView.ViewHolder>(context) {
    override fun getItemViewType(position: Int): Int {
        return if (position == itemCount - 1) {
            ITEM_FOOTER
        } else {
            ITEM_CONTENT
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return if (viewType == ITEM_CONTENT) {
            GameBigEventViewHolder(parent.toBinding())
        } else {
            FooterViewHolder(mLayoutInflater.inflate(com.gh.gamecenter.common.R.layout.refresh_footerview, parent, false))
        }
    }

    override fun getItemCount(): Int = if (bigEvents.isNotEmpty()) bigEvents.size + 1 else 0

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is GameBigEventViewHolder) {
            val bigEvent = bigEvents.getOrNull(position)
            holder.binding.timeTv.text = TimeUtils.getFormatTime(bigEvent?.time ?: 0L)
            if (bigEvent?.link == null) {
                holder.binding.contentTv.text = bigEvent?.content
            } else {
                val link = bigEvent.link!!
                val content = "${bigEvent.content} ${bigEvent.link?.value}"
                val builder = SpannableStringBuilder(content)
                builder.setSpan(object : ClickableSpan() {
                    override fun updateDrawState(ds: TextPaint) {
                        ds.color = com.gh.gamecenter.common.R.color.text_theme.toColor(mContext)
                        ds.isUnderlineText = true
                    }

                    override fun onClick(widget: View) {
                        DirectUtils.directToLinkPage(mContext, link, "游戏详情页", "游戏大事件弹窗", "游戏详情页-大事件")
                        NewFlatLogUtils.logGameDetailMajorEventDetailClick(
                            gameEntity?.name ?: "",
                            gameEntity?.id ?: "",
                            link.link ?: "",
                            link.type ?: "",
                            link.text ?: ""
                        )
                        SensorsBridge.trackGameDetailPageMajorEventClick(
                            gameId = gameEntity?.id ?: "",
                            gameName = gameEntity?.name ?: "",
                            pageName = getCurrentPageEntity().pageName,
                            pageId = getCurrentPageEntity().pageId,
                            pageBusinessId = getCurrentPageEntity().pageBusinessId,
                            lastPageName = getLastPageEntity().pageName,
                            lastPageId = getLastPageEntity().pageId,
                            lastPageBusinessId = getLastPageEntity().pageBusinessId,
                            downloadStatus = gameEntity?.downloadStatusChinese ?: "",
                            gameType = gameEntity?.categoryChinese ?: "",
                            action = "跳转链接",
                            linkText = link.text ?: "",
                            linkType = link.type ?: "",
                            linkId = link.link ?: ""
                        )
                    }
                }, bigEvent.content.length + 1, content.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
                holder.binding.contentTv.movementMethod = LinkMovementMethod()
                holder.binding.contentTv.text = builder
            }
        }
        if (holder is FooterViewHolder) {
            holder.run {
                loading.visibility = View.GONE
                itemView.isClickable = false
                hint.setText(com.gh.gamecenter.common.R.string.load_over_hint)
            }
        }
    }

    class GameBigEventViewHolder(var binding: ItemGameDetailBigEventBinding) :
        RecyclerView.ViewHolder(binding.root)

    companion object {
        private const val ITEM_CONTENT = 0
        private const val ITEM_FOOTER = 1
    }
}