package com.gh.gamecenter.minigame.wechat

import com.gh.common.constant.Config
import com.gh.gamecenter.common.constant.Constants
import com.gh.gamecenter.common.exposure.meta.MetaUtil
import com.gh.gamecenter.common.utils.toRequestBody
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.feature.entity.TagStyleEntity
import com.gh.gamecenter.retrofit.RetrofitManager
import com.gh.gamecenter.feature.retrofit.WGameCPMApiService
import io.reactivex.Single

class WGameSubjectCPMRemoteDataSource(
    private val api: WGameCPMApiService = RetrofitManager.getInstance().wGameCPMApi
) {

    fun getEditorRecommendCPMList(
        page: Int,
        pageSize: Int = 10,
        minimumSize: Int,
        onlyFee: Boolean
    ): Single<MutableList<GameEntity>> {
        val meta = MetaUtil.getMeta()
        val request = mapOf(
            "head" to mapOf(
                "busiAppid" to Config.WGAME_CPM_BUSIAPPID,
                "oaid" to (meta.oaid ?: ""),
                "manufacturer" to (meta.manufacturer ?: ""),
                "mode" to (meta.model ?: ""),
                "androidId" to (MetaUtil.getAndroidId()),
                "imei" to (MetaUtil.getIMEI())
            ),
            "body" to mapOf(
                "page" to page - 1,
                "pageSize" to pageSize,
                "onlyFee" to if (onlyFee) 1 else 0,
            )
        )
        return api.getEditorRecommendList(request.toRequestBody())
            .map {
                if (it.ret == 0) {
                    // 数量不满足最小值时直接返回空
                    if (onlyFee && minimumSize > 0 && it.appInfoList.size < minimumSize) {
                        mutableListOf()
                    } else {
                        it.appInfoList.map { info ->
                            GameEntity(
                                mName = info.appName,
                                mIcon = info.logo,
                                mBrief = info.briefIntro,
                                miniGameUid = info.appID,
                                miniGameAppId = info.userName,
                                miniGameCategory = Constants.WECHAT_MINI_GAME,
                                profit = Constants.WECHAT_MINI_GAME_PROFIT_CPM,
                                miniGameAppStatus = 2,
                                miniGameAppPath = info.wechatAppPath,
                                miniGameExtData = info.extData,
                                miniGameRecommendId = info.recommendID,
                                mTagStyle = arrayListOf(
                                    TagStyleEntity(
                                        name = info.categoryName,
                                        color = TAG_COLOR,
                                        background = TAG_BACKGROUND
                                    ),
                                    TagStyleEntity(
                                        name = info.subcategoryName,
                                        color = TAG_COLOR,
                                        background = TAG_BACKGROUND
                                    )
                                )
                            )
                        }.toMutableList()
                    }
                } else {
                    mutableListOf()
                }
            }
    }

    fun getUserRecommendCPMList(page: Int = 1, pageSize: Int = 10): Single<MutableList<GameEntity>> {
        val meta = MetaUtil.getMeta()
        val request = mapOf(
            "head" to mapOf(
                "busiAppid" to Config.WGAME_CPM_BUSIAPPID,
                "oaid" to (meta.oaid ?: ""),
                "manufacturer" to (meta.manufacturer ?: ""),
                "mode" to (meta.model ?: ""),
                "androidId" to (MetaUtil.getAndroidId()),
                "imei" to (MetaUtil.getIMEI())
            ),
            "body" to mapOf(
                "page" to page - 1,
                "pageSize" to pageSize,
            )
        )
        return api.getUserRecommendList(request.toRequestBody())
            .map {
                if (it.ret == 0) {
                    it.appInfoList.map { info ->
                        GameEntity(
                            mName = info.appName,
                            mIcon = info.logo,
                            mBrief = info.briefIntro,
                            miniGameUid = info.appID,
                            miniGameAppId = info.userName,
                            miniGameCategory = Constants.WECHAT_MINI_GAME,
                            profit = Constants.WECHAT_MINI_GAME_PROFIT_CPM,
                            miniGameAppStatus = 2,
                            miniGameAppPath = info.wechatAppPath,
                            miniGameExtData = info.extData,
                            miniGameRecommendId = info.recommendID,
                            mTagStyle = arrayListOf(
                                TagStyleEntity(name = info.categoryName),
                                TagStyleEntity(name = info.subcategoryName)
                            )
                        )
                    }.toMutableList()
                } else {
                    mutableListOf()
                }
            }
    }

    companion object {

        private const val TAG_COLOR = "999999"
        private const val TAG_BACKGROUND = "F5F5F5"
    }
}