package com.gh.gamecenter.feature.entity

import android.preference.PreferenceManager
import com.gh.gamecenter.common.constant.Config
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.feature.HaloApp
import com.gh.gamecenter.feature.exposure.ExposureEvent
import com.google.gson.annotations.SerializedName

class SettingsEntity(
    var download: List<Download>? = listOf(),
    var suggestion: Suggestion? = null,
    var search: Search? = null,
    var community: CommunityEntity? = null,
    var image: Image? = null,
    var support: Support? = null,
    @SerializedName("community_entrance")
    private var communityEntrance: String? = "",
    @SerializedName("game_comment_blacklist")
    var gameCommentBlackList: List<String>? = listOf(),
    @SerializedName("game_download_blacklist")
    var gameDownloadBlackList: List<String> = listOf(),
    @SerializedName("game_package_match")
    var gamePackageMatch: List<GameWithPackages>? = listOf(),
    @SerializedName("ad")
    var adList: List<AD>? = listOf(),
    @SerializedName("appointment_success")
    var appointment: Appointment? = Appointment(),
    @SerializedName("article_entrance")
    private var articleEntrance: String = "",
    @SerializedName("video_advertisement")
    var videoAdvertisement: VideoAds? = null, //新增字段video_advertisement,可能为空，代表广告关闭
    @SerializedName("permission_popup_switch")
    var permissionPopupSwitch: String = "off",//权限引导弹窗开关，on/off
    @SerializedName("permission_popup_applied_versions")
    var permissionPopupAppliedVersions: PermissionPopupAppliedVersions = PermissionPopupAppliedVersions(),
    @SerializedName("game_smooth")
    var gameSmooth: String = "off", //  畅玩功能，on/off，默认off
    @SerializedName("webview_download_url")
    var webviewDownloadUrl: String = "",//webview下载地址
) {

    fun setCommunityEntrance(communityEntrance: String) {
        this.communityEntrance = communityEntrance
    }

    fun showCommunityEntrance(): Boolean {
        val preferences =
            PreferenceManager.getDefaultSharedPreferences(HaloApp.getInstance())
        val isFixCommunity =
            preferences.getBoolean(Config.FIX_COMMUNITY_KEY, false)
        if (isFixCommunity) return true

        if ("on" == communityEntrance) {
            preferences.edit().putBoolean(Config.FIX_COMMUNITY_KEY, true).apply()
            return true
        }
        return false
    }

    fun setArticleEntrance(articleEntrance: String) {
        this.articleEntrance = articleEntrance
    }

    fun showArticleEntrance(): Boolean {
        val preferences =
            PreferenceManager.getDefaultSharedPreferences(HaloApp.getInstance())
        val isFixArticle =
            preferences.getBoolean(Config.FIX_ARTICLE_KEY, false)
        if (isFixArticle) return true

        if ("on" == articleEntrance) {
            preferences.edit().putBoolean(Config.FIX_ARTICLE_KEY, true).apply()
            return true
        }
        return false
    }

    class Support(
        var qq: String? = "",
        @SerializedName("qq-group")
        var qQun: String? = "",
        @SerializedName("qq-group-key")
        var qQunKey: String? = ""
    )

    class Oss(
        var jpeg: String? = "",
        var gif: String? = "",
        var webp: String? = "",
        @SerializedName("gif_thumb")
        var gitThumb: String? = "",
        @SerializedName("gif_watermark")
        var gifWaterMark: String? = ""
    )

    class Image(
        @SerializedName("upload_limit_size")
        var uploadLimitSize: Long = 0,
        @SerializedName("process_limit_size")
        var processLimitSize: Long = 0,
        var size: Int = 0,
        var quality: Int = 0,
        var ratio: Int = 0,
        var oss: Oss? = null
    )

    class Search(
        @SerializedName("default")
        var defaultData: List<String>? = listOf(),
        @SerializedName("remen")
        var hotSearch: List<HotSearch>? = listOf(),
        @SerializedName("tag")
        var hotTag: List<HotTagEntity>? = listOf(),
        @SerializedName("hot_list")
        var rankList: List<RankList>? = listOf()
    ) {
        class RankList(
            var title: String = "",
            var content: ArrayList<RankContent> = arrayListOf(),
            var isShowIcon: Boolean = false // 榜单Item是否显示图标（所有内容都配置了图标前端才可显示，否则均不显示图标）
        ) {
            class RankContent(
                @SerializedName("_id")
                var id: String = "",
                var name: String = "",
                var icon: String = "",
                var title: String = "",
                var order: Int = 0,
                @SerializedName("recommend_type")
                var recommendType: String = "",
                var link: LinkEntity = LinkEntity(),
                //link.type等于game时，字段存在
                @SerializedName("link_game")
                var linkGame: SimpleGame? = null
            ) {
                var exposureEvent: ExposureEvent? = null
            }
        }
    }

    class Suggestion(
        var plugin: List<String>? = listOf(),
        var game: List<String>? = listOf(),
        var libao: List<String>? = listOf(),
        var service: List<String>? = listOf(),
        var report: List<String>? = listOf(),
        @SerializedName("online_game")
        var onlineGame: List<String>? = listOf(),
        @SerializedName("local_game")
        var localGame: List<String>? = listOf(),
        @SerializedName("welfare_game")
        var welfareGame: List<String>? = listOf(),
        @SerializedName("gjlocal_game")
        var internationalLocalGame: List<String>? = listOf(),
        @SerializedName("gjonline_game")
        var internationalOnlineGame: List<String>? = listOf(),
        var video: List<String>? = listOf()
    )

    class Download(
        var game: String? = "",
        var policy: String? = "",
        var pluginfy: Boolean = false,
        var time: TimeEntity? = null
    ) {
        class TimeEntity {
            var start: Long = 0
            var end: Long = 0
        }
    }

    class AD(
        var location: String? = "",
        var image: String? = "",
        var title: String? = "",
        var type: String? = "",
        var text: String? = "",
        var link: String? = "",
        @SerializedName("link_community", alternate = ["community"])
        var community: CommunityEntity? = CommunityEntity()
    ) {
        fun toLinkEntity(): LinkEntity {
            return LinkEntity(
                title = title,
                image = image,
                link = link,
                type = type,
                text = text,
                community = community
            )
        }
    }

    class GameWithPackages(
        @SerializedName("game_id")
        var gameId: String? = "",
        var packages: List<String>? = listOf()
    )

    class HotSearch(
        @SerializedName("_id")
        var id: String,
        var name: String,
        val icon: String,
        @SerializedName("name_suffix")
        var nameSuffix: String? = null,
        @SerializedName("ori_icon")
        var rawIcon: String? = null,
        @SerializedName("icon_subscript")
        var iconSubscript: String? = null,
        @SerializedName("recommend_type")
        var recommendType: String? = "none",//none:无、hot: 热门、new: 上新、surge: 飙升、update: 更新
        var type: String? = ""//论坛类型
    ) {
        var exposureEvent: ExposureEvent? = null

        fun toGameEntity(): GameEntity {
            return GameEntity(id = id, name = name)
        }

        fun toRankContent(): Search.RankList.RankContent {
            return Search.RankList.RankContent(
                id = id,
                title = name,
                name = name,
                recommendType = recommendType ?: "",
                link = LinkEntity(
                    type = "bbs_detail",
                    link = id,
                    community = CommunityEntity(id = id, type = type ?: "")
                )
            )
        }

    }

    class Appointment(
        @SerializedName("with_mobile")
        var withMobile: DialogConfig = DialogConfig(),
        @SerializedName("without_mobile")
        var withoutMobile: DialogConfig = DialogConfig()
    ) {
        class DialogConfig {
            @SerializedName("html_content")
            var htmlContent: String = ""
            var text: String = ""
            var type: String = ""
            var link: String = ""

            fun toLinkEntity(): LinkEntity {
                return LinkEntity(title = "", image = "", link = link, type = type, text = "")
            }
        }
    }

    class VideoAds(
        var center: ArrayList<Advertisement>? = null,
        var left: ArrayList<Advertisement>? = null,
        var right: ArrayList<Advertisement>? = null
    )

    class Advertisement(
        var duration: Int = -1, //广告总显示秒数
        var frequency: Int = -1,//广告出现频率
        var image: String = "",
        var link: String = "",
        @SerializedName("link_community")
        var linkCommunity: CommunityEntity? = null,
        @SerializedName("link_type")
        var linkType: String = "",
        var title: String = "",
        var type: String = "", //广告类型。revolving走马灯，image图片
        @SerializedName("link_text")
        var text: String = "",
        var display: Display? = Display()
    )

    class PermissionPopupAppliedVersions(
        val install: ArrayList<String> = arrayListOf(),
        @SerializedName("xapk_unzip")
        val xapkUnzip: ArrayList<String> = arrayListOf()
    )

}