package com.gh.gamecenter.forum.home.follow

import com.gh.gamecenter.entity.FollowCommonContentCollection
import com.gh.gamecenter.entity.FollowUserEntity
import com.gh.gamecenter.entity.FollowDynamicEntity
import com.gh.gamecenter.feature.entity.*
import com.gh.gamecenter.home.custom.model.CustomPageData
import com.gh.gamecenter.home.custom.model.CustomPageItem
import com.gh.gamecenter.home.custom.model.CustomPageItem.Companion.COMMON_CONTENT_COLLECTION_LAYOUT_BANNER

abstract class FollowItem {

    abstract val itemType: Int

    fun areItemsTheSame(other: FollowItem) =
        itemType == other.itemType && doAreItemsTheSame(other)


    fun areContentsTheSame(other: FollowItem) =
        itemType == other.itemType && doAreContentsTheSame(other)

    protected open fun doAreItemsTheSame(other: FollowItem) = true

    protected open fun doAreContentsTheSame(other: FollowItem) = this == other

    companion object {

        const val FOLLOW_ITEM_TYPE_INVALID = -4
        const val FOLLOW_ITEM_TYPE_EMPTY = -3
        const val FOLLOW_ITEM_TYPE_FOOTER = -2
        const val FOLLOW_ITEM_TYPE_HEADER = -1
        const val FOLLOW_ITEM_TYPE_RECOMMEND_USER = 0
        const val FOLLOW_ITEM_TYPE_COMMON_BANNER = 1
        const val FOLLOW_ITEM_TYPE_COMMON_BANNER_WITH_CARDS = 2
        const val FOLLOW_ITEM_TYPE_NAVIGATION = 3
        const val FOLLOW_ITEM_TYPE_RECOMMEND = 4
        const val FOLLOW_ITEM_TYPE_COMMON_HORIZONTAL_SLIDE = 5
        const val FOLLOW_ITEM_TYPE_COMMON_VERTICAL_TWO_CARDS = 6
        const val FOLLOW_ITEM_TYPE_POST_CARD = 7
        const val FOLLOW_ITEM_TYPE_GIFT_PACK = 8
        const val FOLLOW_ITEM_TYPE_ARTICLE = 9

        // 通用内容合集 样式
        val commonContentCollection: HashMap<Int, Int> = hashMapOf(
            CustomPageItem.COMMON_CONTENT_COLLECTION_LAYOUT_NAVIGATION to FOLLOW_ITEM_TYPE_NAVIGATION,
            CustomPageItem.COMMON_CONTENT_COLLECTION_LAYOUT_KING_KONG to FOLLOW_ITEM_TYPE_RECOMMEND,
            CustomPageItem.COMMON_CONTENT_COLLECTION_LAYOUT_HORIZONTAL_SLIDE_BANNER to FOLLOW_ITEM_TYPE_COMMON_HORIZONTAL_SLIDE,
            CustomPageItem.COMMON_CONTENT_COLLECTION_LAYOUT_DOUBLE_ROW_BANNER to FOLLOW_ITEM_TYPE_COMMON_VERTICAL_TWO_CARDS,
            CustomPageItem.COMMON_CONTENT_COLLECTION_LAYOUT_HORIZONTAL_VERTICAL_CARD to FOLLOW_ITEM_TYPE_COMMON_HORIZONTAL_SLIDE,
            CustomPageItem.COMMON_CONTENT_COLLECTION_LAYOUT_DOUBLE_ROW_VERTICAL_CARD to FOLLOW_ITEM_TYPE_COMMON_VERTICAL_TWO_CARDS,
            CustomPageItem.COMMON_CONTENT_COLLECTION_LAYOUT_VERTICAL_IMAGE_TEXT to FOLLOW_ITEM_TYPE_COMMON_VERTICAL_TWO_CARDS,
            CustomPageItem.COMMON_CONTENT_COLLECTION_LAYOUT_HORIZONTAL_IMAGE_TEXT to FOLLOW_ITEM_TYPE_COMMON_HORIZONTAL_SLIDE
        )
    }
}

data class FollowRecommendUsersItem(
    val data: List<UserEntity>
) : FollowItem() {

    override val itemType: Int
        get() = FOLLOW_ITEM_TYPE_RECOMMEND_USER
}

