package com.gh.gamecenter

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.KeyEvent
import android.view.View
import com.therouter.router.Route
import com.gh.gamecenter.common.base.activity.ToolBarActivity
import com.gh.gamecenter.common.constant.Constants
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.common.constant.RouteConsts
import com.gh.gamecenter.common.utils.EnvHelper
import com.gh.gamecenter.common.utils.updateStatusBarColor
import com.gh.gamecenter.feature.entity.ConcernEntity
import com.gh.gamecenter.feature.entity.NewsEntity
import com.gh.gamecenter.common.entity.ToolBoxEntity
import com.halo.assistant.fragment.WebFragment

@Route(path = RouteConsts.activity.webActivity)
open class WebActivity : ToolBarActivity() {
    private var mIsFullScreen = false

    override fun onCreate(savedInstanceState: Bundle?) {
        val bundle = intent.getBundleExtra(NORMAL_FRAGMENT_BUNDLE)
        if (bundle != null) {
            val mGameName = bundle.getString(WebFragment.KEY_GAME_NAME)
            val mIsBackpressRequireConfirmation =
                bundle.getBoolean(WebFragment.KEY_REQUIRE_BACK_CONFIRMATION, false)
            mIsFullScreen = !TextUtils.isEmpty(mGameName) && mIsBackpressRequireConfirmation
            if (mIsFullScreen) {
                setTheme(R.style.AppFullScreenTheme)
            }
            super.onCreate(savedInstanceState)
            if (mIsFullScreen) {
                hideToolbar(true)
            }
        } else {
            super.onCreate(savedInstanceState)
        }
        updateStatusBarColor(com.gh.gamecenter.common.R.color.ui_surface, com.gh.gamecenter.common.R.color.ui_surface)
    }

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

    override fun provideNavigationItemClickListener(): View.OnClickListener {
        val bundle = intent.getBundleExtra(NORMAL_FRAGMENT_BUNDLE)
        return if (bundle != null) {
            val isTools = bundle.getBoolean(WebFragment.KEY_ISTOOLS, false)
            val isOpenNativePage = bundle.getBoolean(WebFragment.KEY_OPEN_NATIVE_PAGE, false)
            if (isTools || isOpenNativePage) {
                View.OnClickListener { finish() }
            } else super.provideNavigationItemClickListener()
        } else {
            super.provideNavigationItemClickListener()
        }
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        if (targetFragment is WebFragment) {
            val webFragment = targetFragment as WebFragment
            val consumed = webFragment.onKeyDown(keyCode, event)
            if (consumed) {
                return true
            }
        }
        return super.onKeyDown(keyCode, event)
    }

    override fun onDarkModeChanged() {
        super.onDarkModeChanged()
        updateStatusBarColor(com.gh.gamecenter.common.R.color.ui_surface, com.gh.gamecenter.common.R.color.ui_surface)
    }

