package com.gh.gamecenter.home.custom.ui

import android.content.Context
import androidx.recyclerview.widget.GridLayoutManager
import com.gh.gamecenter.common.entity.LinkEntity
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.view.GridSpacingItemDecoration
import com.gh.gamecenter.databinding.RecyclerNavigationCustomBinding
import com.gh.gamecenter.entity.GameNavigationEntity
import com.gh.gamecenter.feature.exposure.ExposureEvent
import com.gh.gamecenter.home.custom.adapter.CustomGameNavigationAdapter

class CommonContentNavigationUi(
    val binding: RecyclerNavigationCustomBinding,
    private val listener: OnNavigationListener
) {

    private val context: Context
        get() = binding.root.context

    private val layoutManager by lazy {
        GridLayoutManager(context, 4)
    }

    private val adapter by lazy {
        CustomGameNavigationAdapter(context, object : CustomGameNavigationAdapter.OnEventListener {
            override fun navigateToLinkPage(link: LinkEntity, text: String, exposureEvent: ExposureEvent?) {
                listener.navigateToLinkPage(link, text, exposureEvent)
            }
        })
    }

    fun bind(navigationList: List<GameNavigationEntity>?, trackEventList: List<ExposureEvent>?) {
        if (binding.rvNavigation.adapter == null) {
            binding.rvNavigation.layoutManager = layoutManager
            binding.rvNavigation.addItemDecoration(GridSpacingItemDecoration(4, 8f.dip2px(), false, 16f.dip2px()))
            binding.rvNavigation.adapter = adapter
            binding.rvNavigation.isNestedScrollingEnabled = false
        }

        if (!navigationList.isNullOrEmpty()) {
            adapter.setData(navigationList, trackEventList)
        }
    }

    interface OnNavigationListener {
        fun navigateToLinkPage(link: LinkEntity, text: String, exposureEvent: ExposureEvent?)
    }
}