class FollowCommonCollectionItem(
    val data: FollowCommonContentCollection
) : FollowItem() {
    override val itemType: Int
        get() = if (data.linkCommonCollection.layout == COMMON_CONTENT_COLLECTION_LAYOUT_BANNER) {
            // 轮播图右边卡片列表, 至少需要2个卡片才能显示
            if (data.linkCommonCollection.slides.subSlide.size < 2) {
                FOLLOW_ITEM_TYPE_COMMON_BANNER
            } else {
                FOLLOW_ITEM_TYPE_COMMON_BANNER_WITH_CARDS
            }
        } else {
            commonContentCollection[data.linkCommonCollection.layout] ?: FOLLOW_ITEM_TYPE_INVALID
        }

    override fun doAreItemsTheSame(other: FollowItem): Boolean {
        return other is FollowCommonCollectionItem
                && data.linkCommonCollection.id == other.data.linkCommonCollection.id
    }

    override fun doAreContentsTheSame(other: FollowItem): Boolean {
        return other is FollowCommonCollectionItem
                && data.title == other.data.title
                && data.linkCommonCollection == other.data.linkCommonCollection
    }
}

data class FollowSplitCommonContentCollectionItem(
    val data: CustomPageData.CommonContentCollection,
    val leftPosition: Int,
) : FollowItem() {

    override val itemType
        get() = commonContentCollection[data.layout] ?: FOLLOW_ITEM_TYPE_INVALID

    override fun doAreItemsTheSame(other: FollowItem): Boolean {
        return other is FollowSplitCommonContentCollectionItem
                && data.id == other.data.id
    }

    override fun doAreContentsTheSame(other: FollowItem): Boolean {
        return other is FollowSplitCommonContentCollectionItem
                && data == other.data
                && data.data.getOrNull(leftPosition) == other.data.data.getOrNull(leftPosition)
                && data.data.getOrNull(leftPosition + 1) == other.data.data.getOrNull(leftPosition + 1)
    }
}

data class FollowPostCardItem(
    val data: ArticleEntity
) : FollowItem() {

    override val itemType: Int
        get() = FOLLOW_ITEM_TYPE_POST_CARD

    override fun doAreItemsTheSame(other: FollowItem): Boolean {
        return other is FollowPostCardItem
                && data.id == other.data.id
    }

}

data class FollowArticleItem(
    val data: FollowDynamicEntity.Article
) : FollowItem() {

    override val itemType: Int
        get() = FOLLOW_ITEM_TYPE_ARTICLE

    override fun doAreItemsTheSame(other: FollowItem): Boolean {
        return other is FollowArticleItem
                && data.id == other.data.id
    }
}

data class FollowGiftPackItem(
    val libao: LibaoEntity,
    val time: Long
) : FollowItem() {

    override val itemType: Int
        get() = FOLLOW_ITEM_TYPE_GIFT_PACK

    override fun doAreItemsTheSame(other: FollowItem): Boolean {
        return other is FollowGiftPackItem
                && libao.id == other.libao.id
    }

    override fun doAreContentsTheSame(other: FollowItem): Boolean {
        return other is FollowGiftPackItem
                && time == other.time
                && libao.name == other.libao.name
                && libao.des == other.libao.des
                && libao.content == other.libao.content
                && libao.code == other.libao.code
                && libao.game?.mIcon == other.libao.game?.mIcon
                && libao.game?.iconFloat == other.libao.game?.iconFloat
                && libao.game?.iconSubscript == other.libao.game?.iconSubscript
                && libao.game?.name == other.libao.game?.name
    }
}

data class FollowUserItem(
    val isLogin: Boolean,
    val data: List<FollowUserEntity>
) : FollowItem() {

    override val itemType: Int
        get() = FOLLOW_ITEM_TYPE_HEADER

    override fun doAreItemsTheSame(other: FollowItem): Boolean {
        return true
    }

    override fun doAreContentsTheSame(other: FollowItem): Boolean {
        return other is FollowUserItem
                && isLogin == other.isLogin
                && data == other.data
    }

}

object FollowEmptyItem : FollowItem() {
    override val itemType: Int
        get() = FOLLOW_ITEM_TYPE_EMPTY
}

object FollowFooterItem : FollowItem() {

    override val itemType: Int
        get() = FOLLOW_ITEM_TYPE_FOOTER
}

object InvalidItem : FollowItem() {
    override val itemType: Int
        get() = FOLLOW_ITEM_TYPE_INVALID
}