package com.gh.gamecenter.home.custom.adapter

import android.content.Context
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.R
import com.gh.gamecenter.common.utils.goneIf
import com.gh.gamecenter.common.utils.toBinding
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.common.utils.toDrawable
import com.gh.gamecenter.databinding.RecyclerContentLabelLaneItemBinding
import com.gh.gamecenter.feature.exposure.ExposureEvent
import com.gh.gamecenter.feature.exposure.IExposureProvider
import com.gh.gamecenter.home.custom.model.CustomPageData

class ContentLabelLaneAdapter(
    context: Context,
    private val clickInvoke: (Int, CustomPageData.CommonContentCollection.ContentTag) -> Unit,
) : CustomBaseChildAdapter<CustomPageData.CommonContentCollection.ContentTag, ContentLabelLaneAdapter.ContentLabelChildViewHolder>(
    context
) {

    override fun getKey(t: CustomPageData.CommonContentCollection.ContentTag): String {
        return t.id
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ContentLabelChildViewHolder {
        return ContentLabelChildViewHolder(parent.toBinding())
    }

    override fun onBindViewHolder(holder: ContentLabelChildViewHolder, position: Int) {
        val item = getItem(position)

        holder.exposureEvent = item.exposureEvent

        with(holder.binding) {
            vBackground.background = R.drawable.bg_shape_content_label_lane_item.toDrawable(context)
            tvTitle.setTextColor(com.gh.gamecenter.common.R.color.text_primary.toColor(context))
            tvSubTitle.setTextColor(com.gh.gamecenter.common.R.color.primary_theme.toColor(context))

            ivIcon.goneIf(item.image.isBlank()){
                ivIcon.displayGameIcon(item.image, null, null)
            }

            tvTitle.text = item.title
            tvSubTitle.goneIf(item.addedContent.isBlank()) {
                tvSubTitle.text = context.getString(R.string.content_tag_added_content_with_prefix, item.addedContent)
            }
            gBubble.goneIf(!item.isShowGuide) {
                tvBubble.text = item.guide.text
            }
        }

        holder.itemView.setOnClickListener {
            clickInvoke(position, item)
        }
    }

    class ContentLabelChildViewHolder(
        val binding: RecyclerContentLabelLaneItemBinding
    ) : RecyclerView.ViewHolder(binding.root), IExposureProvider {
        var exposureEvent : ExposureEvent? = null

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

}