package com.gh.gamecenter.home.gamecollection.carousel

import android.content.Context
import android.view.View
import android.view.ViewGroup
import com.gh.common.util.DirectUtils
import com.gh.common.util.NewFlatLogUtils
import com.gh.common.util.NewLogUtils
import com.gh.gamecenter.GameDetailActivity
import com.gh.gamecenter.R
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
import com.gh.gamecenter.common.base.activity.BaseActivity
import com.gh.gamecenter.common.json.json
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.common.view.AsyncCell
import com.gh.gamecenter.core.utils.DisplayUtils
import com.gh.gamecenter.core.utils.TimeUtils
import com.gh.gamecenter.databinding.HomeGameCollectionCardItemBinding
import com.gh.gamecenter.entity.GameListCollection
import com.gh.gamecenter.gamecollection.detail.GameCollectionDetailActivity
import com.gh.gamecenter.gamecollection.square.GameCollectionListItemData
import com.lightgame.adapter.BaseRecyclerAdapter

class HomeGameCollectionCarouselAdapter(
    context: Context,
    private val mEntrance: String,
    var gameListCollection: GameListCollection,
    private val mBlockId: String = "",
    private val mBlockName: String = ""
) :
    BaseRecyclerAdapter<HomeGameCollectionCarouselAdapter.HomeGameCollectionCardViewHolder>(context) {
    private val mPosterWidth = DisplayUtils.getScreenWidth() - 50F.dip2px()

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

    override fun onBindViewHolder(holder: HomeGameCollectionCardViewHolder, position: Int) {
        if (gameListCollection.gameCollectionItemDataList.isNotEmpty()) {
            val gameCollectionItemData = gameListCollection.gameCollectionItemDataList.safelyGetInRelease(position)
            (holder.itemView as HomeGameCollectionCarouselItemCell).bindWhenInflated {
                (holder.itemView as HomeGameCollectionCarouselItemCell).binding?.run {
                    gameCollectionItemData?.let {
                        holder.bindGameCollectionCard(
                            this,
                            it,
                            mEntrance,
                            gameListCollection.id,
                            gameListCollection.name,
                            mBlockId,
                            mBlockName
                        )
                    }
                }
            }
        }
    }

    override fun getItemViewType(position: Int) = position

    override fun getItemCount() = gameListCollection.gameCollectionItemDataList.size

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

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

    inner class HomeGameCollectionCardViewHolder(val cell: HomeGameCollectionCarouselItemCell) :
        BaseRecyclerViewHolder<Any>(cell) {
        fun bindGameCollectionCard(
            binding: HomeGameCollectionCardItemBinding,
            itemData: GameCollectionListItemData,
            entrance: String,
            collectionId: String,
            collectionName: String,
            blockId: String,
            blockName: String
        ) {
            binding.run {
                val context = root.context
                val gamesCollectionEntity = itemData.homeGameCollectionItem
                root.layoutParams.width = mPosterWidth
                if (gamesCollectionEntity != null) {
                    poster.setTag(ImageUtils.TAG_TARGET_WIDTH, mPosterWidth)
                    ImageUtils.display(poster, gamesCollectionEntity.cover)
                    ImageUtils.display(userIv, gamesCollectionEntity.user?.icon)
                    titleTv.text = gamesCollectionEntity.title
                    gamesCollectionEntity.games?.take(3)?.forEachIndexed { index, game ->
                        when (index) {
                            0 -> iconIvOne.displayGameIcon(game)
                            1 -> iconIvTwo.displayGameIcon(game)
                            2 -> iconIvThree.displayGameIcon(game)
                        }
                    }
                    gamesCollectionEntity.count?.game?.let {
                        countTv.goneIf(it < 4 || gamesCollectionEntity.games?.size == 0)
                        countTv.text = "+ ${it - 3}"
                    }
                    userTv.text = gamesCollectionEntity.user?.name
                    timeTv.text = TimeUtils.getFormatTime(gamesCollectionEntity.time?.update ?: 0, "MM - dd")
                    timeTv.goneIf(gamesCollectionEntity.stamp.isNotEmpty())
                    stampIv.goneIf(gamesCollectionEntity.stamp.isEmpty())
                    stampIv.setBackgroundResource(if (gamesCollectionEntity.stamp == "official") R.drawable.ic_official else R.drawable.ic_chosen)
                    listOf(iconIvOne, iconIvTwo, iconIvThree).forEachIndexed { index, iconIv ->
                        iconIv.goneIf((gamesCollectionEntity.games?.size ?: 0) < index + 1) {
                            val game = gamesCollectionEntity.games?.safelyGetInRelease(index)
                            iconIv.setOnClickListener {
                                NewLogUtils.logClickGameCollectionGameIcon(
                                    gamesCollectionEntity.title,
                                    gamesCollectionEntity.id,
                                    game?.name ?: "",
                                    game?.id ?: ""
                                )
                                NewFlatLogUtils.logGameListCollectionClick(
                                    entrance,
                                    blockName,
                                    blockId,
                                    collectionName,
                                    collectionId,
                                    "游戏"
                                )
                                SensorsBridge.trackEvent("GameListCollectionClick", json {
                                    "location" to if (mEntrance.contains("首页")) "首页" else if (mEntrance.contains("版块")) "版块" else ""
                                    "block_name" to blockName
                                    "block_id" to blockId
                                    "game_list_collection_name" to collectionName
                                    "game_list_collection_id" to collectionId
                                    "text" to "游戏"
                                    "game_name" to game?.name
                                    "game_id" to game?.id
                                })
                                game?.id?.let { id ->
                                    GameDetailActivity.startGameDetailActivity(
                                        context,
                                        id,
                                        BaseActivity.mergeEntranceAndPath(entrance, "游戏单合集"),
                                        game.exposureEvent
                                    )
                                }
                            }
                        }
                    }
                    userContainer.setOnClickListener {
                        NewLogUtils.logClickGameCollectionAuthor(gamesCollectionEntity.title, gamesCollectionEntity.id)
                        NewFlatLogUtils.logGameListCollectionClick(
                            entrance,
                            blockName,
                            blockId,
                            collectionName,
                            collectionId,
                            "个人主页"
                        )
                        SensorsBridge.trackEvent("GameListCollectionClick", json {
                            "location" to if (mEntrance.contains("首页")) "首页" else if (mEntrance.contains("版块")) "版块" else ""
                            "block_name" to blockName
                            "block_id" to blockId
                            "game_list_collection_name" to collectionName
                            "game_list_collection_id" to collectionId
                            "text" to "个人主页"
                            "mongold_id" to gamesCollectionEntity.user?.id
                        })
                        DirectUtils.directToHomeActivity(
                            context,
                            gamesCollectionEntity.user?.id,
                            0,
                            entrance,
                            "游戏单合集"
                        )
                    }
                    root.setOnClickListener {
                        NewFlatLogUtils.logGameListCollectionClick(
                            entrance,
                            blockName,
                            blockId,
                            collectionName,
                            collectionId,
                            "游戏单"
                        )
                        NewLogUtils.logEnterGameCollectionDetail(gamesCollectionEntity.title, gamesCollectionEntity.id)
                        SensorsBridge.trackEvent("GameListCollectionClick", json {
                            "location" to if (mEntrance.contains("首页")) "首页" else if (mEntrance.contains("版块")) "版块" else ""
                            "block_name" to blockName
                            "block_id" to blockId
                            "game_list_collection_name" to collectionName
                            "game_list_collection_id" to collectionId
                            "text" to "游戏单"
                            "game_collect_title" to gamesCollectionEntity.title
                            "game_collect_id" to gamesCollectionEntity.id
                        })
                        context.startActivity(
                            GameCollectionDetailActivity.getIntent(
                                context,
                                gamesCollectionEntity.id,
                                exposureSourceList = itemData.exposureEvent?.source?.let { source -> ArrayList(source) }
                            )
                        )
                    }
                }
            }
        }
    }
}