    companion object {
        // 获取带分享按钮页面
        @JvmStatic
        fun getWebIntentByShare(context: Context?, url: String?, showWebShare: Boolean, entrance: String): Intent {
            val bundle = Bundle()
            bundle.putBoolean(EntranceConsts.KEY_WEB_SHARE, showWebShare)
            bundle.putString(EntranceConsts.KEY_URL, url)
            bundle.putBoolean(WebFragment.KEY_LEAVE_WEB_PAGE_TO_HANDLE_TITLE, true)
            bundle.putString(EntranceConsts.KEY_ENTRANCE, entrance)
            return getWebTargetIntent(context, bundle, url)
        }

        // 获取通用页面
        @JvmStatic
        fun getWebIntent(context: Context, title: String, url: String): Intent {
            val bundle = Bundle()
            bundle.putString(EntranceConsts.KEY_GAMENAME, title)
            bundle.putString(EntranceConsts.KEY_URL, url)
            return getTargetIntent(context, WebActivity::class.java, WebFragment::class.java, bundle)
        }

        // 获取第三方SDK目录
        @JvmStatic
        fun getThirdPartySdkStatementIntent(context: Context): Intent {
            val bundle = Bundle()
            bundle.putString(
                EntranceConsts.KEY_GAMENAME,
                context.getString(R.string.third_party_sdk_title)
            )
            bundle.putString(
                EntranceConsts.KEY_URL,
                context.getString(R.string.third_party_sdk_statement_url)
            )
            return getTargetIntent(
                context,
                WebActivity::class.java,
                WebFragment::class.java, bundle
            )
        }

        // 获取游戏服务上传准则页
        @JvmStatic
        fun getUploadPolicyIntent(context: Context): Intent {
            val bundle = Bundle()
            //        bundle.putString(EntranceUtils.KEY_GAMENAME, context.getString(R.string.upload_game_policy_title));
            bundle.putBoolean(WebFragment.KEY_COMPLETION_TITLE, true)
            bundle.putString(
                EntranceConsts.KEY_URL,
                context.getString(R.string.upload_game_policy_url)
            )
            return getTargetIntent(
                context,
                WebActivity::class.java,
                WebFragment::class.java, bundle
            )
        }

        // 获取工具箱页面
        @JvmStatic
        fun getWebByCollectionTools(
            context: Context?,
            toolBoxEntity: ToolBoxEntity,
            isCollectionTools: Boolean
        ): Intent {
            val bundle = Bundle()
            bundle.putString(EntranceConsts.KEY_URL, toolBoxEntity.url)
            bundle.putBoolean(WebFragment.KEY_ISTOOLS, true)
            bundle.putString(EntranceConsts.KEY_GAMENAME, toolBoxEntity.name)
            bundle.putParcelable(ToolBoxEntity.TAG, toolBoxEntity)
            bundle.putBoolean(WebFragment.KEY_ISCOLLECTIONTOOLS, isCollectionTools)
            return getTargetIntent(
                context,
                WebActivity::class.java,
                WebFragment::class.java, bundle
            )
        }

        // 获取微信绑定页
        @JvmStatic
        fun getBindWechatIntent(context: Context?): Intent {
            val url: String = if (EnvHelper.isDevEnv) {
                Constants.WECHAT_BIND_ADDRESS_DEV
            } else {
                Constants.WECHAT_BIND_ADDRESS
            }
            val bundle = Bundle()
            bundle.putString(EntranceConsts.KEY_URL, url)
            bundle.putBoolean(WebFragment.KEY_IS_BIND_WECHAT, true)
            bundle.putBoolean(WebFragment.KEY_LEAVE_WEB_PAGE_TO_HANDLE_BACK_PRESSED, true)
            return getTargetIntent(
                context,
                WebActivity::class.java,
                WebFragment::class.java, bundle
            )
        }

        // 获取游戏动态详情页
        @JvmStatic
        fun getIntentByNews(
            context: Context?,
            concernEntity: ConcernEntity,
            entrance: String?
        ): Intent {
            return getIntentByNews(context, concernEntity.link, concernEntity.getGameName(), concernEntity.id, entrance)
        }

        // 获取游戏动态详情页
        @JvmStatic
        fun getIntentByNews(
            context: Context?,
            concernLink: String?,
            concernGameName: String,
            concernId: String?,
            entrance: String?
        ): Intent {
            val bundle = Bundle()
            bundle.putString(EntranceConsts.KEY_URL, concernLink)
            bundle.putString(EntranceConsts.KEY_GAMENAME, concernGameName)
            bundle.putString(EntranceConsts.KEY_NEWSID, concernId)
            bundle.putString(EntranceConsts.KEY_ENTRANCE, entrance)
            return getTargetIntent(
                context,
                WebActivity::class.java,
                WebFragment::class.java, bundle
            )
        }

        // 获取资讯详情页
        @JvmStatic
        fun getIntentByNews(context: Context?, newsEntity: NewsEntity, entrance: String?): Intent {
            val bundle = Bundle()
            bundle.putString(EntranceConsts.KEY_URL, newsEntity.link)
            bundle.putString(EntranceConsts.KEY_GAMENAME, newsEntity.gameName)
            bundle.putString(EntranceConsts.KEY_NEWSID, newsEntity.id)
            bundle.putString(EntranceConsts.KEY_ENTRANCE, entrance)
            return getTargetIntent(
                context,
                WebActivity::class.java,
                WebFragment::class.java, bundle
            )
        }

        @JvmStatic
        fun getIntent(context: Context?, url: String?, autoCompletionTitle: Boolean): Intent {
            val bundle = Bundle()
            bundle.putString(EntranceConsts.KEY_URL, url)
            bundle.putBoolean(WebFragment.KEY_COMPLETION_TITLE, autoCompletionTitle)
            bundle.putBoolean(WebFragment.KEY_LEAVE_WEB_PAGE_TO_HANDLE_TITLE, false)
            return getWebTargetIntent(context, bundle, url)
        }

        @JvmStatic
        @JvmOverloads
        fun getIntent(
            context: Context?,
            url: String?,
            title: String?,
            autoCompletionTitle: Boolean,
            isOpenNativePage: Boolean,
            entrance: String? = null
        ): Intent {
            val bundle = Bundle()
            bundle.putString(EntranceConsts.KEY_URL, url)
            bundle.putString(EntranceConsts.KEY_GAMENAME, title)
            bundle.putBoolean(WebFragment.KEY_COMPLETION_TITLE, autoCompletionTitle)
            bundle.putBoolean(WebFragment.KEY_LEAVE_WEB_PAGE_TO_HANDLE_TITLE, false)
            bundle.putBoolean(WebFragment.KEY_OPEN_NATIVE_PAGE, isOpenNativePage)
            if (entrance != null) bundle.putString(EntranceConsts.KEY_ENTRANCE, entrance)
            return getWebTargetIntent(context, bundle, url)
        }

        // 获取问答页
        @JvmStatic
        fun getQAIntent(
            context: Context?,
            url: String?,
            title: String?,
            isWebPageHandleBackPressed: Boolean,
            qaType: Int
        ): Intent {
            val bundle = Bundle()
            bundle.putString(EntranceConsts.KEY_URL, url)
            bundle.putString(EntranceConsts.KEY_GAMENAME, title)
            bundle.putBoolean(WebFragment.KEY_COMPLETION_TITLE, false)
            bundle.putBoolean(WebFragment.KEY_LEAVE_WEB_PAGE_TO_HANDLE_TITLE, false)
            bundle.putBoolean(WebFragment.KEY_OPEN_NATIVE_PAGE, false)
            bundle.putBoolean(WebFragment.KEY_LEAVE_WEB_PAGE_TO_HANDLE_BACK_PRESSED, true)
            bundle.putBoolean(
                WebFragment.KEY_LEAVE_WEB_PAGE_TO_HANDLE_BACK_PRESSED,
                isWebPageHandleBackPressed
            )
            bundle.putInt(WebFragment.KEY_QA_TYPE, qaType)
            return getWebTargetIntent(context, bundle, url)
        }

        // 获取H5游戏
        @JvmStatic
        fun getIntentForWebGame(
            context: Context?,
            url: String?,
            gameName: String?,
            interveneBackpress: Boolean,
            closeButton: String?
        ): Intent {
            val bundle = Bundle()
            bundle.putString(EntranceConsts.KEY_URL, url)
            bundle.putString(WebFragment.KEY_GAME_NAME, gameName)
            bundle.putBoolean(WebFragment.KEY_LEAVE_WEB_PAGE_TO_HANDLE_TITLE, true)
            bundle.putString(WebFragment.KEY_CLOSE_BUTTON, closeButton)
            if (interveneBackpress) {
                bundle.putBoolean(WebFragment.KEY_REQUIRE_BACK_CONFIRMATION, true)
                bundle.putString(WebFragment.KEY_BACK_CONFIRMATION_CONTENT, "退出后将不保存当前游戏进度，确定退出吗？")
            }
            return getWebTargetIntent(context, bundle, url)
        }

        private fun getWebTargetIntent(context: Context?, bundle: Bundle, url: String?): Intent {
            val cls: Class<out ToolBarActivity?> = if (url?.contains("android_page_type=singleton") == true) {
                SingletonWebActivity::class.java
            } else {
                WebActivity::class.java
            }
            if (url?.contains("leave_web_page_handle_back_pressed=true") == true) {
                bundle.putBoolean(WebFragment.KEY_LEAVE_WEB_PAGE_TO_HANDLE_BACK_PRESSED, true)
            }
            val intent = Intent(context, cls)
            intent.putExtra(NORMAL_FRAGMENT_NAME, WebFragment::class.java.canonicalName)
            intent.putExtra(NORMAL_FRAGMENT_BUNDLE, bundle)
            return intent
        }
    }
}