package com.gh.gamecenter.home

import com.gh.gamecenter.common.entity.LinkEntity
import com.gh.gamecenter.entity.GameEntity
import com.gh.gamecenter.entity.SubjectEntity
import com.gh.gamecenter.game.data.GameSubjectData

object LegacyHomeSubjectTransformer {

    fun transform(
        itemList: ArrayList<LegacyHomeItemData>,
        subjectEntity: SubjectEntity?,
        blockPosition: Int,
        newItemInstance: () -> LegacyHomeItemData,
        addGamePositionAndPackage: (gameEntity: GameEntity) -> Unit
    ) {
        val data = subjectEntity?.data
        var containsImageBeforeHead = false // 在专题名上面是否有大图
        val subjectId = subjectEntity?.id

        if (data == null || data.isEmpty()) return

        itemList.add(getBlankSpacingItem(newItemInstance()))

        if (!data[0].image.isNullOrEmpty() && subjectEntity.type != "column_collection") {
            val itemDataImage = newItemInstance()
            var isBigImageWithGame = false

            itemDataImage.blockPosition = blockPosition + 1

            val gameEntity = data[0]
            if (gameEntity.id.isEmpty()) gameEntity.id = gameEntity.link ?: ""

            gameEntity.subjectId = subjectId
            gameEntity.outerSequence = blockPosition
            gameEntity.isAdData = subjectEntity.adIconActive

            //  图片专题样式(后台操作->只显示(滑动))
            if (data[0].type == "column" && subjectEntity.type == "image_slide") {
                itemDataImage.imageSlide = gameEntity
                gameEntity.games?.let {
                    for ((index, g) in it.withIndex()) {
                        g.sequence = index
                        g.outerSequence = blockPosition
                        g.subjectId = subjectId
                    }
                }
            } else {
                itemDataImage.image = gameEntity
                itemDataImage.image?.subjectData = GameSubjectData(
                    id = subjectEntity.id,
                    name = subjectEntity.name,
                    isOrder = false
                )
                if (gameEntity.type == "game") {
                    isBigImageWithGame = true
                }
            }

            containsImageBeforeHead = true
            itemList.add(itemDataImage)

            // 确保下载按钮更新
            if (isBigImageWithGame) {
                gameEntity.sequence = 0
                addGamePositionAndPackage(gameEntity)
            }

            itemList.add(getBlankSpacingItem(newItemInstance())) // 图片风格 item 后需要空白间距

            if (subjectEntity.type == "image" || subjectEntity.type == "image_slide") return
        }

        if (subjectEntity.type != "gallery" && subjectEntity.showName) {
            val itemDataHead = newItemInstance()
            itemDataHead.blockPosition = blockPosition + 1
            itemDataHead.columnHead = subjectEntity

            // 专题名称与大图间需要额外的空白间距
            if (containsImageBeforeHead) {
                itemList.add(getBlankSpacingItem(newItemInstance()))
            }

            itemList.add(itemDataHead)
        } else {
            // 标题隐藏的情况下移除默认添加到空白间距
            if (itemList.lastOrNull()?.blankDivider != null) {
                itemList.removeLastOrNull()
            }
        }

        if (subjectEntity.type == "game_vertical_slide") {
            val itemVerticalSlide = newItemInstance()
            itemVerticalSlide.blockPosition = blockPosition + 1
            itemVerticalSlide.verticalSlide = subjectEntity
            itemList.add(itemVerticalSlide)

            for (i in 0 until data.size) {
                val game = data[i]
                if (!game.image.isNullOrEmpty()) continue
                game.subjectData = GameSubjectData(
                    id = subjectEntity.id,
                    name = subjectEntity.name,
                    tag = subjectEntity.tag,
                    position = i + if (data[0].image.isNullOrEmpty()) 1 else 0,
                    isOrder = subjectEntity.isOrder,
                    briefStyle = subjectEntity.briefStyle,
                    isShowSuffix = subjectEntity.showSuffix
                )
                game.outerSequence = blockPosition
                game.sequence = i
                game.isAdData = subjectEntity.adIconActive

                addGamePositionAndPackage(game)
            }
            return
        }

        if (subjectEntity.type == "game_horizontal_slide") {
            val itemHorizontalSlide = newItemInstance()
            itemHorizontalSlide.blockPosition = blockPosition + 1
            itemHorizontalSlide.horizontalSlide = subjectEntity
            if (subjectEntity.indexRightTop == "all") {
                subjectEntity.indexRightTopLink =
                    LinkEntity(text = subjectEntity.name, link = subjectEntity.id, type = "column_test")
            }
            itemList.add(itemHorizontalSlide)

            if (!subjectEntity.showDownload) {
                appendAdditionalInfoToSubjectGame(subjectEntity, blockPosition)
            } else {
                for (i in 0 until data.size) {
                    val game = data[i]
                    if (!game.image.isNullOrEmpty()) continue
                    game.subjectData = GameSubjectData(
                        id = subjectEntity.id,
                        name = subjectEntity.name,
                        tag = subjectEntity.tag,
                        position = i + if (data[0].image.isNullOrEmpty()) 1 else 0,
                        isOrder = subjectEntity.isOrder,
                        briefStyle = subjectEntity.briefStyle,
                        isShowSuffix = subjectEntity.showSuffix
                    )
                    game.outerSequence = blockPosition
                    game.sequence = i
                    game.isAdData = subjectEntity.adIconActive

                    addGamePositionAndPackage(game)
                }
            }
            return
        }

        if (subjectEntity.type == "game_horizontal") {
            val itemDataSubject = newItemInstance()
            itemDataSubject.blockPosition = blockPosition + 1
            itemDataSubject.horizontalColumn = subjectEntity
            itemList.add(itemDataSubject)

            if (!subjectEntity.showDownload) {
                appendAdditionalInfoToSubjectGame(subjectEntity, blockPosition)
            } else {
                for (i in 0 until data.size) {
                    val game = data[i]
                    if (!game.image.isNullOrEmpty()) continue
                    game.subjectData = GameSubjectData(
                        id = subjectEntity.id,
                        name = subjectEntity.name,
                        tag = subjectEntity.tag,
                        position = i + if (data[0].image.isNullOrEmpty()) 1 else 0,
                        isOrder = subjectEntity.isOrder,
                        briefStyle = subjectEntity.briefStyle,
                        isShowSuffix = subjectEntity.showSuffix
                    )
                    game.outerSequence = blockPosition
                    game.sequence = i
                    game.isAdData = subjectEntity.adIconActive

                    addGamePositionAndPackage(game)
                }
            }
            return
        }

        if (subjectEntity.type == "column_collection") {
            val itemColumnCollection = newItemInstance()
            itemColumnCollection.blockPosition = blockPosition + 1
            itemColumnCollection.columnCollection = subjectEntity
            appendAdditionalInfoToSubjectGame(subjectEntity, blockPosition)
            itemList.add(itemColumnCollection)
            return
        }

        if (subjectEntity.type == "gallery") {
            val itemItemData = newItemInstance()
            itemItemData.gallery = subjectEntity
            itemItemData.blockPosition = blockPosition + 1
            // 小于7就不要展示了
            if (subjectEntity.data != null && subjectEntity.data!!.size >= 7) {
                appendAdditionalInfoToSubjectGame(subjectEntity, blockPosition)
                itemList.add(itemItemData)
            }
            return
        }

        if (subjectEntity.type == "gallery_slide") {
            val itemItemData = newItemInstance()
            itemItemData.gallerySlide = subjectEntity
            // 小于3就不要滚动了
            if (subjectEntity.data != null && subjectEntity.data!!.size > 3) {
                appendAdditionalInfoToSubjectGame(subjectEntity, blockPosition)
                itemList.add(itemItemData)
            }
            return
        }

        if (subjectEntity.type == "game_double_card") {
            val gameCount = subjectEntity.data?.size ?: return

            appendAdditionalInfoToSubjectGame(subjectEntity, blockPosition)
            for (i in 0 until gameCount) {
                if (i % 2 == 0) {
                    if (i == 0) {
                        itemList.add(newItemInstance().apply { blankDivider = 12F })
                    }

                    val itemDataSubject = newItemInstance()
                    itemDataSubject.blockPosition = blockPosition + 1
                    itemDataSubject.doubleCardColumn = subjectEntity
                    itemDataSubject.offset = i
                    itemList.add(itemDataSubject)
                }
            }

            itemList.add(newItemInstance().apply { blankDivider = 12F })
            return
        }

        for (i in 0 until data.size) {
            val game = data[i]
            game.sequence = i
            game.outerSequence = blockPosition

            // 第一个普通游戏顶部需要增加半高的填充间距
            if (i == 0) {
                itemList.add(newItemInstance().apply { blankDivider = BlankDividerViewHolder.DEFAULT_BLANK_HEIGHT })
            }

            if (!game.image.isNullOrEmpty()) continue

            game.subjectData = GameSubjectData(
                id = subjectEntity.id,
                name = subjectEntity.name,
                tag = subjectEntity.tag,
                position = i + if (data[0].image.isNullOrEmpty()) 1 else 0,
                isOrder = subjectEntity.isOrder,
                briefStyle = subjectEntity.briefStyle,
                isShowSuffix = subjectEntity.showSuffix
            )
            val itemDataGame = newItemInstance()
            itemDataGame.blockPosition = blockPosition + 1
            itemDataGame.game = game
            itemList.add(itemDataGame)
            addGamePositionAndPackage(game)

            if (i == data.size - 1) {
                itemList.add(newItemInstance().apply { blankDivider = BlankDividerViewHolder.DEFAULT_BLANK_HEIGHT })
            }
        }
    }

    private fun appendAdditionalInfoToSubjectGame(subject: SubjectEntity, outerPosition: Int) {
        subject.outerSequence = outerPosition
        subject.data?.let {
            for ((index, game) in it.withIndex()) {
                if (subject.tag != "test") {
                    game.subjectId = subject.id
                }
                game.sequence = index
                game.outerSequence = outerPosition
                game.isAdData = subject.adIconActive
            }
        }
    }

    /**
     * 获取填充间距的空白 item
     */
    fun getBlankSpacingItem(legacyHomeItemData: LegacyHomeItemData) =
        legacyHomeItemData.apply { blankDivider = BlankDividerViewHolder.DEFAULT_BLANK_HEIGHT * 2 }

}