package com.gh.gamecenter

import android.content.Context
import android.content.ContextWrapper
import android.content.Intent
import android.os.Bundle
import android.view.View
import com.gh.base.DownloadToolbarActivity
import com.gh.common.exposure.ExposureManager.log
import com.gh.common.exposure.ExposureTraceUtils.appendTrace
import com.gh.gamecenter.common.constant.Constants
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.core.utils.DisplayUtils
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.feature.exposure.ExposureEvent
import com.gh.gamecenter.feature.exposure.ExposureEvent.Companion.createEvent
import com.gh.gamecenter.feature.exposure.ExposureType
import com.gh.gamecenter.gamedetail.GameDetailFragment
import com.halo.assistant.HaloApp

/**
 * Created by khy on 2017/3/24.
 * 游戏详情适配器
 */
class GameDetailActivity : DownloadToolbarActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        DisplayUtils.transparentStatusBar(this)
    }

    override fun provideNormalIntent(): Intent {
        return getTargetIntent(this, GameDetailActivity::class.java, GameDetailFragment::class.java)
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_game_detail
    }

    override fun showToolbarAtLeft(): Boolean {
        return true
    }

    override fun showDownloadMenu(): Boolean {
        return true
    }

    override fun onDestroy() {
        super.onDestroy()
        HaloApp.remove(Constants.GAME_DETAIL_COME_IN)
    }

    override fun getActivityNameInChinese(): String {
        return "游戏详情"
    }

    override fun getBusinessId(): Pair<String, String> {
        val fragment = targetFragment as GameDetailFragment
        return if (fragment.arguments != null) {
            Pair(fragment.requireArguments().getString(EntranceConsts.KEY_GAMEID) ?: "", "")
        } else {
            super.getBusinessId()
        }
    }


    override fun attachBaseContext(newBase: Context) {
        super.attachBaseContext(object : ContextWrapper(newBase) {
            override fun getSystemService(name: String): Any? {
                // 解决 VideoView 中 AudioManager 造成的内存泄漏
                if (Context.AUDIO_SERVICE == name) {
                    return applicationContext.getSystemService(name)
                }
                return super.getSystemService(name)
            }
        })
    }

    override fun isAutoResetViewBackgroundEnabled(): Boolean = true

    override fun updateStaticViewBackground(view: View?) {
        updateStaticView(
            view,
            listOf(
                R.id.menu_download_iv,
                R.id.gameBigEvent,
                R.id.cardContainer,
                R.id.iv_reserve,
                R.id.iv_concern,
                R.id.tab_title
            )
        )
    }

    companion object {

        @JvmStatic
        fun startGameDetailActivity(
            context: Context,
            gameEntity: GameEntity?,
            entrance: String,
            traceEvent: ExposureEvent?
        ) {
            startGameDetailActivity(context, gameEntity, entrance, "", traceEvent = traceEvent)
        }

        /**
         * @param gameEntity 游戏实体
         * @param defaultTab 默认定位到哪个tab
         * @param isSkipGameComment 是否跳转到评论tab
         * @param scrollToLibao 滚动到礼包区域
         * @param scrollToServer 滚动到开服表区域
         * @param traceEvent 曝光事件
         */
        @JvmStatic
        fun startGameDetailActivity(
            context: Context,
            gameEntity: GameEntity?,
            entrance: String,
            defaultTab: String = "",
            isSkipGameComment: Boolean = false,
            scrollToLibao: Boolean = false,
            scrollToServer: Boolean = false,
            traceEvent: ExposureEvent? = null
        ) {
            val bundle = Bundle()

            if (traceEvent != null) {
                val clickEvent = createEvent(gameEntity, traceEvent.source, appendTrace(traceEvent), ExposureType.CLICK)
                log(clickEvent)
                bundle.putParcelable(EntranceConsts.KEY_TRACE_EVENT, clickEvent)
            }
            if (gameEntity != null && traceEvent != null && gameEntity.id != traceEvent.payload.gameId) {
                // 当游戏 ID 跟曝光 traceEvent 的游戏ID 不一样的时候更新 traceEvent 的游戏ID
                val (payload) = createEvent(
                    gameEntity,
                    traceEvent.source,
                    appendTrace(traceEvent),
                    ExposureType.EXPOSURE
                )
                traceEvent.payload = payload
            }
            if (defaultTab.isNotEmpty()) {
                bundle.putString(EntranceConsts.KEY_TARGET, defaultTab)
            }
            if (isSkipGameComment) {
                bundle.putBoolean(EntranceConsts.KEY_SKIP_GAME_COMMENT, true)
            }
            if (scrollToLibao) {
                bundle.putString(EntranceConsts.KEY_TARGET, GameDetailFragment.TAB_DESC)
                bundle.putBoolean(EntranceConsts.KEY_SCROLL_TO_LIBAO, true)
            }
            if (scrollToServer) {
                bundle.putString(EntranceConsts.KEY_TARGET, GameDetailFragment.TAB_DESC)
                bundle.putBoolean(EntranceConsts.KEY_SCROLL_TO_SERVER, true)
            }
            bundle.putString(EntranceConsts.KEY_GAMEID, gameEntity?.id)
            bundle.putString(EntranceConsts.KEY_ENTRANCE, entrance)
            bundle.putParcelable(GameEntity.TAG, gameEntity)
            context.startActivity(
                getTargetIntent(
                    context,
                    GameDetailActivity::class.java,
                    GameDetailFragment::class.java,
                    bundle
                )
            )
        }

        @JvmStatic
        fun startGameDetailActivity(context: Context, gameId: String, entrance: String?, traceEvent: ExposureEvent?) {
            startGameDetailActivity(context, gameId, entrance, -1, traceEvent = traceEvent)
        }

        @JvmStatic
        fun startGameDetailActivity(
            context: Context,
            gameId: String,
            entrance: String?,
            defaultTab: Int = -1,
            isSkipGameComment: Boolean = false,
            scrollToLibao: Boolean = false,
            openVideoStreaming: Boolean = false,
            openPlatformWindow: Boolean = false,
            traceEvent: ExposureEvent? = null
        ) {
            startGameDetailActivity(
                context,
                gameId,
                entrance,
                defaultTab,
                isSkipGameComment,
                scrollToLibao,
                openVideoStreaming,
                openPlatformWindow,
                null,
                null,
                traceEvent
            )
        }

        /**
         * @param gameId 游戏Id
         * @param defaultTab 默认定位到哪个tab
         * @param isSkipGameComment 是否跳转到评论tab
         * @param scrollToLibao 滚动到礼包区域
         * @param openVideoStreaming 是否打开视频流
         * @param openPlatformWindow 打开多版本下载弹窗
         * @param platformName 平台名称
         * @param packageName 包名
         * @param traceEvent 曝光事件
         */
        @JvmStatic
        fun startGameDetailActivity(
            context: Context,
            gameId: String,
            entrance: String?,
            defaultTab: Int = -1,
            isSkipGameComment: Boolean = false,
            scrollToLibao: Boolean = false,
            openVideoStreaming: Boolean = false,
            openPlatformWindow: Boolean = false,
            platformName: String? = null,
            packageName: String? = null,
            traceEvent: ExposureEvent? = null
        ) {
            val bundle = Bundle()

            if (traceEvent != null) {
                val clickEvent = createEvent(
                    GameEntity(
                        gameId,
                        traceEvent.payload.gameName
                    ),
                    traceEvent.source,
                    appendTrace(traceEvent),
                    ExposureType.CLICK
                )
                log(clickEvent)
                bundle.putParcelable(EntranceConsts.KEY_TRACE_EVENT, clickEvent)
            }
            if (traceEvent != null && gameId != traceEvent.payload.gameId) {
                // 当游戏 ID 跟曝光 traceEvent 的游戏ID 不一样的时候更新 traceEvent 的游戏ID
                val (payload) = createEvent(
                    GameEntity(gameId),
                    traceEvent.source,
                    appendTrace(traceEvent),
                    ExposureType.EXPOSURE
                )
                traceEvent.payload = payload
            }
            if (defaultTab != -1) {
                bundle.putInt(EntranceConsts.KEY_TARGET, defaultTab)
            }
            if (isSkipGameComment) {
                bundle.putBoolean(EntranceConsts.KEY_SKIP_GAME_COMMENT, true)
            }
            if (openVideoStreaming) {
                bundle.putBoolean(EntranceConsts.KEY_OPEN_VIDEO_STREAMING, true)
                bundle.putString(EntranceConsts.KEY_TARGET, GameDetailFragment.TAB_DESC)
            }
            if (openPlatformWindow) {
                bundle.putBoolean(EntranceConsts.KEY_OPEN_PLATFORM_WINDOW, true)
                if (!platformName.isNullOrEmpty()) {
                    bundle.putString(EntranceConsts.KEY_PLATFORM, platformName)
                }
                if (!packageName.isNullOrEmpty()) {
                    bundle.putString(EntranceConsts.KEY_PACKAGENAME, packageName)
                }
            }
            if (scrollToLibao) {
                bundle.putString(EntranceConsts.KEY_TARGET, GameDetailFragment.TAB_DESC)
                bundle.putBoolean(EntranceConsts.KEY_SCROLL_TO_LIBAO, true)
            }
            bundle.putString(EntranceConsts.KEY_GAMEID, gameId)
            bundle.putString(EntranceConsts.KEY_ENTRANCE, entrance)
            context.startActivity(
                getTargetIntent(
                    context,
                    GameDetailActivity::class.java,
                    GameDetailFragment::class.java,
                    bundle
                )
            )
        }

    }
}