package com.gh.common.util

import android.content.Context
import android.os.Message
import android.text.TextUtils
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.collection.ArrayMap
import androidx.recyclerview.widget.RecyclerView
import com.gh.common.chain.*
import com.gh.common.constant.Config
import com.gh.common.dialog.DeviceRemindDialog
import com.gh.common.exposure.ExposureEvent
import com.gh.common.filter.RegionSettingHelper
import com.gh.common.history.HistoryHelper
import com.gh.common.repository.ReservationRepository
import com.gh.common.simulator.NewSimulatorGameManager
import com.gh.common.simulator.SimulatorDownloadManager
import com.gh.common.simulator.SimulatorGameManager
import com.gh.common.view.DownloadButton
import com.gh.common.xapk.XapkInstaller
import com.gh.common.xapk.XapkInstaller.cancelUnzipTask
import com.gh.common.xapk.XapkUnzipStatus
import com.gh.download.DownloadManager
import com.gh.download.dialog.DownloadDialog
import com.gh.gamecenter.R
import com.gh.gamecenter.WebActivity
import com.gh.gamecenter.adapter.viewholder.GameViewHolder
import com.gh.gamecenter.common.callback.CancelListener
import com.gh.gamecenter.common.constant.Constants
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.common.entity.LinkEntity
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.core.utils.*
import com.gh.gamecenter.energy.EnergyBridge
import com.gh.gamecenter.entity.GameEntity
import com.gh.gamecenter.entity.PluginLocation
import com.gh.gamecenter.gamedetail.dialog.GamePermissionDialogFragment
import com.gh.gamecenter.manager.PackagesManager
import com.gh.gamecenter.teenagermode.TeenagerModeActivity
import com.gh.vspace.VHelper
import com.lightgame.download.DownloadConfig
import com.lightgame.download.DownloadEntity
import com.lightgame.download.DownloadStatus
import com.lightgame.download.FileUtils
import com.lightgame.utils.Utils
import java.io.File
import java.util.concurrent.LinkedBlockingQueue

/**
 * todo 下载判断不能以按钮文案为判断条件,否则按钮文案修改时又要修改判断逻辑
 */
object DownloadItemUtils {
    // 更新下载进度条
    fun processDate(
        gameEntity: GameEntity,
        downloadEntity: DownloadEntity,
        adapter: RecyclerView.Adapter<out RecyclerView.ViewHolder?>?,
        index: Int
    ) {
        if (gameEntity.id != downloadEntity.gameId) {
            adapter?.notifyItemChanged(index)
            return
        }
        var queue = DownloadManager.getInstance().getQueue(downloadEntity.name)
        if (queue == null) {
            queue = LinkedBlockingQueue()
            DownloadManager.getInstance().putQueue(downloadEntity.name, queue)
        }
        val platform = downloadEntity.platform
        val entryMap: ArrayMap<String, DownloadEntity>
        val status = downloadEntity.status
        if (status == DownloadStatus.pause || status == DownloadStatus.cancel || status == DownloadStatus.done) {
            queue.remove(platform)
            entryMap = ArrayMap()
            gameEntity.setEntryMap(entryMap)
            entryMap[platform] = downloadEntity
            adapter?.notifyItemChanged(index)
        } else {
            if (!queue.contains(platform) && !TextUtils.isEmpty(platform)) {
                queue.offer(platform)
                if (AppDebugConfig.IS_DEBUG) {
                    AppDebugConfig.logMethodWithParams(
                        DownloadItemUtils::class.java,
                        queue.size,
                        gameEntity.brief,
                        downloadEntity.platform,
                        index
                    )
                }
                // 有两个平台同时下载的时候启用
                if (queue.size == 2) {
                    //TODO fuck this
                    val msg = Message.obtain()
                    msg.obj = downloadEntity.name
                    msg.what = DownloadConfig.DOWNLOAD_ROLL
                    DownloadManager.getInstance().sendMessageDelayed(msg, 3000)
                }
            }
            if (platform != null && platform == queue.peek()) {
                entryMap = ArrayMap()
                gameEntity.setEntryMap(entryMap)
                entryMap[platform] = downloadEntity
                if (DownloadStatus.pause != DownloadManager.getInstance().getStatus(downloadEntity.url)) {
                    adapter?.notifyItemChanged(index)
                }
            }
        }
    }

    // 下载按钮显示为查看，并且不提供下载功能
    fun updateItemWithViewOnlyStyle(holder: GameViewHolder) {
        holder.gameDownloadBtn.visibility = View.VISIBLE
        holder.gameDownloadBtn.text = "查看"
        holder.gameDownloadBtn.isClickable = false
    }

    // 根据预约状态更新下载按钮
    fun updateItemWithReserveStatus(holder: GameViewHolder, gameEntity: GameEntity) {
        if ("download" == gameEntity.reserveStatus) {
            // 已上线
            updateItem(holder.gameDes.context, gameEntity, holder, false)
        } else if ("appointment" == gameEntity.reserveStatus) {
            // 已预约
            holder.gameDownloadBtn.text = "已预约"
            holder.gameDownloadBtn.visibility = View.VISIBLE
            holder.gameDownloadBtn.buttonStyle = DownloadButton.ButtonStyle.RESERVED
            updateItemViewStatus(holder, null, null)
        }
    }

