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

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

class NotificationColumnAdapter(context: Context, ) :
    CustomBaseChildAdapter<CustomPageData.Notify, NotificationColumnAdapter.NotificationColumChildViewHolder>(context) {

    private var toBeDeletedItemIds = arrayListOf<String>()

    override fun getKey(t: CustomPageData.Notify): String {
        return "${t.id}-${t.title}"
    }

    val dataCount: Int
        get() = dataList.size

    private val dataCountWithoutToBeDeleted: Int
        get() = dataCount - toBeDeletedItemIds.size

    val isBanner: Boolean
        get() = dataCount > 1

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

    override fun getItemCount(): Int {
        return if (dataCount > 1) {
            Int.MAX_VALUE
        } else {
            dataCount
        }
    }

    fun getDataPosition(position: Int): Int {
        return if (isBanner) position % dataCount else position
    }

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

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

        holder.exposureEvent = item.exposureEvent
        with(holder.binding) {
            ivIcon.displayGameIcon(item.image, null, goneIfEmpty = true, null)
            tvTitle.text = item.title
            tvDesc.text = item.addedContent
            ivClose.visibleIf(item.isClosable)
        }
    }

    private fun getValidItem(position: Int): CustomPageData.Notify {
        val item = getItem(position)
        val hasRemoved = toBeDeletedItemIds.remove(item.id)
        return if (hasRemoved) {
            _dataList.remove(item)
            getValidItem(position)
        } else {
            item
        }
    }

    /**
     *   为了不影响当前循环的数据位置，这里暂时将需要删除的数据标记为 待删除 状态，直接下一轮循环到需要删除的数据时，真正执行删除操作
     */
    fun removeItem(itemId: String) {
        val hasContains = _dataList.any { it.id == itemId }
        if (hasContains) {
            toBeDeletedItemIds.add(itemId)
            if (dataCountWithoutToBeDeleted == 1) {
                // 退化为普通列表
                _dataList.removeAll {
                    toBeDeletedItemIds.contains(it.id)
                }
                toBeDeletedItemIds.clear()
            }
        }
    }

    class NotificationColumChildViewHolder(
        val binding: RecyclerNotificationColumnItemBinding
    ) : RecyclerView.ViewHolder(binding.root), IExposureProvider {

        var exposureEvent: ExposureEvent? = null

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

    }
}