package com.gh.gamecenter.search

import android.content.Context
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.vectordrawable.graphics.drawable.VectorDrawableCompat
import com.gh.common.exposure.ExposureEvent
import com.gh.common.exposure.ExposureSource
import com.gh.common.util.DirectUtils
import com.gh.gamecenter.R
import com.gh.gamecenter.common.utils.setDrawableEnd
import com.gh.gamecenter.common.utils.goneIf
import com.gh.gamecenter.common.utils.setDrawableEnd
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.common.utils.toDrawable
import com.gh.gamecenter.common.view.DrawableView
import com.gh.gamecenter.databinding.SearchGameIndexItemBinding
import com.gh.gamecenter.databinding.SearchSubjectItemBinding
import com.gh.gamecenter.db.SearchHistoryDao
import com.gh.gamecenter.entity.GameEntity

class SearchGameIndexItemViewHolder(var binding: SearchGameIndexItemBinding) : RecyclerView.ViewHolder(binding.root) {
    fun initServerType(gameEntity: GameEntity) {
        val serverLabel = gameEntity.serverLabel
        when {
            gameEntity.test != null -> {
                binding.gameItemIncluded.gameKaifuType.visibility = View.GONE
                binding.gameItemIncluded.gameKaifuType.text = ""
            }
            serverLabel != null && !gameEntity.advanceDownload -> {
                binding.gameItemIncluded.gameKaifuType.visibility = View.VISIBLE
                binding.gameItemIncluded.gameKaifuType.text = serverLabel.value
                if (gameEntity.isUseDefaultServerStyle()) {
                    binding.gameItemIncluded.gameKaifuType.background =
                        R.drawable.server_label_default_bg.toDrawable(binding.root.context)
                    binding.gameItemIncluded.gameKaifuType.setTextColor(R.color.text_server_label.toColor(binding.root.context))
                } else {
                    binding.gameItemIncluded.gameKaifuType.background =
                        DrawableView.getServerDrawable(serverLabel.color)
                    binding.gameItemIncluded.gameKaifuType.setTextColor(R.color.white.toColor(binding.root.context))
                }
            }
            else -> binding.gameItemIncluded.gameKaifuType.visibility = View.GONE
        }

        // 由于RecyclerView的复用机制 需要每次测量gameName的宽
        binding.gameItemIncluded.gameName.requestLayout()
    }
}

class SearchSubjectItemViewHolder(var binding: SearchSubjectItemBinding) : RecyclerView.ViewHolder(binding.root) {
    fun bindSubjectItem(
        context: Context,
        itemData: SearchItemData,
        type: String,
        key: String,
        dao: SearchHistoryDao? = null
    ) {
        val entity = itemData.subject ?: return

        val exposureList = arrayListOf<ExposureEvent>()
        for ((index, game) in entity.games.withIndex()) {
            game.sequence = index
            game.outerSequence = adapterPosition
            game.isAdData = entity.adIconActive

            val exposureSources = arrayListOf<ExposureSource>().apply {
                add(ExposureSource("首页搜索"))
                add(ExposureSource(type, key))
                add(ExposureSource("专题", entity.name))
            }
            val exposureEvent = ExposureEvent.createEvent(game, exposureSources)
            exposureList.add(exposureEvent)

            game.exposureEvent = exposureEvent
        }
        itemData.exposureEventList = exposureList

        binding.run {
            subjectRv.layoutManager = LinearLayoutManager(context, RecyclerView.HORIZONTAL, false)
            subjectRv.adapter = SearchSubjectAdapter(context, entity.games, "($type-专题)") {
                dao?.add(key)
            }
            headContainer.adLabelTv.goneIf(!entity.adIconActive)
            headContainer.headTitle.text = entity.name
            headContainer.headActionTv.text = "全部"
            headContainer.headActionTv.setTextColor(R.color.theme_font.toColor(context))
            headContainer.headActionTv.setDrawableEnd(R.drawable.ic_home_head_arrow.toDrawable(context))
            headContainer.headActionTv.setOnClickListener {
                dao?.add(key)
                DirectUtils.directToSubject(context, entity.columnId, entity.name, "($type-专题)")
            }
        }
    }
}