    fun updateItem(
        context: Context, gameEntity: GameEntity, holder: GameViewHolder,
        isShowPlatform: Boolean, hideDownloadBtnIfNoAvailableContent: Boolean
    ) {
        updateItem(
            context,
            gameEntity,
            holder,
            isShowPlatform,
            PluginLocation.only_game,
            hideDownloadBtnIfNoAvailableContent,
            null
        )
    }

    fun updateItem(
        context: Context, gameEntity: GameEntity, holder: GameViewHolder,
        isShowPlatform: Boolean, briefStyle: String?
    ) {
        updateItem(context, gameEntity, holder, isShowPlatform, PluginLocation.only_game, false, briefStyle)
    }

    @JvmStatic
    @JvmOverloads
    fun updateItem(
        context: Context,
        gameEntity: GameEntity,
        holder: GameViewHolder,
        isShowPlatform: Boolean,
        pluginLocation: PluginLocation? = PluginLocation.only_game,
        hideDownloadBtnIfNoAvailableContent: Boolean = false,
        briefStyle: String? = null,
        isShowRecommendStar: Boolean = false
    ) {
        // 显示预约
        if (gameEntity.isReservable) {
            holder.multiVersionDownloadTv?.visibility = View.GONE
            updateItemViewStatus(holder, briefStyle, gameEntity.columnRecommend, isShowRecommendStar)
            updateDownloadButton(
                context,
                holder.gameDownloadBtn,
                gameEntity,
                hideDownloadBtnIfNoAvailableContent,
                pluginLocation
            )
            return
        }
        if (gameEntity.getApk().isEmpty() || gameEntity.downloadOffStatus != null) {
            holder.multiVersionDownloadTv?.visibility = View.GONE
            holder.gameDownloadTips?.visibility = View.GONE
            updateItemViewStatus(holder, briefStyle, gameEntity.columnRecommend, isShowRecommendStar)
        } else if (gameEntity.getApk().size == 1) {
            holder.multiVersionDownloadTv?.visibility = View.GONE
            holder.gameDownloadTips?.visibility = View.GONE
            updateNormalItem(context, holder, gameEntity, briefStyle, isShowRecommendStar)
        } else {
            holder.multiVersionDownloadTv?.visibility = View.VISIBLE
            updatePluginItem(context, holder, gameEntity, briefStyle, isShowRecommendStar)
        }
        updateDownloadButton(
            context,
            holder.gameDownloadBtn,
            gameEntity,
            hideDownloadBtnIfNoAvailableContent,
            pluginLocation
        )
    }

