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

import android.annotation.SuppressLint
import android.content.Context
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.common.utils.ImageUtils
import com.gh.gamecenter.common.utils.toBinding
import com.gh.gamecenter.databinding.GameCollectionBannerItemBinding
import com.gh.gamecenter.feature.exposure.ExposureEvent
import com.gh.gamecenter.feature.exposure.IExposureProvider
import com.gh.gamecenter.home.custom.model.CustomPageData

class AnnouncementBannerAdapter(
    context: Context,
    private val listener: OnChildEventListener
) :
    CustomBaseChildAdapter<CustomPageData.Announcement, AnnouncementBannerAdapter.AnnouncementBannerChildViewHolder>(
        context
    ) {

    @SuppressLint("NotifyDataSetChanged")
    override fun checkResetData(updates: List<CustomPageData.Announcement>?) {
        if (updates.isNullOrEmpty()) {
            _countAndKey = null
            notifyDataSetChanged()
            return
        }

        val countAndKey = _countAndKey ?: Pair(0, "")
        val (oldSize, oldKeys) = countAndKey

        val newSize = updates.size
        var newKeys = ""
        updates.forEach {
            newKeys += it.id
        }

        val needRefresh = !(oldSize == newSize && oldKeys == newKeys && !isDarkModeChange())
        if (needRefresh) {
            if (isBanner) {
                // 刷新 [前 中 后] 三个 itemView
                val currentPosition = listener.getCurrentPosition()
                val startPosition = if (currentPosition > 0) currentPosition - 1 else currentPosition
                notifyItemRangeChanged(startPosition, 3)
            } else {
                notifyDataSetChanged()
            }
        }
    }

    val isBanner: Boolean
        get() = dataList.size > 1

    override fun getItemCount(): Int {
        return if (isBanner) Int.MAX_VALUE else super.getItemCount()
    }

    fun getDataPosition(position: Int) =
        if (isBanner) {
            position % dataList.size
        } else {
            position
        }

    override fun getItem(position: Int): CustomPageData.Announcement {
        return super.getItem(getDataPosition(position))
    }

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

    override fun onBindViewHolder(holder: AnnouncementBannerChildViewHolder, position: Int) {
        val item = getItem(position)
        holder.bind(item)
        holder.binding.bannerIv.setOnClickListener {
            listener.onItemClick(getDataPosition(position), item)
        }
    }

    class AnnouncementBannerChildViewHolder(val binding: GameCollectionBannerItemBinding) :
        RecyclerView.ViewHolder(binding.root), IExposureProvider {

        var exposureEvent: ExposureEvent? = null

        fun bind(item: CustomPageData.Announcement) {
            exposureEvent = item.exposureEvent
            ImageUtils.display(binding.bannerIv, item.image)
        }

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

    interface OnChildEventListener {
        fun onItemClick(childPosition: Int, announcement: CustomPageData.Announcement)

        fun getCurrentPosition(): Int
    }
}