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

import android.content.Context
import android.view.View
import android.view.ViewGroup
import com.gh.gamecenter.R
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
import com.gh.gamecenter.common.utils.ImageUtils
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.utils.goneIf
import com.gh.gamecenter.common.utils.safelyGetInRelease
import com.gh.gamecenter.common.view.AsyncCell
import com.gh.gamecenter.core.utils.DisplayUtils
import com.gh.gamecenter.databinding.HomeGameCollectionCardItemCustomBinding
import com.gh.gamecenter.feature.exposure.ExposureEvent
import com.gh.gamecenter.feature.exposure.IExposureListProvider
import com.gh.gamecenter.home.PageConfigure
import com.gh.gamecenter.home.custom.eventlistener.GameSubjectCollectionEventHelper
import com.gh.gamecenter.home.custom.model.CustomPageData
import com.gh.gamecenter.home.custom.model.CustomSubjectCollectionItem

class CustomHomeGameCollectionCarouselAdapter(
    context: Context,
    private val pageConfigure: PageConfigure,
    private val eventHelper: GameSubjectCollectionEventHelper
) : CustomBaseChildAdapter<
            CustomPageData.LinkColumnCollection.CustomSubjectEntity,
            CustomHomeGameCollectionCarouselAdapter.HomeGameCollectionCardViewHolder>(context) {

    private val mPosterWidth = DisplayUtils.getScreenWidth() - 50F.dip2px()

    private lateinit var _data: CustomSubjectCollectionItem

    fun setData(item: CustomSubjectCollectionItem) {
        _data = item
        submitList(item.data.data, true)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
        HomeGameCollectionCardViewHolder(HomeGameCollectionCarouselItemCell(context).apply { inflate() })

    override fun onBindViewHolder(holder: HomeGameCollectionCardViewHolder, position: Int) {
        val item = getItem(position)
        (holder.itemView as HomeGameCollectionCarouselItemCell).bindWhenInflated {
            (holder.itemView as HomeGameCollectionCarouselItemCell).binding?.run {
                holder.bindGameCollectionCard(
                    this,
                    item,
                    pageConfigure.entrance,
                    position
                )
            }
        }
    }

    override fun getItemViewType(position: Int) = position

    class HomeGameCollectionCarouselItemCell(context: Context) : AsyncCell(context) {
        var binding: HomeGameCollectionCardItemCustomBinding? = null

        override val layoutId = R.layout.home_game_collection_card_item_custom
        override val delayFirstTimeBindView = false
        override fun createDataBindingView(view: View): View? {
            binding = HomeGameCollectionCardItemCustomBinding.bind(view)
            return view.rootView
        }
    }

    inner class HomeGameCollectionCardViewHolder(cell: HomeGameCollectionCarouselItemCell) :
        BaseRecyclerViewHolder<Any>(cell), IExposureListProvider {
            private var boundedItemData: CustomPageData.LinkColumnCollection.CustomSubjectEntity? = null

        fun bindGameCollectionCard(
            binding: HomeGameCollectionCardItemCustomBinding,
            itemData: CustomPageData.LinkColumnCollection.CustomSubjectEntity,
            entrance: String,
            itemPosition: Int
        ) {
            boundedItemData = itemData

            binding.run {
                root.layoutParams.width = mPosterWidth

                if (itemData.user.isValid) {
                    binding.userContainer.goneIf(false)
                    ImageUtils.display(userIv, itemData.user.icon)
                    userTv.text = itemData.user.name
                    userContainer.setOnClickListener {
                        eventHelper.onUserClick(itemData.user.id)
                    }
                } else {
                    binding.userContainer.goneIf(true)
                }
                poster.setTag(ImageUtils.TAG_TARGET_WIDTH, mPosterWidth)
                ImageUtils.display(poster, itemData.image)

                titleTv.text = itemData.title
                itemData.games.take(3).forEachIndexed { index, game ->
                    when (index) {
                        0 -> iconIvOne.displayGameIcon(game)
                        1 -> iconIvTwo.displayGameIcon(game)
                        2 -> iconIvThree.displayGameIcon(game)
                    }
                }
                itemData.count.game.let {
                    moreTv.goneIf(it < 4 || itemData.games.isEmpty())
                    moreTv.text = "+${itemData.count.game - 3}"
                }

                stampIv.goneIf(itemData.stamp.isEmpty())
                stampIv.setBackgroundResource(if (itemData.stamp == "official") R.drawable.ic_official else R.drawable.ic_chosen)
                listOf(iconIvOne, iconIvTwo, iconIvThree).forEachIndexed { index, iconIv ->
                    iconIv.goneIf((itemData.games.size) < index + 1) {
                        val game = itemData.games.safelyGetInRelease(index)
                        iconIv.setOnClickListener {
                            game?.let { game ->
                                eventHelper.navigateToGameDetailPage(index, game, itemData)
                            }
                        }
                    }
                }

                root.setOnClickListener {
                    eventHelper.navigateSubjectCollectionPage(itemPosition, itemData)
                }
            }
        }

        override fun provideExposureData(): ExposureEvent? = null

        override fun provideExposureDataList(): List<ExposureEvent>? {
           return boundedItemData?.games?.map {
               it.exposureEvent?.getFreshExposureEvent()!!
           }
        }
    }
}