    fun updateDownloadButton(
        context: Context,
        downloadBtn: DownloadButton,
        gameEntity: GameEntity,
        hideDownloadBtnIfNoAvailableContent: Boolean = false,
        pluginLocation: PluginLocation? = PluginLocation.only_game
    ) {
        // 控制是否显示下载按钮
        downloadBtn.goneIf(!Config.isShowDownload(gameEntity.id) || context.getString(R.string.app_name) == gameEntity.name)
        // 青少年模式或者需要特殊处理显示查看
        if (SPUtils.getBoolean(Constants.SP_TEENAGER_MODE) || gameEntity.isSpecialDownload()) {
            downloadBtn.text = "查看"
            downloadBtn.buttonStyle = DownloadButton.ButtonStyle.TEENAGER_MODE
            return
        }
        if (gameEntity.isReservable) {
            downloadBtn.apply {
                if (!ReservationRepository.thisGameHasBeenReserved(gameEntity.id)) {
                    text = "预约"
                    buttonStyle = DownloadButton.ButtonStyle.RESERVABLE
                } else {
                    text = "已预约"
                    buttonStyle = DownloadButton.ButtonStyle.RESERVED
                }
            }
            return
        }
        if (RegionSettingHelper.getGameH5DownloadByGameId(gameEntity.id) != null) {
            downloadBtn.apply {
                isClickable = true
                text = context.getString(R.string.check)
                setBackgroundResource(R.drawable.download_button_normal_style)
                setTextColor(R.color.white.toColor(context))
            }
            return
        }
        if (gameEntity.getApk().isEmpty() || gameEntity.downloadOffStatus != null) {
            val h5LinkEntity = gameEntity.h5Link
            val offStatus = gameEntity.downloadOffStatus
            downloadBtn.apply {
                if (h5LinkEntity != null) {
                    text = if ("play" == h5LinkEntity.type) {
                        context.getString(R.string.start_playing)
                    } else {
                        context.getString(R.string.check)
                    }
                    isClickable = true
                } else {
                    if ("dialog" == offStatus) {
                        text = context.getString(R.string.check)
                    } else if ("updating" == offStatus) {
                        text = context.getString(R.string.updating)
                        buttonStyle = DownloadButton.ButtonStyle.UPDATING
                    } else {
                        text = context.getString(R.string.none)
                        buttonStyle = DownloadButton.ButtonStyle.NONE
                        if (hideDownloadBtnIfNoAvailableContent) {
                            visibility = View.GONE
                        }
                    }
                    downloadBtn.isClickable = false
                }
            }
        } else if (gameEntity.getApk().size == 1) {
            var downloadEntity = DownloadManager.getInstance().getDownloadEntitySnapshot(gameEntity)

            if (downloadEntity == null && gameEntity.isVGame()) {
                downloadEntity = VHelper.getDownloadEntitySnapshot(gameEntity.id, gameEntity.getUniquePackageName())
            }

            if (downloadEntity == null) {
                val entryMap: ArrayMap<String, DownloadEntity> = gameEntity.getEntryMap()
                val apkEntity = gameEntity.getApk()[0]
                if (entryMap.isNotEmpty()) {
                    downloadEntity = entryMap[apkEntity.getPlatform()]
                }
            }

            if (downloadEntity != null) {
                downloadBtn.apply {
                    when (downloadEntity.status) {
                        DownloadStatus.done -> {
                            if (downloadEntity.isSimulatorGame() && gameEntity.simulator != null) {
                                GameUtils.setDownloadBtnStatus(context, gameEntity, downloadBtn, pluginLocation)
                            } else if (downloadEntity.isVGame()) {
                                buttonStyle =
                                    if (PackagesManager.isCanUpdate(
                                            downloadEntity.gameId,
                                            downloadEntity.packageName
                                        )
                                    ) {
                                        setText(R.string.update)
                                        DownloadButton.ButtonStyle.NORMAL
                                    } else {
                                        setText(R.string.launch)
                                        DownloadButton.ButtonStyle.LAUNCH_OR_OPEN
                                    }
                            } else {
                                buttonStyle = DownloadButton.ButtonStyle.INSTALL_NORMAL
                                setText(R.string.install)
                            }
                            buttonStyle =
                                if (downloadEntity.isPluggable && PackagesManager.isInstalled(downloadEntity.packageName)) {
                                    DownloadButton.ButtonStyle.PLUGIN
                                } else {
                                    DownloadButton.ButtonStyle.NORMAL
                                }
                        }
                        DownloadStatus.pause,
                        DownloadStatus.timeout,
                        DownloadStatus.neterror,
                        DownloadStatus.subscribe,
                        DownloadStatus.overflow -> {
                            buttonStyle = DownloadButton.ButtonStyle.NORMAL
                            setText(R.string.resume)
                        }
                        DownloadStatus.cancel -> {
                            GameUtils.setDownloadBtnStatus(context, gameEntity, downloadBtn, pluginLocation)
                        }
                        else -> {}
                    }
                }
            } else {
                GameUtils.setDownloadBtnStatus(context, gameEntity, downloadBtn, pluginLocation)
            }
        } else {
            GameUtils.setDownloadBtnStatus(context, gameEntity, downloadBtn, pluginLocation)
        }
    }

    // 更新正常的条目，只有一个apk包
    private fun updateNormalItem(
        context: Context,
        holder: GameViewHolder,
        gameEntity: GameEntity,
        briefStyle: String?,
        isShowRecommendStar: Boolean = false
    ) {
        val entryMap: ArrayMap<String, DownloadEntity> = gameEntity.getEntryMap()
        val apkEntity = gameEntity.getApk()[0]
        if (entryMap.isNotEmpty()) {
            val downloadEntity = entryMap[apkEntity.getPlatform()]
            if (downloadEntity != null) {
                if (downloadEntity.isSimulatorGame()) {
                    if (downloadEntity.status != DownloadStatus.done) {
                        // 更改进度条和提示文本的状态
                        changeStatus(context, holder, downloadEntity)
                        return
                    }
                } else {
                    // 更改进度条和提示文本的状态
                    changeStatus(context, holder, downloadEntity)
                    return
                }
            }
        }
        updateItemViewStatus(holder, briefStyle, gameEntity.columnRecommend, isShowRecommendStar)
    }

    // 更新插件的条目，有多个apk包
    private fun updatePluginItem(
        context: Context,
        holder: GameViewHolder,
        gameEntity: GameEntity,
        briefStyle: String?,
        isShowRecommendStar: Boolean = false
    ) {
        val entryMap = gameEntity.getEntryMap()
        if (entryMap.isNotEmpty()) {
            val queue = DownloadManager.getInstance().getQueue(gameEntity.name)
            val downloadEntity = if (queue != null && !queue.isEmpty()) {
                entryMap[queue.peek()]
            } else {
                entryMap[entryMap.keyAt(0)]
            }
            if (downloadEntity != null) {
                // 更改进度条和提示文本的状态
                changeStatus(context, holder, downloadEntity, gameEntity.getApk().size > 1)
                return
            }
        }
        updateItemViewStatus(holder, briefStyle, gameEntity.columnRecommend, isShowRecommendStar)
    }

