package com.gh.gamecenter.home.custom.model

import com.gh.common.filter.RegionSettingHelper
import com.gh.gamecenter.common.entity.CommunityEntity
import com.gh.gamecenter.common.entity.Display
import com.gh.gamecenter.common.entity.LinkEntity
import com.gh.gamecenter.common.entity.PKEntity
import com.gh.gamecenter.entity.*
import com.gh.gamecenter.feature.entity.FloatingWindowEntity
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.feature.exposure.ExposureEvent
import com.google.gson.annotations.SerializedName
import kotlinx.parcelize.IgnoredOnParcel

class CustomPageData(
    @SerializedName("title")
    private val _title: String? = null,
    @SerializedName("page_switch")
    private val _pageSwitch: PageSwitch? = null,
    @SerializedName("link_suspended_window")
    val linkSuspendedWindow: FloatingWindowEntity? = null,
    @SerializedName("link_pull_down_push")
    val linkPullDownPush: PullDownPush? = null,
    @SerializedName("link_custom_page")
    private val _customsComponents: List<CustomsComponent>? = null
) {

    val title: String
        get() = _title ?: ""

    val pageSwitch: PageSwitch
        get() = _pageSwitch ?: PageSwitch()

    val customsComponents: List<CustomsComponent>
        get() = _customsComponents ?: emptyList()


    data class PageSwitch(
        @SerializedName("top_search")
        private val _topSearch: String? = null,// 顶部搜索类型（halo_game：光环游戏、mini_game：小游戏（低于v5.36版本返回qq_mini_game）、bbs：论坛、空字符串：不显示）
        @SerializedName("pull_down_push")
        private val _pullDownPush: String? = null,
        @SerializedName("suspended_window")
        private val _suspendedWindow: String? = null
    ) {

        val topSearch: String
            get() = _topSearch ?: ""

        val pullDownPush: Boolean
            get() = _pullDownPush == "on"

        val suspendedWindow: Boolean
            get() = _suspendedWindow == "on"
    }

    data class CustomsComponent(
        @SerializedName("link")
        private val _link: LinkEntity? = null,
        @SerializedName("link_column")
        val linkColumn: SubjectEntity? = null,
        @SerializedName("link_game")
        val game: GameEntity? = null,
        @SerializedName("link_column_collection")
        val linkColumnCollection: LinkColumnCollection? = null,
        @SerializedName("link_game_list_collection")
        val linkGameListCollection: LinkColumnCollection? = null,
        @SerializedName("link_content_card")
        val contentCard: ContentCard? = null,
        @SerializedName("link_column_test_v2")
        val homeItemTestV2: HomeItemTestV2Entity? = null,
        @SerializedName("link_top_game_comment")
        val linkTopGameComments: List<AmwayCommentEntity>? = null,
        @SerializedName("link_qq_mini_game_column_detail")
        val qqMiniGameColumn: SubjectEntity? = null,
        @SerializedName("link_wechat_game_column_detail")
        val wechatMiniGameColumn: SubjectEntity? = null,
        @SerializedName("link_wechat_game_cpm_column_detail")
        val wechatMiniGameCPMColumn: SubjectEntity? = null,
        @SerializedName("link_common_collection")
        val linkCommonCollection: CommonContentCollection? = null,
        @SerializedName("link_qq_game_recently_played")
        val linkQqGameRecentlyPlayed: LinkRecentlyPlayed? = null,
        @SerializedName("link_accelerator_recently_played")
        val linkAcceleratorRecentlyPlayed: LinkRecentlyPlayed? = null,
        @SerializedName("link_dsp_game_column")
        val linkDspGameColumn: SubjectEntity? = null,
        var linkPK: PKEntity? = null,
    ) {
        // 游戏专题
        val gameSubjectEntity: SubjectEntity?
            get() = when (link.type) {
                CustomPageItem.CUSTOM_LINK_TYPE_QQ_MINI_GAME_COLUMN_DETAIL -> qqMiniGameColumn
                CustomPageItem.CUSTOM_LINK_TYPE_WECHAT_MINI_GAME_COLUMN_DETAIL -> wechatMiniGameColumn
                CustomPageItem.CUSTOM_LINK_TYPE_WECHAT_WECHAT_GAME_CPM_COLUMN_DETAIL -> wechatMiniGameCPMColumn
                else -> linkColumn
            }

        val link: LinkEntity
            get() = _link ?: LinkEntity()
    }

    data class LinkColumn(
        @SerializedName("_id")
        private val _id: String? = null,
        @SerializedName("name")
        private val _name: String? = null,
        @SerializedName("show_name")
        private val _showName: Boolean? = null,
        @SerializedName("show_star")
        private val _showStar: Boolean? = null,
        @SerializedName("show_download")
        private val _showDownload: Boolean? = null,
        @SerializedName("order")
        private val _order: Boolean? = null,
        @SerializedName("more")
        private val _more: Int? = null,
        @SerializedName("list")
        private val _list: Int? = null
    )

    data class LinkColumnCollection(
        @SerializedName("_id")
        private val _id: String? = null,
        @SerializedName("name")
        private val _name: String? = null,
        @SerializedName("style")
        private val _style: String? = null,
        @SerializedName("style_setting")
        private val _styleSetting: StyleSetting? = null,
        @SerializedName("data")
        private var _data: List<CustomSubjectEntity>? = null,
        @SerializedName("game_platform")
        private val _gamePlatform: String? = null,
        @SerializedName("explain")
        private val _explain: String? = null
    ) {

        val id: String
            get() = _id ?: ""

        val name: String
            get() = _name ?: ""

        val style: String
            get() = _style ?: ""

        val styleSetting: StyleSetting
            get() = _styleSetting ?: StyleSetting()


        var data: List<CustomSubjectEntity>
            get() = _data ?: emptyList()
            set(value) {
                _data = value
            }

        val gamePlatform: String
            get() = _gamePlatform ?: ""

        val isQqMini: Boolean
            get() = gamePlatform == "qq_mini"

        val isWechatMini: Boolean
            get() = gamePlatform == "wechat_mini"

        val subjectType: SubjectData.SubjectType
            get() = when {
                isQqMini -> SubjectData.SubjectType.QQ_GAME
                isWechatMini -> SubjectData.SubjectType.WECHAT_GAME
                else -> SubjectData.SubjectType.NORMAL
            }

        val explain: String
            get() = _explain ?: ""

        data class StyleSetting(
            @SerializedName("size")
            private val _size: Int? = null,
            @SerializedName("hide_name")
            private val _hideName: Boolean? = null,
            @SerializedName("right_top")
            private val _rightTop: String? = null,
            @SerializedName("right_top_link")
            private val _rightTopLink: LinkEntity? = null,
            @SerializedName("rows_num")
            private val _rowsNum: Int? = null
        ) {

            val size: Int
                get() = _size ?: 0

            val hideName: Boolean
                get() = _hideName ?: false

            val rightTop: String
                get() = _rightTop ?: ""

            val rightTopLink: LinkEntity
                get() = _rightTopLink ?: LinkEntity()

            val rowsNum: Int
                get() = _rowsNum ?: 0
        }

        /**
         * 此实体包含了旧的专题合集与游戏单的所有字段
         */
        data class CustomSubjectEntity(
            @SerializedName("_id")
            private val _id: String? = null,
            @SerializedName("_seq")
            private val _seq: Int? = null,
            @SerializedName("image")
            private val _image: String? = null,
            @SerializedName("title")
            private val _title: String? = null,
            @SerializedName("intro")
            private val _intro: String? = null,
            @SerializedName("ad_icon_active")
            private val _adIconActive: Boolean? = null,
            @SerializedName("games")
            private var _games: List<GameEntity>? = null,
            @SerializedName("user")
            private val _user: User? = null,
            @SerializedName("count")
            private val _count: Count? = null,
            @SerializedName("time")
            private val _time: Long? = null,
            @SerializedName("stamp")
            private val _stamp: String? = null,
            @SerializedName("style")
            private val _style: String? = null,
            @SerializedName("tags")
            private val _tags: List<TagInfoEntity>? = null
        ) {

            val id: String
                get() = _id ?: ""

            val seq: Int
                get() = _seq ?: 0

            val image: String
                get() = _image ?: ""

            val title: String
                get() = _title ?: ""

            val intro: String
                get() = _intro ?: ""

            val adIconActive: Boolean
                get() = _adIconActive ?: false

            var games: List<GameEntity>
                get() = filteredGames ?: RegionSettingHelper.filterGame(_games).also { filteredGames = it }
                set(value) {
                    _games = RegionSettingHelper.filterGame(value).also { filteredGames = it }
                }

            val user: User
                get() = _user ?: User()

            val count: Count
                get() = _count ?: Count()

            val time: Long
                get() = _time ?: 0

            val stamp: String
                get() = _stamp ?: ""

            val style: String
                get() = _style ?: ""

            @IgnoredOnParcel
            val styleChinese: String
                get() = CustomPageItem.subjectTypeToComponentStyle[style] ?: ""

            @IgnoredOnParcel
            private var filteredGames: List<GameEntity>? = null

            val tags: List<TagInfoEntity>
                get() = _tags ?: emptyList()

            data class User(
                @SerializedName("_id")
                private val _id: String? = null,
                @SerializedName("name")
                private val _name: String? = null,
                @SerializedName("icon")
                private val _icon: String? = null
            ) {

                val id: String
                    get() = _id ?: ""

                val name: String
                    get() = _name ?: ""

                val icon: String
                    get() = _icon ?: ""

                val isValid: Boolean
                    get() = id.isNotBlank()
            }

            data class Count(
                @SerializedName("vote")
                private val _vote: Int? = null,
                @SerializedName("hot")
                private val _hot: Int? = null,
                @SerializedName("favorite")
                private val _favorite: Int? = null,
                @SerializedName("share")
                private val _share: Int? = null,
                @SerializedName("comment")
                private val _comment: Int? = null,
                @SerializedName("vote_comment")
                private val _voteComment: Int? = null,
                @SerializedName("real_hot")
                private val _realHot: Int? = null,
                @SerializedName("game")
                private val _game: Int? = null,
                @SerializedName("download")
                private val _download: Int? = null,
                @SerializedName("game_played")
                private val _gamePlayed: Int? = null
            ) {

                val vote: Int
                    get() = _vote ?: 0

                val hot: Int
                    get() = _hot ?: 0

                val favorite: Int
                    get() = _favorite ?: 0

                val share: Int
                    get() = _share ?: 0

                val comment: Int
                    get() = _comment ?: 0

                val voteComment: Int
                    get() = _voteComment ?: 0

                val realHot: Int
                    get() = _realHot ?: 0

                val game: Int
                    get() = _game ?: 0

                val download: Int
                    get() = _download ?: 0

                val gamePlayed: Int
                    get() = _gamePlayed ?: 0
            }
        }
    }

    data class ContentCard(
        @SerializedName("img")
        private val _img: String? = null,
        @SerializedName("first_line_recommend")
        private val _firstLineRecommend: String? = null,
        @SerializedName("second_line_recommend")
        private val _secondLineRecommend: String? = null,
        @SerializedName("recommend_tag")
        private val _recommendTag: String? = null
    ) {
        val img: String
            get() = _img ?: ""

        val firstLineRecommend: String
            get() = _firstLineRecommend ?: ""

        val secondLineRecommend: String
            get() = _secondLineRecommend ?: ""

        val recommendTag: String
            get() = _recommendTag ?: ""
    }

    data class CommonContentCollection(
        @SerializedName("_id")
        private val _id: String? = null,
        @SerializedName("name")
        private var _name: String? = null,
        @SerializedName("subtitle")
        private val _subtitle: String? = null,
        @SerializedName("detail_style")
        private val _detailStyle: String? = null,
        @SerializedName("layout")
        private val _layout: Int? = null,
        @SerializedName("primary_topic_count")
        private val _primaryTopicCount: Int? = null,
        @SerializedName("top_right_text")
        private val _topRightText: String? = null,
        @SerializedName("vertical_line")
        private val _verticalLine: String? = null,
        @SerializedName("slides")
        private val _slides: Slides? = null,
        @SerializedName("data")
        private val _data: List<CommonCollectionContentEntity>? = null,
        @SerializedName("navigations")
        private val _navigations: List<GameNavigationEntity>? = null,
        @SerializedName("recommends")
        private val _recommends: List<HomeRecommend>? = null,
        @SerializedName("content_tags")
        private val _contentTags: List<ContentTag>? = null,
        @SerializedName("notifies")
        private var _notifies: MutableList<Notify>? = null,
        @SerializedName("announcements")
        private var _announcements: List<Announcement>? = null,
        @SerializedName("recommend_cards")
        private var _recommendCards: List<RecommendCard>? = null
    ) {

        val id: String
            get() = _id ?: ""

        var name: String
            get() = _name ?: ""
            set(value) {
                _name = value
            }

        val subtitle: String
            get() = _subtitle ?: ""

        val detailStyle: String
            get() = _detailStyle ?: ""

        val layout: Int
            get() = _layout ?: 0

        val layoutChinese: String
            get() = when (layout) {
                0 -> "轮播banner"
                1 -> "导航栏"
                2 -> "金刚区"
                3 -> "横向滑动banner"
                4 -> "双列banner"
                5 -> "横排竖式卡片"
                6 -> "双列竖式卡片"
                7 -> "竖式图文列表"
                8 -> "横排图文列表"
                9 -> "内容标签泳道"
                10 -> "通知栏目"
                11 -> "公告横幅"
                12 -> "推荐卡片"
                else -> ""
            }

        val primaryTopicCount: Int
            get() = _primaryTopicCount ?: 0

        val topRightText: String
            get() = _topRightText ?: ""

        val verticalLine: String
            get() = _verticalLine ?: ""

        val slides: Slides
            get() = _slides ?: Slides()

        val data: List<CommonCollectionContentEntity>
            get() = _data ?: emptyList()

        val navigations: List<GameNavigationEntity>
            get() = _navigations ?: emptyList()

        val recommends: List<HomeRecommend>
            get() = _recommends ?: emptyList()

        val contentTags: List<ContentTag>
            get() = _contentTags ?: emptyList()

        var notifies: MutableList<Notify>
            get() = _notifies ?: mutableListOf()
            set(value) {
                _notifies = value
            }

        val announcements: List<Announcement>
            get() = _announcements ?: emptyList()

        val recommendCards: List<RecommendCard>
            get() = _recommendCards ?: emptyList()

        data class Slides(
            @SerializedName("slide")
            private val _slide: List<HomeSlide>? = null,
            @SerializedName("sub_slide")
            private val _subSlide: List<HomeSubSlide>? = null
        ) {

            val slide: List<HomeSlide>
                get() = _slide ?: emptyList()

            val subSlide: List<HomeSubSlide>
                get() = _subSlide ?: emptyList()
        }


        data class ContentTag(
            @SerializedName("_id")
            private val _id: String? = null,
            @SerializedName("title")
            private val _title: String? = null,
            @SerializedName("image")
            private val _image: String? = null,
            @SerializedName("added_content")
            private val _addedContent: String? = null,
            @SerializedName("guide")
            private val _guide: Guide? = null,
            @SerializedName("link_id")
            private val _linkId: String? = null, // 链接地址
            @SerializedName("link_type")
            private val _linkType: String? = null,
            @SerializedName("link_text")
            private val _linkText: String? = null,
            @SerializedName("link_community")
            private val _linkCommunity: CommunityEntity? = null,
            @SerializedName("display")
            private val _display: Display? = null
        ) {

            val id: String
                get() = _id ?: ""

            val title: String
                get() = _title ?: ""

            val image: String
                get() = _image ?: ""

            val addedContent: String
                get() = _addedContent ?: ""

            val isShowGuide: Boolean
                get() = guide.text.isNotBlank()

            val guide: Guide
                get() = _guide ?: Guide()

            val linkId: String
                get() = _linkId ?: ""

            val linkType: String
                get() = _linkType ?: ""

            val linkText: String
                get() = _linkText ?: ""

            val link: LinkEntity
                get() = LinkEntity(
                    name = title,
                    type = linkType,
                    display = _display,
                    link = linkId,
                    text = linkText,
                    community = _linkCommunity
                )

            var exposureEvent: ExposureEvent? = null

            data class Guide(
                @SerializedName("text")
                private val _text: String? = null
            ) {

                val text: String
                    get() = _text ?: ""
            }
        }

    }

    data class Notify(
        @SerializedName("_id")
        private val _id: String? = null,
        @SerializedName("title")
        private val _title: String? = null,
        @SerializedName("image")
        private val _image: String? = null,
        @SerializedName("added_content")
        private val _addedContent: String? = null,
        @SerializedName("link_type")
        private val _linkType: String? = null,
        @SerializedName("link_id")
        private val _linkId: String? = null,
        @SerializedName("link_text")
        private val _linkText: String? = null,
        @SerializedName("link_community")
        private val _linkCommunity: CommunityEntity? = null,
        @SerializedName("display")
        private val _display: Display? = null,
        @SerializedName("display_status")
        private val _displayStatus: String? = null
    ) {
        val id: String
            get() = _id ?: ""

        val title: String
            get() = _title ?: ""

        val image: String
            get() = _image ?: ""

        val addedContent: String
            get() = _addedContent ?: ""

        val linkType: String
            get() = _linkType ?: ""

        val linkId: String
            get() = _linkId ?: ""

        val linkText: String
            get() = _linkText ?: ""

        val isClosable: Boolean
            get() = _displayStatus == "closable"

        val link: LinkEntity
            get() = LinkEntity(
                name = title,
                type = linkType,
                display = _display,
                link = linkId,
                text = linkText,
                community = _linkCommunity
            )

        var exposureEvent: ExposureEvent? = null
    }

    data class Announcement(
        @SerializedName("_id")
        private val _id: String? = null,
        @SerializedName("image")
        private val _image: String? = null,
        @SerializedName("link_type")
        private val _linkType: String? = null,
        @SerializedName("link_id")
        private val _linkId: String? = null,
        @SerializedName("link_text")
        private val _linkText: String? = null,
        @SerializedName("link_community")
        private val _linkCommunity: CommunityEntity? = null,
        @SerializedName("display")
        private val _display: Display? = null
    ) {
        val id: String
            get() = _id ?: ""

        val image: String
            get() = _image ?: ""

        val linkType: String
            get() = _linkType ?: ""

        val linkId: String
            get() = _linkId ?: ""

        val linkText: String
            get() = _linkText ?: ""

        val link: LinkEntity
            get() = LinkEntity(
                name = "",
                type = linkType,
                display = _display,
                link = linkId,
                text = linkText,
                community = _linkCommunity
            )

        var exposureEvent: ExposureEvent? = null
    }

    data class LinkRecentlyPlayed(
        @SerializedName("home")
        private val _home: String? = null,
        @SerializedName("more_link")
        val moreLink: LinkEntity? = null
    ) {

        val home: String
            get() = _home ?: ""
    }

    data class RecommendCard(
        @SerializedName("_id")
        private val _id: String? = null,
        @SerializedName("title")
        private val _title: String? = null,
        @SerializedName("image")
        private val _image: String? = null,
        @SerializedName("tag")
        private val _tag: String? = null,
        @SerializedName("highlight")
        private val _highlight: TextWithSymbol? = null,
        @SerializedName("deletion")
        private val _deletion: TextWithSymbol? = null,
        @SerializedName("added_content")
        private val _addedContent: String? = null,
        @SerializedName("link_id")
        private val _linkId: String? = null,
        @SerializedName("link_type")
        private val _linkType: String? = null,
        @SerializedName("link_text")
        private val _linkText: String? = null,
        @SerializedName("link_community")
        private val _linkCommunity: CommunityEntity? = null,
        @SerializedName("display")
        private val _display: Display? = null
    ) {

        val id: String
            get() = _id ?: ""

        val title: String
            get() = _title ?: ""

        val image: String
            get() = _image ?: ""

        val tag: String
            get() = _tag ?: ""

        val highlight: TextWithSymbol
            get() = _highlight ?: TextWithSymbol()

        val deletion: TextWithSymbol
            get() = _deletion ?: TextWithSymbol()

        val addedContent: String
            get() = _addedContent ?: ""

        val linkId: String
            get() = _linkId ?: ""

        val linkType: String
            get() = _linkType ?: ""

        val linkText: String
            get() = _linkText ?: ""

        val link: LinkEntity
            get() = LinkEntity(
                name = title,
                type = linkType,
                display = _display,
                link = linkId,
                text = linkText,
                community = _linkCommunity
            )

        var exposureEvent: ExposureEvent? = null

        data class TextWithSymbol(
            @SerializedName("text")
            private val _text: String? = null,
            @SerializedName("is_show_currency_symbol")
            private val _isShowCurrencySymbol: Boolean? = null
        ) {
            val text: String
                get() = _text ?: ""

            val isShowCurrencySymbol: Boolean
                get() = _isShowCurrencySymbol ?: false
        }

    }

}