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

import android.content.Context
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.utils.goneIf
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.common.view.SpacingItemDecoration
import com.gh.gamecenter.databinding.CommonCollectionListCustomBinding
import com.gh.gamecenter.databinding.LayoutTitleCustomBinding
import com.gh.gamecenter.entity.CommonCollectionContentEntity
import com.gh.gamecenter.entity.ExposureLinkEntity
import com.gh.gamecenter.home.custom.adapter.CustomCommonCollectionAdapter
import com.gh.gamecenter.home.custom.model.CustomPageData

/**
 * ui-通用内容合集-横排滑动Banner
 * ui-通用内容合集-横排竖式卡片
 * ui-通用内容合集-横排图文列表
 */
class CommonContentHorizontalSlideListUi(
    val binding: CommonCollectionListCustomBinding,
    private val listener: OnHorizontalSlideListListener
) {

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

    private val layoutManager by lazy {
        LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
    }

    private val adapter by lazy {
        CustomCommonCollectionAdapter(context, object : CustomCommonCollectionAdapter.OnEventListener {
            override fun addExposureEvent(childPosition: Int, exposureLinkEntity: ExposureLinkEntity) {
                listener.addExposureEvent(childPosition, exposureLinkEntity)
            }

            override fun onChildItemClick(childPosition: Int, item: CommonCollectionContentEntity) {
                listener.onChildItemClick(childPosition, item)
            }
        })
    }

    fun bind(data: CustomPageData.CommonContentCollection) {
        binding.layoutTitle.tvTitle.setTextColor(com.gh.gamecenter.common.R.color.text_primary.toColor(context))
        setTitle(data, binding.layoutTitle) {
            listener.navigateToCommonCollectionDetailPage(data)
        }
        if (binding.collectionList.adapter == null) {
            binding.collectionList.addItemDecoration(
                SpacingItemDecoration(
                    notDecorateTheFirstItem = true,
                    left = 8f.dip2px()
                )
            )
            binding.collectionList.layoutManager = layoutManager
            binding.collectionList.addOnScrollListener(object : RecyclerView.OnScrollListener() {
                override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                    super.onScrollStateChanged(recyclerView, newState)
                    if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                        var position = layoutManager.findLastCompletelyVisibleItemPosition()
                        if (position == -1) position = layoutManager.findLastVisibleItemPosition() - 1
                        if (position < 0) return
                    }
                }
            })
            binding.collectionList.adapter = adapter
        }
        adapter.setData(data)
    }

    companion object {

        fun setTitle(
            data: CustomPageData.CommonContentCollection,
            layoutTitle: LayoutTitleCustomBinding,
            trackInvoke: () -> Unit
        ) {
            with(layoutTitle) {
                lavRefresh.goneIf(true)
                gUser.goneIf(true)
                tvTitle.text = data.name

                if (data.topRightText.isEmpty()) {
                    tvRight.goneIf(true)
                } else {
                    tvRight.goneIf(false)
                    tvRight.text = data.topRightText
                    tvRight.setOnClickListener {
                        trackInvoke()
                    }
                }
            }
        }
    }

    interface OnHorizontalSlideListListener {
        fun addExposureEvent(childPosition: Int, link: ExposureLinkEntity)

        fun onChildItemClick(childPosition: Int, entity: CommonCollectionContentEntity)

        fun navigateToCommonCollectionDetailPage(data: CustomPageData.CommonContentCollection)
    }
}