    // 更改进度条和提示文本的状态
    fun changeStatus(
        context: Context,
        holder: GameViewHolder,
        downloadEntity: DownloadEntity,
        isMultiVersion: Boolean = false
    ) {
        when (downloadEntity.status) {
            DownloadStatus.redirected,
            DownloadStatus.downloading -> {
                if (isMultiVersion) {
                    holder.gameDownloadBtn.buttonStyle = DownloadButton.ButtonStyle.NORMAL
                    val darkMode = (holder.gameDownloadTips?.getTag(R.string.is_dark_mode_on_id) as? Boolean) ?: false
                    val isDarkModeChanged = DarkModeUtils.isDarkModeOn(context) != darkMode
                    if (holder.gameDownloadTips?.visibility == View.GONE || holder.gameDownloadTips?.isAnimating == false || isDarkModeChanged) {
                        holder.gameDownloadTips?.visibility = View.VISIBLE
                        holder.gameDownloadTips?.setDownloadTipsAnimation(true)
                    }
                    holder.gameDownloadTips?.setTag(R.string.is_dark_mode_on_id, DarkModeUtils.isDarkModeOn(context))
                } else {
                    holder.gameDownloadTips?.visibility = View.GONE
                    holder.gameDownloadBtn.buttonStyle = DownloadButton.ButtonStyle.DOWNLOADING_NORMAL
                    holder.gameDownloadBtn.progress = (downloadEntity.percent * 10).toInt()
                    holder.gameDownloadBtn.text = downloadEntity.percent.toString() + "%"
                }
            }
            DownloadStatus.waiting -> {
                if (isMultiVersion) {
                    holder.gameDownloadTips?.visibility = View.VISIBLE
                    holder.gameDownloadTips?.setDownloadTipsAnimation(false)
                }
                holder.gameDownloadBtn.buttonStyle = DownloadButton.ButtonStyle.WAITING
                holder.gameDownloadBtn.text = context.getString(R.string.waiting)
            }
            DownloadStatus.pause,
            DownloadStatus.timeout,
            DownloadStatus.neterror,
            DownloadStatus.subscribe,
            DownloadStatus.overflow -> {
                if (isMultiVersion) {
                    holder.gameDownloadTips?.visibility = View.VISIBLE
                    holder.gameDownloadTips?.setDownloadTipsAnimation(false)
                }
                holder.gameDownloadBtn.buttonStyle = DownloadButton.ButtonStyle.NORMAL
                holder.gameDownloadBtn.text = context.getString(R.string.resume)
            }
            DownloadStatus.done -> {
                if (isMultiVersion) {
                    holder.gameDownloadTips?.visibility = View.VISIBLE
                    holder.gameDownloadTips?.setDownloadTipsAnimation(false)
                }
                val xapkStatus = downloadEntity.meta[XapkInstaller.XAPK_UNZIP_STATUS]
                if (XapkUnzipStatus.UNZIPPING.name == xapkStatus) {
                    val percent = downloadEntity.meta[XapkInstaller.XAPK_UNZIP_PERCENT]
                    holder.gameDownloadBtn.progress = (java.lang.Float.valueOf(percent) * 10).toInt()
                    holder.gameDownloadBtn.text = "$percent%"
                    return
                } else if (XapkUnzipStatus.FAILURE.name == xapkStatus) {
                    holder.gameDownloadBtn.setText(R.string.install)
                    holder.gameDownloadBtn.buttonStyle = DownloadButton.ButtonStyle.INSTALL_NORMAL
                    return
                }
                holder.gameDownloadBtn.progress = 1000
                holder.gameDownloadBtn.setText(R.string.hundred_percent)
            }
            else -> {
                holder.gameDownloadTips?.visibility = View.GONE
            }
        }
    }

    private fun updateItemViewStatus(
        holder: GameViewHolder,
        briefStyle: String?,
        recommendStyle: LinkEntity?,
        isShowRecommendStar: Boolean = false
    ) {
        holder.gameDownloadTips?.visibility = View.GONE
        // 推荐指数优先，现暂时为游戏单详情列表游戏使用
        if (isShowRecommendStar && holder.recommendStarInfo != null) {
            holder.recommendStarInfo!!.visibility = View.VISIBLE
            if (holder.gameRating != null) holder.gameRating!!.visibility = View.GONE
            holder.gameDes.visibility = View.GONE
            return
        }

        // 推荐优先，有推荐内容时不执行下面的 star 和 brief 代码块
        if (briefStyle != null && recommendStyle != null && briefStyle.contains("recommend")) {
            if (holder.recommendContainer != null) {
                holder.recommendContainer!!.visibility = View.VISIBLE
            }
            if (holder.gameRating != null) holder.gameRating!!.visibility = View.GONE
            holder.gameDes.visibility = View.GONE
            holder.recommendTv.text = recommendStyle.text
            if (TextUtils.isEmpty(recommendStyle.icon)) {
                holder.recommendIv.visibility = View.GONE
                holder.recommendContainer?.setPadding(6F.dip2px(), 0, 8F.dip2px(), 0)
            } else {
                holder.recommendIv.visibility = View.VISIBLE
                ImageUtils.display(holder.recommendIv, recommendStyle.icon)
            }
            return
        } else {
            if (holder.recommendContainer != null) {
                holder.recommendContainer!!.visibility = View.GONE
            }
        }
        if (briefStyle != null && briefStyle.contains("star")) {
            if (holder.gameRating != null) holder.gameRating!!.visibility = View.VISIBLE
        } else {
            if (holder.gameRating != null) holder.gameRating!!.visibility = View.GONE
        }

        // 缺省情况下回落到游戏简介
        if (TextUtils.isEmpty(briefStyle) || briefStyle!!.contains("brief") || briefStyle.contains("recommend")) {
            holder.gameDes.visibility = View.VISIBLE
        } else {
            holder.gameDes.visibility = View.GONE
        }
    }

