package com.gh.gamecenter.home.gamecollection

import android.content.Context
import android.view.View
import android.view.ViewGroup
import com.gh.gamecenter.common.base.activity.BaseActivity
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
import com.gh.common.util.*
import com.gh.gamecenter.common.view.AsyncCell
import com.gh.gamecenter.GameDetailActivity
import com.gh.gamecenter.R
import com.gh.gamecenter.core.utils.TimeUtils
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.core.utils.DisplayUtils
import com.gh.gamecenter.common.utils.ImageUtils
import com.gh.gamecenter.databinding.HomeGameCollectionCardItemBinding
import com.gh.gamecenter.gamecollection.detail.GameCollectionDetailActivity
import com.gh.gamecenter.gamecollection.square.GameCollectionListItemData
import com.lightgame.adapter.BaseRecyclerAdapter

class HomeGameCollectionAdapter(context: Context, private val entrance: String) :
    BaseRecyclerAdapter<HomeGameCollectionAdapter.HomeGameCollectionCardViewHolder>(context) {

    private var mGameCollectionItemDataList = listOf<GameCollectionListItemData>()

    fun setGameCollectionList(gameCollectionItemDataList: List<GameCollectionListItemData>) {
        mGameCollectionItemDataList = gameCollectionItemDataList
        notifyDataSetChanged()
    }

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

    override fun onBindViewHolder(holder: HomeGameCollectionCardViewHolder, position: Int) {
        if (mGameCollectionItemDataList.isNotEmpty()) {
            (holder.itemView as HomeGameCollectionItemCell).bindWhenInflated {
                (holder.itemView as HomeGameCollectionItemCell).binding?.run {
                    holder.bindGameCollectionCard(this, mGameCollectionItemDataList[position], entrance)
                }
            }
        }
    }

    override fun getItemViewType(position: Int) = position

    override fun getItemCount() = mGameCollectionItemDataList.size

    class HomeGameCollectionItemCell(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
        }
    }

    class HomeGameCollectionCardViewHolder(val cell: HomeGameCollectionItemCell) : BaseRecyclerViewHolder<Any>(cell) {
        fun bindGameCollectionCard(
            binding: HomeGameCollectionCardItemBinding,
            itemData: GameCollectionListItemData,
            entrance: String
        ) {
            binding.run {
                val context = root.context
                val gamesCollectionEntity = itemData.gameCollectionItem
                root.layoutParams.width = DisplayUtils.getScreenWidth() - 50F.dip2px()
                if (gamesCollectionEntity != null) {
                    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.toGameEntity())
                            1 -> iconIvTwo.displayGameIcon(game.toGameEntity())
                            2 -> iconIvThree.displayGameIcon(game.toGameEntity())
                        }
                    }
                    gamesCollectionEntity.games?.size?.let {
                        iconIvOne.goneIf(it == 0)
                        iconIvTwo.goneIf(it < 2)
                        iconIvThree.goneIf(it < 3)
                    }
                    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)
                    iconIvOne.setOnClickListener {
                        val game = gamesCollectionEntity.games?.get(0)
                        NewLogUtils.logClickGameCollectionGameIcon(
                            gamesCollectionEntity.title,
                            gamesCollectionEntity.id,
                            game?.name ?: "",
                            game?.id ?: ""
                        )
                        game?.id?.let { id ->
                            GameDetailActivity.startGameDetailActivity(
                                context,
                                id,
                                BaseActivity.mergeEntranceAndPath(entrance, "游戏单"),
                                itemData.exposureEventList?.safelyGetInRelease(0)
                            )
                        }
                    }
                    iconIvTwo.setOnClickListener {
                        val game = gamesCollectionEntity.games?.get(1)
                        NewLogUtils.logClickGameCollectionGameIcon(
                            gamesCollectionEntity.title,
                            gamesCollectionEntity.id,
                            game?.name ?: "",
                            game?.id ?: ""
                        )
                        game?.id?.let { id ->
                            GameDetailActivity.startGameDetailActivity(
                                context,
                                id,
                                BaseActivity.mergeEntranceAndPath(entrance, "游戏单"),
                                itemData.exposureEventList?.safelyGetInRelease(1)
                            )
                        }
                    }
                    iconIvThree.setOnClickListener {
                        val game = gamesCollectionEntity.games?.get(2)
                        NewLogUtils.logClickGameCollectionGameIcon(
                            gamesCollectionEntity.title,
                            gamesCollectionEntity.id,
                            game?.name ?: "",
                            game?.id ?: ""
                        )
                        game?.id?.let { id ->
                            GameDetailActivity.startGameDetailActivity(
                                context,
                                id,
                                BaseActivity.mergeEntranceAndPath(entrance, "游戏单"),
                                itemData.exposureEventList?.safelyGetInRelease(2)
                            )
                        }
                    }
                    userContainer.setOnClickListener {
                        NewLogUtils.logClickGameCollectionAuthor(gamesCollectionEntity.title, gamesCollectionEntity.id)
                        DirectUtils.directToHomeActivity(context, gamesCollectionEntity.user?.id, 0, entrance, "游戏单")
                    }
                    root.setOnClickListener {
                        NewLogUtils.logEnterGameCollectionDetail(gamesCollectionEntity.title, gamesCollectionEntity.id)
                        context.startActivity(GameCollectionDetailActivity.getIntent(context, gamesCollectionEntity.id))
                    }
                }
            }
        }
    }
}