package com.gh.common.provider

import android.content.Context
import com.alibaba.android.arouter.facade.annotation.Route
import com.gh.common.filter.RegionSettingHelper
import com.gh.common.util.ApkActiveUtils
import com.gh.download.DownloadManager
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.common.constant.RouteConsts
import com.gh.gamecenter.common.utils.countOccurrences
import com.gh.gamecenter.core.provider.IHandleGameResponseProvider
import com.gh.gamecenter.entity.GameEntity

@Route(path = RouteConsts.provider.handleGameResponse, name = "处理游戏类型返回")
class HandleGameResponseProviderImpl : IHandleGameResponseProvider {

    override fun handleGameResponse(response: List<Any>, entrance: String): List<Any> {
        // 针对游戏的一些操作(过滤隐藏游戏，过滤隐藏APK吗，增加下载数据)
        if (response.isNotEmpty() && response[0] is GameEntity) {
            val responseResult = RegionSettingHelper.filterGame(response as? List<GameEntity>?)
            if (responseResult.isNotEmpty()) {
                for (entity in responseResult) {
                    val game: GameEntity = entity
                    game.setEntryMap(DownloadManager.getInstance().getEntryMap(game.name))

                    // 当页面是由启动弹窗过来的第一或第二个页面的时候添加启动弹窗信息
                    if (entrance.contains(EntranceConsts.ENTRANCE_WELCOME)
                        && entrance.countOccurrences("+") <= 1
                    ) {
                        game.setWelcomeDialogInfoIfAvailable()
                    }
                    ApkActiveUtils.filterHideApk(game)
                }
            }
            return responseResult
        }
        return response
    }

    override fun init(context: Context?) {
        // Do nothing
    }

}