    @JvmStatic
    fun setOnClickListener(
        context: Context,
        downloadBtn: DownloadButton,
        gameEntity: GameEntity,
        position: Int,
        adapter: RecyclerView.Adapter<out RecyclerView.ViewHolder?>?,
        entrance: String,
        location: String
    ) {
        setOnClickListener(context, downloadBtn, gameEntity, position, adapter, entrance, location, null)
    }

    @JvmStatic
    fun setOnClickListener(
        context: Context,
        downloadBtn: DownloadButton,
        gameEntity: GameEntity,
        position: Int,
        adapter: RecyclerView.Adapter<out RecyclerView.ViewHolder?>?,
        entrance: String,
        location: String,
        traceEvent: ExposureEvent?
    ) {
        setOnClickListener(context, downloadBtn, gameEntity, position, adapter, entrance, location, traceEvent, null)
    }

    /**
     * @param clickCallback 供那些需要知道点击回调的地方使用，触发具体响应才会回调
     */
    @JvmStatic
    fun setOnClickListener(
        context: Context,
        downloadBtn: DownloadButton,
        gameEntity: GameEntity,
        position: Int,
        adapter: RecyclerView.Adapter<out RecyclerView.ViewHolder?>?,
        entrance: String,
        location: String,
        traceEvent: ExposureEvent?,
        clickCallback: EmptyCallback?
    ) {
        setOnClickListener(
            context,
            downloadBtn,
            gameEntity,
            position,
            adapter,
            entrance,
            location,
            traceEvent,
            clickCallback,
            null,
            null
        )
    }

