package com.gh.gamecenter.catalog

import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.viewpager.widget.PagerAdapter
import com.facebook.drawee.view.SimpleDraweeView
import com.gh.common.exposure.ExposureEvent
import com.gh.common.exposure.ExposureSource
import com.gh.common.util.DirectUtils
import com.gh.gamecenter.common.utils.ImageUtils
import com.gh.gamecenter.R
import com.gh.gamecenter.adapter.RecyclingPagerAdapter
import com.gh.gamecenter.core.utils.PageSwitchDataHelper
import com.gh.gamecenter.entity.GameEntity

class BannerAdapter(
    private val mContext: Context,
    private val mCatalogViewModel: SpecialCatalogViewModel,
    private var mItemData: SpecialCatalogItemData,
    mExposureSource: List<ExposureSource>,
    private val mLastPageDataMap: HashMap<String, String>?
) : RecyclingPagerAdapter() {

    private var mBanners = mItemData.banner!!.data
    private var mIsInfiniteLoop = getDataSize() != 1
    private var mExposureSourceList = ArrayList<ExposureSource>()

    init {
        mItemData.exposureEventList = arrayListOf()
        mExposureSourceList.addAll(mExposureSource)
        mExposureSourceList.add(ExposureSource("精选页轮播图"))
    }

    override fun getCount() = if (mIsInfiniteLoop) Int.MAX_VALUE else getDataSize()

    override fun getItemPosition(`object`: Any) = PagerAdapter.POSITION_NONE

    override fun getView(position: Int, convertView: View?, container: ViewGroup?): View {
        var view: View? = convertView
        if (convertView == null) {
            view = View.inflate(mContext, R.layout.banner_viewpager_item, null)
        }

        val index = getRealPosition(position)
        if (index >= getDataSize()) return view!!

        val banner = mBanners[index]
        val image = view!!.findViewById<SimpleDraweeView>(R.id.image)
        val title = view.findViewById<TextView>(R.id.title)
        ImageUtils.display(image, banner.image)
        title.text = banner.title

        var exposureEvent: ExposureEvent? = null
        if ("game" == banner.link.type) {
            exposureEvent = ExposureEvent.createEvent(
                GameEntity(
                    id = banner.link.link,
                    name = banner.link.text
                ).apply { sequence = index },
                mExposureSourceList
            ).apply {
                mLastPageDataMap?.let {
                    payload.sourcePage = it[PageSwitchDataHelper.PAGE_BUSINESS_TYPE]
                    payload.sourcePageId = it[PageSwitchDataHelper.PAGE_BUSINESS_ID]
                    payload.sourcePageName = it[PageSwitchDataHelper.PAGE_BUSINESS_NAME]
                }
            }
            mItemData.exposureEventList?.add(exposureEvent)
        }

        view.setOnClickListener {
            mCatalogViewModel.logSpecialCatalogBannerClick(banner.title, banner.link.text ?: "", index)
            DirectUtils.directToLinkPage(mContext, banner.link, "新分类-精选分类", "轮播图", exposureEvent)
        }

        return view
    }

    private fun getRealPosition(position: Int) = if (mIsInfiniteLoop) position % getDataSize() else position

    fun getDataSize() = mBanners.size

    fun checkResetData(itemData: SpecialCatalogItemData) {
        val banners = itemData.banner!!.data
        if (mBanners != banners) {
            mBanners = banners
        }

        if (mItemData.exposureEventList != null) {
            mItemData.exposureEventList?.clear()
            mItemData = itemData
            mItemData.exposureEventList = arrayListOf()
        }

        notifyDataSetChanged()
    }
}