package com.gh.gamecenter.home.custom.floatview

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.FrameLayout
import com.gh.common.exposure.ExposureManager
import com.gh.common.prioritychain.CustomFloatingWindowHandler
import com.gh.gamecenter.common.exposure.ExposureSource
import com.gh.gamecenter.common.utils.SensorsBridge
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.utils.visibleIf
import com.gh.gamecenter.databinding.LayoutFloatingWindowCustomBinding
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.feature.entity.PageLocation
import com.gh.gamecenter.feature.entity.WelcomeDialogEntity
import com.gh.gamecenter.feature.exposure.ExposureEvent
import com.gh.gamecenter.feature.entity.FloatingWindowEntity

class CustomFloatView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {

    private val binding: LayoutFloatingWindowCustomBinding =
        LayoutFloatingWindowCustomBinding.inflate(LayoutInflater.from(context), this, true)

    private val adapter by lazy {
        CustomFloatingWindowViewAdapter(context, object : CustomFloatingWindowViewAdapter.Callback {
            override fun onItemExpandCallback(entity: WelcomeDialogEntity) {
                // 展开大图时，需要隐藏悬浮窗
                visibility = GONE
                expandListener?.invoke(entity, true)
            }

            override fun onItemDismissClickedCallback(position: Int) {
                binding.horizontalScrollView.removeIndicator(position)
            }

            override fun onDataListEmptyCallback() {
                visibleIf(false)
            }

        })
    }

    fun setData(
        entities: ArrayList<FloatingWindowEntity>,
        pageLocation: PageLocation,
        basicExposureSourceList: List<ExposureSource>,
        floatingWindowHandler: CustomFloatingWindowHandler
    ) {
        entities.forEachIndexed { index, entity ->
            entity.link.traceEvent = ExposureEvent.createEventWithSourceConcat(
                GameEntity(sequence = index),
                basicExposureSourceList,
                listOf(ExposureSource("右下悬浮窗", entity.id))
            )
        }

        adapter.submitList(entities, true)
        adapter.pageLocation = pageLocation
        binding.horizontalScrollView.setFixedWidthAndHeightForViewPager2(64F.dip2px(), 64F.dip2px())
        binding.horizontalScrollView.disableAutoScroll()
        binding.horizontalScrollView.start(entities, false, adapter) {
            val entity = entities.getOrNull(it)
            val link = entity?.link
            SensorsBridge.trackSuspendedWindowShow(
                "收起",
                "自定义页面",
                bottomTab = pageLocation.bottomTab,
                multiTabId = pageLocation.severalTabPageId,
                multiTabName = pageLocation.severalTabPageName,
                position = pageLocation.tabPosition,
                tabContent = pageLocation.tabContent,
                customPageId = pageLocation.pageId,
                customPageName = pageLocation.pageName,
                linkText = link?.text ?: "",
                linkId = link?.link ?: "",
                linkType = link?.type ?: "",
            )
            entity?.link?.traceEvent?.let { exposureEvent -> ExposureManager.log(exposureEvent) }
        }


        // 是否需要自动展开大图
        entities.find {
            it.autoExpandBigPopup
        }?.let {
            post {
                it.link.icon = it.expandedImage
                it.link.floatingWindowId = it.id
                visibility = INVISIBLE
                expandListener?.invoke(it.link, false)
            }
        } ?: floatingWindowHandler.dismiss()


    }

    override fun setVisibility(visibility: Int) {
        if (adapter.dataList.isEmpty()) {
            super.setVisibility(GONE)
        } else {
            super.setVisibility(visibility)
        }
    }

    private var expandListener: ((WelcomeDialogEntity, Boolean) -> Unit)? = null
    fun setExpandListener(listener: (WelcomeDialogEntity, hasAnimator: Boolean) -> Unit) {
        expandListener = listener
    }

}