    /**
     * @param allStateClickCallback 供那些需要知道点击回调的地方使用，只要触发点击动作就会回调
     */
    @JvmStatic
    fun setOnClickListener(
        context: Context,
        downloadBtn: View,
        gameEntity: GameEntity,
        position: Int,
        adapter: RecyclerView.Adapter<out RecyclerView.ViewHolder?>?,
        entrance: String,
        location: String,
        traceEvent: ExposureEvent?,
        clickCallback: EmptyCallback?,
        refreshCallback: EmptyCallback?,
        allStateClickCallback: EmptyCallback?
    ) {
        val gamePermissionDialogFragment = (context as AppCompatActivity).supportFragmentManager.findFragmentByTag(
            GamePermissionDialogFragment::class.java.name
        ) as GamePermissionDialogFragment?
        gamePermissionDialogFragment?.dismissAllowingStateLoss()
        if (SPUtils.getBoolean(Constants.SP_TEENAGER_MODE)) {
            downloadBtn.setOnClickListener {
                DialogHelper.showDialog(
                    context,
                    "提示",
                    "当前处于儿童/青少年模式， \n暂不提供游戏下载",
                    "退出青少年模式",
                    "关闭",
                    { context.startActivity(TeenagerModeActivity.getIntent(context)) },
                    {},
                    DialogHelper.Config(
                        centerTitle = true,
                        centerContent = true
                    )
                )
            }
            return
        }
        if (gameEntity.isSpecialDownload()) {
            val info = RegionSettingHelper.getGameSpecialDownloadInfo(gameEntity.id) ?: return
            downloadBtn.setOnClickListener {
                DialogHelper.showDialog(
                    context,
                    "提示",
                    info.hintText,
                    "前往论坛",
                    "",
                    {
                        if (info.bbsId.isNotBlank()) {
                            if (info.topId.isNotBlank()) {
                                val data = hashMapOf(EntranceConsts.KEY_TOP_ID to info.topId)
                                PageSwitchDataHelper.pushCurrentPageData(data)
                            }
                            DirectUtils.directForumDetail(context, info.bbsId, entrance)
                        }
                    },
                    {},
                    DialogHelper.Config(
                        centerTitle = true,
                        centerContent = true
                    )
                )
            }
            return
        }
        if (gameEntity.isReservable) {
            if (!ReservationRepository.thisGameHasBeenReserved(gameEntity.id)) {
                downloadBtn.setOnClickListener {
                    allStateClickCallback?.onCallback()
                    CheckLoginUtils.checkLogin(context, entrance) {
                        clickCallback?.onCallback()
                        ReservationHelper.reserve(context, gameEntity.id, object : EmptyCallback {
                            override fun onCallback() {
                                LogUtils.logReservation(gameEntity, traceEvent)
                                adapter?.notifyItemChanged(position)
                                refreshCallback?.onCallback()
                            }
                        })
                    }
                }
            } else {
                downloadBtn.setOnClickListener {
                    allStateClickCallback?.onCallback()
                    clickCallback?.onCallback()
                    if ("download" == gameEntity.reserveStatus) {
                        ReservationHelper.showDeleteReservationDialog(context) {
                            ReservationHelper.deleteReservation(gameEntity) {
                                adapter?.notifyItemChanged(position)
                                refreshCallback?.onCallback()
                            }
                        }
                    } else {
                        ReservationHelper.showCancelReservationDialog(context, {
                            NewFlatLogUtils.logMyGameCancelReserveDialogClick(
                                "确定取消",
                                gameEntity.id,
                                gameEntity.name ?: ""
                            )
                            ReservationHelper.cancelReservation(gameEntity) {
                                adapter?.notifyItemChanged(position)
                                refreshCallback?.onCallback()
                            }
                        }, object : CancelListener {
                            override fun onCancel() {
                                NewFlatLogUtils.logMyGameCancelReserveDialogClick(
                                    "关闭弹窗",
                                    gameEntity.id,
                                    gameEntity.name ?: ""
                                )
                            }
                        })
                    }
                }
            }
            return
        }
        val gameH5Download = RegionSettingHelper.getGameH5DownloadByGameId(gameEntity.id)
        if (gameH5Download != null) {
            downloadBtn.setOnClickListener {
                DialogUtils.showGameH5DownloadDialog(context, gameEntity, gameH5Download)
            }
            return
        }
        if (gameEntity.getApk().size == 0 && gameEntity.h5Link != null) {
            downloadBtn.setOnClickListener {
                allStateClickCallback?.onCallback()
                MtaHelper.onEvent("H5页面", "入口", "列表页_" + gameEntity.name)
                val linkEntity = gameEntity.h5Link
                val isPlay = "play" == linkEntity!!.type // 是否为开始玩
                if (isPlay) {
                    HistoryHelper.insertGameEntity(gameEntity)
                }
                val i = WebActivity.getIntentForWebGame(
                    context,
                    gameEntity.h5Link!!.link,
                    gameEntity.name,
                    isPlay,
                    linkEntity.closeButton
                )
                context.startActivity(i)
            }
        } else if (gameEntity.getApk().size == 1) {
            downloadBtn.setOnClickListener {
                val clickRunnable = EmptyCallback {
                    allStateClickCallback?.onCallback()
                    clickCallback?.onCallback()
                    onNormalClick(
                        context,
                        downloadBtn,
                        gameEntity,
                        position,
                        adapter,
                        entrance,
                        location,
                        traceEvent,
                        refreshCallback
                    )
                }

                // 启动不需要请求存储权限
                if (downloadBtn is DownloadButton && downloadBtn.text == context.getString(R.string.launch)) {
                    clickRunnable.onCallback()
                } else {
                    PermissionHelper.checkStoragePermissionBeforeAction(context, clickRunnable)
                }
            }
        } else {
            downloadBtn.setOnClickListener { view: View ->
                allStateClickCallback?.onCallback()
                clickCallback?.onCallback()

                ChainBuilder().apply {
                    addHandler(GamePermissionHandler())
                    addHandler(CheckStoragePermissionHandler())
                    addHandler(CertificationHandler())
                    addHandler(VersionNumberHandler())
                }
                    .setProcessEndCallback {
                        DownloadDialog.showDownloadDialog(view.context, gameEntity, traceEvent, entrance, location)
                    }
                    .buildHandlerChain()
                    ?.handleRequest(context, gameEntity)
            }
        }
    }

