package com.gh.gamecenter.feature.entity

import com.gh.gamecenter.common.constant.Constants
import com.google.gson.annotations.SerializedName

data class DspEntity(
    @SerializedName("ret")
    private val _ret: Int? = null,
    @SerializedName("bidList")
    private val _bidList: List<BidInfo>? = null
) {

    val bidList: List<BidInfo>
        get() = _bidList ?: emptyList()

    data class BidInfo(
        @SerializedName("ad_id")
        private val _adId: String? = null,
        @SerializedName("interaction_type")
        private val _interactionType: Int? = null,
        @SerializedName("nurl")
        private val _nUrl: String? = null,
        @SerializedName("lurl")
        private val _lUrl: String? = null,
        @SerializedName("price")
        private val _price: Int? = null,
        @SerializedName("show_url")
        private val _showUrl: String? = null,
        @SerializedName("click_url")
        private val _clickUrl: String? = null,
        @SerializedName("download_url")
        private val _downloadUrl: String? = null,
        @SerializedName("install_url")
        private val _installUrl: String? = null,
        @SerializedName("creative_type")
        private val _creativeType: Int? = null,
        @SerializedName("icon")
        private val _icon: String? = null,
        @SerializedName("title")
        private val _title: String? = null,
        @SerializedName("description")
        private val _description: String? = null,
        @SerializedName("image_list")
        private val _imageList: List<Image>? = null,
        @SerializedName("video")
        private val _video: Video? = null,
        @SerializedName("target_url")
        private val _targetUrl: String? = null,
        @SerializedName("deeplink_url")
        private val _deeplinkUrl: String? = null,
        @SerializedName("wechat_path")
        private val _wechatPath: String? = null,
        @SerializedName("wechat_appid")
        private val _wechatAppId: String? = null,
        @SerializedName("wechat_ext_data")
        private val _wechatExtData: String? = null,
        @SerializedName("apk_url")
        private val _apkUrl: String? = null,
        @SerializedName("app_info")
        private val _appInfo: AppInfo? = null
    ) {

        val appInfo: AppInfo
            get() = _appInfo ?: AppInfo()

        fun toGameEntity() =
            GameEntity(
                _id = _appInfo?.apkMd5 ?: _adId ?: "",
                mIcon = _icon,
                mName = _appInfo?.name,
                mBrief = _description,
                apk = arrayListOf(toGhApk()),
                mTagStyle = arrayListOf(
                    TagStyleEntity(name = appInfo.categoryName),
                ),
                developer = _appInfo?.icpEntity,
                permissionsUrl = _appInfo?.permissions,
                privacyUrl = _appInfo?.privacy,
                miniGameUid = _wechatAppId ?: "",
                miniGameAppId = _wechatAppId ?: "",
                miniGameCategory = Constants.WECHAT_MINI_GAME,
                profit = Constants.DSP_GAME,
                miniGameAppStatus = 2,
                miniGameAppPath = _wechatPath ?: "",
                miniGameExtData = _wechatExtData ?: "",
                interactionType = _interactionType ?: -1,
                showUrl = _showUrl,
                clickUrl = _clickUrl,
                downloadUrl = _downloadUrl,
                installUrl = _installUrl,
                dspAdId = _adId ?: "",
            ).apply {
                isDspGame = true
            }

        private fun toGhApk(): ApkEntity {
            val appInfo = _appInfo ?: AppInfo()
            return ApkEntity(
                packageName = appInfo.packageName,
                url = _apkUrl ?: "",
                version = appInfo.version.removePrefix("版本："),
                isActive = true,
                md5 = appInfo.apkMd5,
                versionCode = appInfo.versionCode.toIntOrNull() ?: 0,
                format = "apk"
            )
        }

        data class Image(
            @SerializedName("url")
            private val _url: String? = null,
            @SerializedName("width")
            private val _width: Int? = null,
            @SerializedName("height")
            private val _height: Int? = null
        ) {
            val url: String
                get() = _url ?: ""

            val width: Int
                get() = _width ?: 0

            val height: Int
                get() = _height ?: 0
        }

        data class Video(
            @SerializedName("url")
            private val _url: String? = null,
            @SerializedName("width")
            private val _width: Int? = null,
            @SerializedName("height")
            private val _height: Int? = null,
            @SerializedName("duration")
            private val _duration: String? = null,
            @SerializedName("cover_url")
            private val _coverUrl: String? = null,
            @SerializedName("cover_width")
            private val _coverWidth: Int? = null,
            @SerializedName("cover_height")
            private val _coverHeight: Int? = null
        )

        data class AppInfo(
            @SerializedName("name")
            private val _name: String? = null,
            @SerializedName("version")
            private val _version: String? = null,
            @SerializedName("version_code")
            private val _versionCode: String? = null,
            @SerializedName("package_name")
            private val _packageName: String? = null,
            @SerializedName("apk_md5")
            private val _apkMd5: String? = null,
            @SerializedName("developer")
            private val _developer: String? = null,
            @SerializedName("permissions")
            private val _permissions: String? = null,
            @SerializedName("privacy")
            private val _privacy: String? = null,
            @SerializedName("icp_entity")
            private val _icpEntity: String? = null,
            @SerializedName("category_name")
            private val _categoryName: String? = null
        ) {
            val name: String
                get() = _name ?: ""

            val version: String
                get() = _version ?: ""

            val versionCode: String
                get() = _versionCode ?: ""

            val packageName: String
                get() = _packageName ?: ""

            val apkMd5: String
                get() = _apkMd5 ?: ""

            val developer: String
                get() = _developer ?: ""

            val permissions: String
                get() = _permissions ?: ""

            val privacy: String
                get() = _privacy ?: ""

            val icpEntity: String
                get() = _icpEntity ?: ""

            val categoryName: String
                get() = _categoryName ?: ""
        }
    }

}