package com.gh.gamecenter.gamedetail.detail.adapter

import android.view.ViewGroup
import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.DiffUtil.ItemCallback
import androidx.recyclerview.widget.ListAdapter
import com.gh.gamecenter.common.utils.toBinding
import com.gh.gamecenter.feature.view.DownloadButton
import com.gh.gamecenter.gamedetail.GameDetailViewModel
import com.gh.gamecenter.gamedetail.detail.viewholder.*
import com.gh.gamecenter.gamedetail.entity.GameDetailData

class GameDetailListAdapter(
    private val downloadBtn: DownloadButton?,
    private val viewModel: GameDetailViewModel,
    private val lifecycleOwner: LifecycleOwner
) :
    ListAdapter<GameDetailData, BaseGameDetailItemViewHolder>(createDiffCallback()) {
    override fun getItemViewType(position: Int): Int {
        val data = currentList[position]
        return when {
            data?.linkContentCard != null -> {
                when (data.linkContentCard.size) {
                    1 -> ITEM_CONTENT_CARD_SINGLE
                    2 -> ITEM_CONTENT_CARD_DOUBLE
                    else -> ITEM_CONTENT_CARD_TRIPLE
                }
            }

            data?.linkAdvertising != null -> if (data.linkAdvertising.img.isEmpty()) ITEM_ADVERTISING else ITEM_ADVERTISING_IMAGE
            data?.linkComprehensive != null -> ITEM_COMPREHENSIVE_PANEL
            data?.linkCustomColumn != null -> ITEM_CUSTOM_COLUMN
            data?.linkDrawer != null -> ITEM_DRAWER
            data?.linkAnnouncement != null -> ITEM_ANNOUNCEMENT
            data?.linkGameBrief != null -> ITEM_GAME_BRIEF
            data?.linkDeveloperWord != null -> ITEM_DEVELOPER_WORD
            data?.linkUpdate != null -> ITEM_UPDATE
            data?.linkComment != null -> ITEM_COMMENT
            data?.linkInfo != null -> ITEM_DETAIL_INFO
            data?.linkContentRecommend != null -> ITEM_CONTENT_RECOMMEND
            data?.linkGameVideo != null -> ITEM_GAME_VIDEO
            data?.linkGameGuide != null -> ITEM_GAME_STRATEGY
            data?.linkServer != null -> ITEM_SERVER
            data?.linkLibao != null -> ITEM_GIFT
            data?.linkRelatedGame != null -> ITEM_RELATED_GAME
            data?.linkImageRecommend != null -> ITEM_RECOMMEND_IMAGE
            data?.linkEveryonePlaying != null -> ITEM_RECOMMEND_GAME
            data?.linkRecommendGameList != null -> ITEM_RECOMMEND_GAME_COLLECTION
            else -> ITEM_RECOMMEND_COLUMN
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseGameDetailItemViewHolder {
        return when (viewType) {
            ITEM_CONTENT_CARD_SINGLE -> GameDetailContentCardSingleItemViewHolder(parent.toBinding(), downloadBtn, viewModel, lifecycleOwner)
            ITEM_CONTENT_CARD_DOUBLE -> GameDetailContentCardDoubleItemViewHolder(parent.toBinding(), downloadBtn, viewModel, lifecycleOwner)
            ITEM_CONTENT_CARD_TRIPLE -> GameDetailContentCardTripleItemViewHolder(parent.toBinding(), downloadBtn, viewModel, lifecycleOwner)
            ITEM_ADVERTISING -> GameDetailAdvertisingItemViewHolder(parent.toBinding(), downloadBtn, viewModel)
            ITEM_ADVERTISING_IMAGE -> GameDetailAdvertisingImageItemViewHolder(parent.toBinding(), downloadBtn, viewModel)
            ITEM_COMPREHENSIVE_PANEL -> GameDetailComprehensivePanelItemViewHolder(parent.toBinding(), downloadBtn, viewModel)
            ITEM_CUSTOM_COLUMN -> GameDetailCustomColumnItemViewHolder(parent.toBinding(), downloadBtn, viewModel)
            ITEM_DRAWER -> GameDetailDrawerItemViewHolder(parent.toBinding(), downloadBtn, viewModel)
            ITEM_ANNOUNCEMENT -> GameDetailAnnouncementItemViewHolder(parent.toBinding(), downloadBtn, viewModel)
            ITEM_GAME_BRIEF -> GameDetailBriefItemViewHolder(parent.toBinding(), downloadBtn, viewModel)
            ITEM_DEVELOPER_WORD -> GameDetailDeveloperWordItemViewHolder(
                parent.toBinding(),
                downloadBtn,
                viewModel
            )
            ITEM_UPDATE -> GameDetailUpdateItemViewHolder(parent.toBinding(), downloadBtn, viewModel)
            ITEM_COMMENT -> GameDetailCommentItemViewHolder(parent.toBinding(), downloadBtn, viewModel)
            ITEM_DETAIL_INFO -> GameDetailInfoItemViewHolder(parent.toBinding(), downloadBtn, viewModel)
            ITEM_CONTENT_RECOMMEND -> GameDetailContentRecommendItemViewHolder(parent.toBinding(), downloadBtn, viewModel, lifecycleOwner)
            ITEM_GAME_VIDEO -> GameDetailVideoItemViewHolder(parent.toBinding(), downloadBtn, viewModel)
            ITEM_GAME_STRATEGY -> GameDetailStrategyItemViewHolder(parent.toBinding(), downloadBtn, viewModel)
            ITEM_SERVER -> GameDetailServerItemViewHolder(parent.toBinding(), downloadBtn, viewModel)
            ITEM_GIFT -> GameDetailGiftItemViewHolder(parent.toBinding(), downloadBtn, viewModel)
            ITEM_RELATED_GAME -> GameDetailRelatedGameItemViewHolder(parent.toBinding(), downloadBtn, viewModel)
            ITEM_RECOMMEND_IMAGE -> GameDetailRecommendImageItemViewHolder(parent.toBinding(), downloadBtn, viewModel)
            ITEM_RECOMMEND_GAME -> GameDetailRecommendGameItemViewHolder(parent.toBinding(), downloadBtn, viewModel)
            ITEM_RECOMMEND_GAME_COLLECTION -> GameDetailRecommendGameCollectionItemViewHolder(parent.toBinding(), downloadBtn, viewModel)
            else -> GameDetailRecommendColumnItemViewHolder(parent.toBinding(), downloadBtn, viewModel)
        }
    }

    override fun onBindViewHolder(holder: BaseGameDetailItemViewHolder, position: Int) {
        val item = getItem(position)
        holder.bindView(item)
    }

    companion object {
        const val ITEM_CONTENT_CARD_SINGLE = 0 // 内容卡片-单个
        const val ITEM_CONTENT_CARD_DOUBLE = 1 // 内容卡片-两个
        const val ITEM_CONTENT_CARD_TRIPLE = 2 // 内容卡片-三个
        const val ITEM_ADVERTISING = 3 // 广告推荐-无图片
        const val ITEM_ADVERTISING_IMAGE = 4 // 广告推荐-带图片
        const val ITEM_COMPREHENSIVE_PANEL = 5 // 综合面板
        const val ITEM_CUSTOM_COLUMN = 6 // 自定义栏目
        const val ITEM_DRAWER = 7 // 列表抽屉
        const val ITEM_ANNOUNCEMENT = 8 // 资讯公告
        const val ITEM_GAME_BRIEF = 9 // 游戏简介
        const val ITEM_DEVELOPER_WORD = 10 // 开发者的话
        const val ITEM_UPDATE = 11 // 更新内容
        const val ITEM_COMMENT = 12 // 玩家评论
        const val ITEM_DETAIL_INFO = 13 // 详情信息
        const val ITEM_CONTENT_RECOMMEND = 14 // 内容推荐（PK组件）
        const val ITEM_GAME_VIDEO = 15 // 视频
        const val ITEM_GAME_STRATEGY = 16 // 游戏攻略
        const val ITEM_SERVER = 17 // 游戏开服
        const val ITEM_GIFT = 18 // 游戏礼包
        const val ITEM_RELATED_GAME = 19 // 相关游戏
        const val ITEM_RECOMMEND_IMAGE = 20 // 图片推荐
        const val ITEM_RECOMMEND_GAME = 21 // 大家都在玩
        const val ITEM_RECOMMEND_GAME_COLLECTION = 22 // 游戏单推荐
        const val ITEM_RECOMMEND_COLUMN = 23 // 专题推荐

        fun createDiffCallback() = object : ItemCallback<GameDetailData>() {
            override fun areItemsTheSame(oldItem: GameDetailData, newItem: GameDetailData): Boolean {
                return oldItem.areItemsTheSame(newItem)
            }

            override fun areContentsTheSame(oldItem: GameDetailData, newItem: GameDetailData): Boolean {
                if (oldItem.linkLibao != null && newItem.linkLibao != null) {
                    return false
                }
                if (oldItem.linkEveryonePlaying != null && newItem.linkEveryonePlaying != null) {
                    return false
                }
                if (oldItem.linkContentRecommend != null && newItem.linkContentRecommend != null) {
                    return false
                }
                if (oldItem.recommendColumn != null && newItem.recommendColumn != null) {
                    return false
                }
                return oldItem.areContentsTheSame(newItem)
            }
        }
    }
}