    @JvmOverloads
    fun onNormalClick(
        context: Context,
        downloadBtn: View?,
        gameEntity: GameEntity,
        position: Int,
        adapter: RecyclerView.Adapter<out RecyclerView.ViewHolder?>?,
        entrance: String,
        location: String,
        traceEvent: ExposureEvent? = null,
        refreshCallback: EmptyCallback? = null
    ) {
        val str = if (downloadBtn is DownloadButton) downloadBtn.text else context.getString(R.string.download)
        if (gameEntity.getApk().isEmpty()) return
        val apk = gameEntity.getApk().safelyGetInRelease(0) ?: return
        if (str == context.getString(R.string.download)) {
            ChainBuilder().apply {
                addHandler(UpdateNewSimulatorHandler())
                addHandler(GamePermissionHandler())
                addHandler(BrowserInstallHandler())
                addHandler(PackageCheckHandler())
                addHandler(DownloadDialogHelperHandler())
                addHandler(CertificationHandler())
                addHandler(OverseaDownloadHandler())
                addHandler(CheckDownloadHandler())
            }
                .setProcessEndCallback {
                    download(context, gameEntity, downloadBtn, entrance, location, it as Boolean, traceEvent)
                }
                .buildHandlerChain()
                ?.handleRequest(context, gameEntity)
            DataLogUtils.uploadGameLog(context, gameEntity.id, gameEntity.name, entrance)
        } else if (str == context.getString(R.string.attempt)) {
            ChainBuilder().apply {
                addHandler(UpdateNewSimulatorHandler())
                addHandler(GamePermissionHandler())
                addHandler(BrowserInstallHandler())
                addHandler(PackageCheckHandler())
                addHandler(DownloadDialogHelperHandler())
                addHandler(CertificationHandler())
                addHandler(VersionNumberHandler())
                addHandler(OverseaDownloadHandler())
                addHandler(CheckDownloadHandler())
            }
                .setProcessEndCallback {
                    download(context, gameEntity, downloadBtn, entrance, location, it as Boolean, traceEvent)
                }
                .buildHandlerChain()
                ?.handleRequest(context, gameEntity)
            DataLogUtils.uploadGameLog(context, gameEntity.id, gameEntity.name, entrance)
        } else if (str == context.getString(R.string.smooth)) {
            ChainBuilder().apply {
                addHandler(GamePermissionHandler())
                addHandler(PackageCheckHandler())
                addHandler(DownloadDialogHelperHandler())
                addHandler(CertificationHandler())
                addHandler(VersionNumberHandler())
                addHandler(OverseaDownloadHandler())
                addHandler(ValidateVSpaceHandler())
                addHandler(CheckDownloadHandler())
            }
                .setProcessEndCallback {
                    download(context, gameEntity, downloadBtn, entrance, location, it as Boolean, traceEvent)
                }
                .buildHandlerChain()
                ?.handleRequest(context, gameEntity)
        } else if (str.contains("化")) {
            if (entrance.contains("我的游戏")) {
                MtaHelper.onEvent("我的游戏_启动", "插件化", gameEntity.name)
            }
            if (gameEntity.pluggableCollection != null) {
                DownloadDialog.showDownloadDialog(context, gameEntity, traceEvent, entrance, location)
            } else {
                ChainBuilder().apply {
                    addHandler(DownloadDialogHelperHandler())
                    addHandler(CertificationHandler())
                    addHandler(CheckDownloadHandler())
                }
                    .setProcessEndCallback {
                        plugin(context, gameEntity, downloadBtn, entrance, location, it as Boolean, traceEvent)
                    }
                    .buildHandlerChain()
                    ?.handleRequest(context, gameEntity)
            }
        } else if (str == context.getString(R.string.install)) {
            val downloadEntity = SimulatorGameManager.findDownloadEntityByUrl(apk.url)
            if (gameEntity.simulator != null) {
                val isInstalled =
                    PackageUtils.isInstalledFromAllPackage(context, gameEntity.simulator!!.apk!!.packageName)
                val isInstalledNewSimulator = SimulatorGameManager.isNewSimulatorInstalled(context)
                val isInstalledOldSimulator = SimulatorGameManager.isOldSimulatorInstalled(context)
                var simulator = gameEntity.simulator
                val newSimulator = Config.getNewSimulatorEntitySetting()
                if (!isInstalledOldSimulator && newSimulator != null) {//在没有安装旧的模拟器且有配置新版模拟器 才使用新版模拟器 否则还是用以前旧的
                    if (newSimulator.active) {
                        simulator = newSimulator
                    }
                }
                if (downloadEntity != null && SimulatorGameManager.isSimulatorGame(gameEntity) && !isInstalled && !isInstalledNewSimulator) {
                    SimulatorDownloadManager.getInstance().showDownloadDialog(
                        context, simulator,
                        SimulatorDownloadManager.SimulatorLocation.LAUNCH, gameEntity.id, gameEntity.name!!, null
                    )
                    return
                }
            }

            if (gameEntity.isVGame()) {
                VHelper.installOrLaunch((context as AppCompatActivity), gameEntity.getUniquePackageName() ?: "")
            } else {
                install(context, gameEntity, position, adapter, refreshCallback)
            }
        } else if (str == context.getString(R.string.launch)) {
            EnergyBridge.postEnergyTask("play_game", gameEntity.id, gameEntity.getApk()[0].packageName)
            //启动模拟器游戏
            if (SimulatorGameManager.isSimulatorGame(gameEntity)) {
                if (NewSimulatorGameManager.shouldShowUpdateNewSimulatorAlert(context)) {
                    NewSimulatorGameManager.showUpdateNewsSimulator(context, null)
                    return
                }
                val downloadEntity = SimulatorGameManager.findDownloadEntityByUrl(gameEntity.getApk()[0].url)
                if (downloadEntity != null) {
                    val file = File(downloadEntity.path)
                    if (!file.exists()) {
                        download(context, gameEntity, downloadBtn, entrance, location, false, traceEvent)
                        return
                    }
                    NewFlatLogUtils.logSimulatorGameCardClick("启动")
                    SimulatorGameManager.launchSimulatorGame(downloadEntity, gameEntity)
                }
                return
            }

            if (gameEntity.isVGame()) {
                VHelper.installOrLaunch((context as AppCompatActivity), gameEntity.getUniquePackageName() ?: "")
                return
            }

            if (entrance.contains("我的游戏")) {
                MtaHelper.onEvent("我的游戏_启动", "启动", gameEntity.name)
            }
            PackageUtils.launchApplicationByPackageName(context, gameEntity.getApk()[0].packageName)
        } else if (str == context.getString(R.string.update)) {
            if (entrance.contains("我的游戏")) {
                MtaHelper.onEvent("我的游戏_启动", "更新", gameEntity.name)
            }
            if (gameEntity.isVGame()) {
                VHelper.updateOrReDownload(gameEntity)
                return
            }
            DownloadDialogHelper.findAvailableDialogAndShow(context, gameEntity, apk, object : EmptyCallback {
                override fun onCallback() {
                    DialogUtils.checkDownload(context, apk.size) { isSubscribe: Boolean ->
                        update(context, gameEntity, entrance, location, isSubscribe, traceEvent)
                    }
                }
            })
        } else {
            var downloadEntity = DownloadManager.getInstance().getDownloadEntitySnapshot(gameEntity)
            if (downloadEntity == null && gameEntity.getApk().size == 1) {
                val entryMap: ArrayMap<String, DownloadEntity> = gameEntity.getEntryMap()
                val apkEntity = gameEntity.getApk()[0]
                if (entryMap.isNotEmpty()) {
                    downloadEntity = entryMap[apkEntity.getPlatform()]
                }
            }
            if (downloadEntity != null) {
                val xapkStatus = downloadEntity.meta[XapkInstaller.XAPK_UNZIP_STATUS]
                if (XapkUnzipStatus.UNZIPPING.name == xapkStatus) {
                    cancelUnzipTask(downloadEntity)
                    return
                }
                when (str) {
                    context.getString(R.string.resume) -> {
                        DownloadManager.getInstance().resume(downloadEntity, true)
                    }
                    context.getString(R.string.waiting) -> {
                        Utils.toast(context, "最多只能同时下载三个任务，请稍等")
                    }
                    else -> {
                        DownloadManager.getInstance().pause(downloadEntity.url)
                    }
                }
            }
        }
    }

    //下载
    private fun download(
        context: Context,
        gameEntity: GameEntity,
        downloadBtn: View?,
        entrance: String,
        location: String,
        isSubscribe: Boolean,
        traceEvent: ExposureEvent?
    ) {
        if (gameEntity.getApk().isEmpty()) return

        val msg = FileUtils.isCanDownload(context, gameEntity.getApk()[0].size)
        if (TextUtils.isEmpty(msg)) {
            DownloadManager.createDownload(
                context,
                gameEntity,
                context.getString(R.string.download),
                entrance,
                location,
                isSubscribe,
                traceEvent
            )
            Utils.toast(context, gameEntity.name + "已加入下载队列")
            if (downloadBtn is DownloadButton) {
                downloadBtn.text = "0%"
                downloadBtn.buttonStyle = DownloadButton.ButtonStyle.DOWNLOADING_NORMAL
            }
            DeviceRemindDialog.showDeviceRemindDialog(context, gameEntity)
        } else {
            Utils.toast(context, msg)
        }
    }

    //插件化
    private fun plugin(
        context: Context,
        gameEntity: GameEntity,
        downloadBtn: View?,
        entrance: String,
        location: String,
        isSubscribe: Boolean,
        traceEvent: ExposureEvent?
    ) {
        val msg = FileUtils.isCanDownload(context, gameEntity.getApk()[0].size)
        if (TextUtils.isEmpty(msg)) {
            DownloadManager.createDownload(context, gameEntity, "插件化", entrance, location, isSubscribe, traceEvent)
            Utils.toast(context, gameEntity.name + "已加入下载队列")
            if (downloadBtn is DownloadButton) {
                downloadBtn.setText(R.string.downloading)
                downloadBtn.buttonStyle = DownloadButton.ButtonStyle.DOWNLOADING_PLUGIN
            }
        } else {
            Utils.toast(context, msg)
        }
    }

    //安装
    private fun install(
        context: Context,
        gameEntity: GameEntity,
        position: Int,
        adapter: RecyclerView.Adapter<out RecyclerView.ViewHolder?>?,
        refreshCallback: EmptyCallback?
    ) {
        val apkEntity = gameEntity.getApk()[0]
        val downloadEntity = DownloadManager.getInstance().getDownloadEntitySnapshot(gameEntity)
        if (downloadEntity != null) {
            val path = downloadEntity.path
            when {
                FileUtils.isEmptyFile(path) -> {
                    Utils.toast(context, R.string.install_failure_hint)
                    DownloadManager.getInstance().cancel(downloadEntity.url)
                    gameEntity.getEntryMap().remove(apkEntity.getPlatform())
                    adapter?.notifyItemChanged(position)
                    refreshCallback?.onCallback()
                }
                PackageUtils.isCanPluggable(apkEntity) -> {
                    DialogHelper.showPluginDialog(context) { PackageInstaller.uninstall(context, path) }
                }
                else -> {
                    PackageInstaller.install(context, downloadEntity)
                }
            }
        }
    }

    //更新
    private fun update(
        context: Context,
        gameEntity: GameEntity,
        entrance: String,
        location: String,
        isSubscribe: Boolean,
        traceEvent: ExposureEvent?
    ) {
        DownloadManager.createDownload(context, gameEntity, "更新", entrance, location, isSubscribe, traceEvent)
    }
}