package com.gh.common

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.text.TextUtils
import android.util.Base64
import com.gh.common.util.CheckLoginUtils
import com.gh.common.util.DirectUtils
import com.gh.common.util.DirectUtils.directToFeedback
import com.gh.common.util.DirectUtils.directToGameDetailVideoStreaming
import com.gh.common.util.DirectUtils.directToGameServerCalendar
import com.gh.common.util.DirectUtils.directToGameVideo
import com.gh.common.util.DirectUtils.directToLegacyVideoDetail
import com.gh.common.util.DirectUtils.directToLinkPage
import com.gh.common.util.DirectUtils.directToQa
import com.gh.common.util.PackageUtils
import com.gh.gamecenter.LibaoDetailActivity
import com.gh.gamecenter.MainActivity
import com.gh.gamecenter.NewsDetailActivity
import com.gh.gamecenter.WebActivity
import com.gh.gamecenter.common.constant.Constants
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.common.entity.CommunityEntity
import com.gh.gamecenter.common.entity.LinkEntity
import com.gh.gamecenter.common.entity.SimpleGameEntity
import com.gh.gamecenter.common.eventbus.EBReuse
import com.gh.gamecenter.common.utils.DialogHelper
import com.gh.gamecenter.common.utils.EnvHelper
import com.gh.gamecenter.core.AppExecutor
import com.gh.gamecenter.core.utils.CurrentActivityHolder
import com.gh.gamecenter.core.utils.GsonUtils.gson
import com.gh.gamecenter.core.utils.ToastUtils
import com.gh.gamecenter.entity.ActivityLabelEntity
import com.gh.gamecenter.entity.SubjectRecommendEntity
import com.gh.gamecenter.entity.VideoLinkEntity
import com.gh.gamecenter.eventbus.EBSkip
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.feature.utils.PlatformUtils
import com.gh.gamecenter.fragment.MainWrapperFragment
import com.gh.gamecenter.gamecollection.publish.GameCollectionEditActivity
import com.gh.gamecenter.qa.BbsType
import com.gh.gamecenter.qa.video.publish.VideoPublishActivity
import com.gh.gamecenter.subject.SubjectActivity
import com.gh.gamecenter.video.detail.VideoDetailContainerViewModel
import com.lightgame.utils.Utils
import org.greenrobot.eventbus.EventBus
import java.nio.charset.Charset

object DefaultUrlHandler {

    @JvmStatic
    fun interceptUrl(context: Context, url: String, entrance: String): Boolean {
        return interceptUrl(context, url, entrance, false)
    }

    /**
     * @param bringAppToFront 是否需要在不匹配 host 的时候把 APP 调回到前台 (如微信调起)
     */
    @JvmStatic
    fun interceptUrl(context: Context, url: String, entrance: String, bringAppToFront: Boolean = false): Boolean {
        val uri = Uri.parse(url)
        if ("ghzhushou" == uri.scheme) {
            Utils.log("url = $url")
            Utils.log("url = " + uri.scheme!!)
            val host = uri.host
            val path = uri.path
            var id = ""
            if (!TextUtils.isEmpty(path)) {
                id = path!!.substring(1)
            }
            if (TextUtils.isEmpty(id)) {
                id = uri.getQueryParameter("id") ?: ""
            }

            val intent: Intent
            when (host) {
                "article" -> context.startActivity(NewsDetailActivity.getIntentById(context, id, entrance))

                "game" -> DirectUtils.directToGameDetail(
                    context,
                    id = id,
                    tab = uri.getQueryParameter("to"),
                    autoDownload = uri.getQueryParameter("auto_download") == "true",
                    entrance = entrance
                )

                "column" -> SubjectActivity.startSubjectActivity(
                    context,
                    id,
                    uri.getQueryParameter("name"),
                    false,
                    entrance
                )

                "libao" -> context.startActivity(LibaoDetailActivity.getIntentById(context, id, entrance))

                "qq" -> try {
                    DirectUtils.directToQqConversation(context, id)
                } catch (e: Throwable) {
                    Utils.toast(context, "请检查是否已经安装手机QQ")
                    e.printStackTrace()
                }

                EntranceConsts.HOST_QQ_QUN -> {
                    val key = uri.getQueryParameter("key")
                    if (!DirectUtils.directToQqGroup(context, key)) {
                        Utils.toast(context, "请检查是否已经安装手机QQ")
                    }
                }

                "inurl" -> {
                    DirectUtils.directToWebView(context, uri.getQueryParameter("url") ?: "", entrance)
                }

                "outurl" -> {
                    intent = Intent()
                    intent.action = Intent.ACTION_VIEW
                    intent.data = Uri.parse(uri.getQueryParameter("url"))
                    try {
                        context.startActivity(intent)
                    } catch (e: Exception) {
                        Utils.toast(context, "请检查是否已经安装手机浏览器")
                        e.printStackTrace()
                    }

                }
                "question" -> DirectUtils.directToQuestionDetail(context, id, entrance, "文章链接")

                "real_name" -> DirectUtils.directToRealName(context)

                "community" -> {
                    val community = CommunityEntity()
                    community.id = id
                    community.name = uri.getQueryParameter("name") ?: ""
                    DirectUtils.directToCommunity(context, community)
                }

                "community_column" -> {
                    val community = CommunityEntity()
                    community.id = uri.getQueryParameter("community_id") ?: ""
                    community.name = uri.getQueryParameter("community_name") ?: ""
                    val columnId = uri.getQueryParameter("column_id") ?: ""
                    DirectUtils.directToCommunityColumn(context, community, columnId, entrance, "文章链接")
                }

                "answer" -> DirectUtils.directToAnswerDetail(context, id, entrance, "文章链接")

                "communities" -> {
                    // ghzhushou://communities/5a32405b2397ab000f688de3/articles/5c99d262c140b321564f04e3
                    var communityId = ""
                    var type = ""
                    var typeId = ""
                    val split = id.split("/".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
                    for (text in split) {
                        if (TextUtils.isEmpty(communityId)) {
                            communityId = text
                            continue
                        }
                        if (TextUtils.isEmpty(type)) {
                            type = text
                            continue
                        }
                        if (TextUtils.isEmpty(typeId)) {
                            typeId = text
                        }
                    }
                    if ("articles" == type) {
                        DirectUtils.directToCommunityArticle(
                            context, typeId, communityId,
                            entrance, "文章链接"
                        )
                    }
                }
                EntranceConsts.HOST_UPLOAD_VIDEO -> {
                    val titleParameter = uri.getQueryParameter("title")
                    val title = if (titleParameter.isNullOrEmpty()) "" else "#$titleParameter#"
                    val categoryId = uri.getQueryParameter("category_id") ?: ""
                    val link = uri.getQueryParameter("link") ?: ""
                    val gameId = uri.getQueryParameter("gameId") ?: ""
                    val gameName = uri.getQueryParameter("gameName") ?: ""
                    val tagActivityId = uri.getQueryParameter("tagActivityId") ?: ""
                    val tagActivityName = uri.getQueryParameter("tagActivityName") ?: ""
                    val linkEntity = VideoLinkEntity(title, categoryId, link, tagActivityId, tagActivityName)
                    val simpleGameEntity = SimpleGameEntity(gameId, gameName)
                    CheckLoginUtils.checkLogin(context, null, true, EntranceConsts.ENTRANCE_BROWSER) {
                        DirectUtils.directToVideoManager(
                            context,
                            linkEntity,
                            simpleGameEntity,
                            EntranceConsts.ENTRANCE_BROWSER,
                            ""
                        )
                    }
                }
                EntranceConsts.HOST_USERHOME -> {
                    val position = uri.getQueryParameter("position")
                    val subtype = uri.getQueryParameter("sub_type") ?: ""
                    val subGameType = uri.getQueryParameter("sub_game_type") ?: "game_collection"
                    DirectUtils.directToHomeActivity(
                        context,
                        id,
                        subtype,
                        subGameType,
                        if (position.isNullOrEmpty()) -1 else position.toInt(),
                        entrance,
                        ""
                    )
                }
                EntranceConsts.HOST_VIDEO_MORE -> {
                    val referer = uri.getQueryParameter("referer") ?: ""
                    val type = uri.getQueryParameter("type") ?: ""
                    val act = uri.getQueryParameter("act") ?: ""
                    val gameId = uri.getQueryParameter("gameId") ?: ""
                    val fieldId = uri.getQueryParameter("fieldId") ?: ""
                    val sectionName = uri.getQueryParameter("sectionName") ?: ""
                    val paginationType = uri.getQueryParameter("paginationType")
                        ?: "page"//活动分页方式 page filter
                    val location = if (!TextUtils.isEmpty(act)) {
                        VideoDetailContainerViewModel.Location.VIDEO_ACTIVITY.value
                    } else if (!TextUtils.isEmpty(fieldId)) {
                        VideoDetailContainerViewModel.Location.GAME_ZONE.value
                    } else {
                        id
                    }
                    directToLegacyVideoDetail(
                        context,
                        id,
                        location,
                        false,
                        gameId,
                        entrance,
                        "",
                        referer,
                        type,
                        act,
                        paginationType,
                        fieldId,
                        sectionName
                    )
                }
                EntranceConsts.HOST_VIDEO_DETAIL -> {
                    DirectUtils.directToVideoDetail(context, id, entrance, path)
                }
                EntranceConsts.HOST_VIDEO_SINGLE -> {
                    val referer = uri.getQueryParameter("referer") ?: ""
                    DirectUtils.directToVideoDetail(
                        context, id, VideoDetailContainerViewModel.Location.SINGLE_VIDEO.value,
                        false, "", entrance, "", if (TextUtils.isEmpty(referer)) "" else referer
                    )
                }
                EntranceConsts.HOST_VIDEO_STREAMING_HOME -> {
                    intent = Intent(context, MainActivity::class.java)
                    intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
                    intent.putExtra(MainActivity.SWITCH_TO_VIDEO, true)
                    context.startActivity(intent)
                }
                EntranceConsts.HOST_VIDEO_STREAMING_DESC -> {
                    directToGameDetailVideoStreaming(context, id, entrance)
                }
                EntranceConsts.HOST_VIDEO_COLLECTION -> {
                    directToGameVideo(context, id, entrance, "")
                }

                EntranceConsts.HOST_CATEGORY -> {
                    val title = uri.getQueryParameter("title")
                    DirectUtils.directCategoryDirectory(context, id, title ?: "", entrance, "")
                }
                EntranceConsts.HOST_COLUMN_COLLECTION -> {
                    val name = uri.getQueryParameter("name")
                    DirectUtils.directToColumnCollection(context, id, -1, entrance, name ?: "")
                }
                EntranceConsts.HOST_COLUMN -> {
                    DirectUtils.directToSubject(context, id, uri.getQueryParameter(EntranceConsts.KEY_NAME), entrance)
                }
                EntranceConsts.HOST_BLOCK -> {
                    val name = uri.getQueryParameter("name")
                        ?: ""
                    val entity = SubjectRecommendEntity(link = id, name = name, text = name)
                    DirectUtils.directToBlock(context, entity, entrance)
                }

                EntranceConsts.HOST_SERVER_BLOCK -> {
                    DirectUtils.directToGameServers(context, entrance = entrance, path = "")
                }

                EntranceConsts.HOST_AMWAY_BLOCK -> {
                    DirectUtils.directToAmway(context, entrance = entrance, path = "")
                }

                EntranceConsts.HOST_HELP -> {
                    val name = uri.getQueryParameter("name")
                        ?: ""
                    DirectUtils.directToQa(context, name, id)
                }

                EntranceConsts.HOST_HELP_COLLECTION -> {
                    val name = uri.getQueryParameter("name")
                        ?: ""
                    DirectUtils.directToQaCollection(context, name, id)
                }

                EntranceConsts.HOST_GAME_UPLOAD -> {
                    DirectUtils.directGameUpload(context, entrance = entrance, path = "")
                }
                EntranceConsts.HOST_GAME_ZONE -> {
                    val zoneUrl = uri.getQueryParameter("url") ?: ""
                    DirectUtils.directGameZone(context, id, zoneUrl, entrance)
                }

                EntranceConsts.HOST_LINK -> {
                    try {
                        val dataString = uri.getQueryParameter("data")
                        if (!TextUtils.isEmpty(dataString)) {
                            val linkData = Base64.decode(dataString, Base64.DEFAULT)
                            val linkDataString = String(linkData, Charset.defaultCharset())
                            val le = gson.fromJson(linkDataString, LinkEntity::class.java)
                            directToLinkPage(context, le, entrance, "")
                        }
                    } catch (e: Exception) {
                        e.printStackTrace()
                    }
                }

                EntranceConsts.HOST_GAME_NEWS -> {
                    DirectUtils.directToGameNews(
                        context,
                        uri.getQueryParameter(EntranceConsts.KEY_GAME_ID),
                        uri.getQueryParameter(EntranceConsts.KEY_GAME_NAME),
                        entrance
                    );
                }

                EntranceConsts.HOST_GAME_CALENDAR -> {
                    directToGameServerCalendar(context, uri.getQueryParameter(EntranceConsts.KEY_GAME_ID))
                }

                EntranceConsts.HOST_HISTORY_APK -> {
                    DirectUtils.directToHistoryApk(context, uri.getQueryParameter(EntranceConsts.KEY_GAME_ID))
                }

                EntranceConsts.HOST_FORUM_DETAIL -> {
                    DirectUtils.directForumDetail(context, id, entrance)
                }

                EntranceConsts.HOST_GAME_RATING_DETAIL -> {
                    DirectUtils.directToGameRatingDetail(
                        context,
                        uri.getQueryParameter(EntranceConsts.KEY_GAME_ID),
                        uri.getQueryParameter(EntranceConsts.KEY_COMMENT_ID),
                        EntranceConsts.ENTRANCE_BROWSER
                    )
                }

                EntranceConsts.HOST_FORUM -> {
                    val position = uri.getQueryParameter(EntranceConsts.KEY_POSITION)?.toInt()

                    DirectUtils.directToForum(context, position ?: 0)
                }

                EntranceConsts.HOST_UPLOAD_VIDEO_NEW -> {
                    val activityName = uri.getQueryParameter("activity_name") ?: ""
                    val activityId = uri.getQueryParameter("activity_id") ?: ""
                    val original = uri.getQueryParameter("original") ?: ""
                    val forumName = uri.getQueryParameter("forum_name") ?: ""
                    val forumId = uri.getQueryParameter("forum_id") ?: ""
                    val forumIcon = uri.getQueryParameter("forum_icon") ?: ""
                    val forumType = uri.getQueryParameter("forum_type") ?: BbsType.OFFICIAL_BBS.value
                    val gameId = uri.getQueryParameter("game_id") ?: ""
                    val gameName = uri.getQueryParameter("game_name") ?: ""
                    val icon = uri.getQueryParameter("game_icon") ?: ""
                    val iconSubscript = uri.getQueryParameter("game_icon_subscript") ?: ""
                    val gameEntity =
                        if (forumType == BbsType.OFFICIAL_BBS.value && gameId.isNotEmpty() && gameName.isNotEmpty() && icon.isNotEmpty()) {
                            GameEntity(id = gameId, mName = gameName, mIcon = icon, mIconSubscript = iconSubscript)
                        } else null
                    val activityLabelEntity = if (activityId.isNotEmpty() && activityName.isNotEmpty()) {
                        ActivityLabelEntity(
                            id = activityId,
                            name = activityName,
                            original = original.ifEmpty { "false" }.toBoolean()
                        )
                    } else null
                    val communityEntity =
                        if (forumId.isNotEmpty() && forumName.isNotEmpty() && forumIcon.isNotEmpty()) {
                            CommunityEntity(id = forumId, name = forumName, icon = forumIcon)
                        } else null

                    context.startActivity(
                        VideoPublishActivity.getIntent(
                            context,
                            communityEntity,
                            gameEntity,
                            activityLabelEntity,
                            forumType,
                            disableForumSelection = false,
                            isFromCommunityActivity = true,
                            entrance,
                            ""
                        )
                    )
                }

                EntranceConsts.HOST_SUGGESTION -> {
                    val platform = uri.getQueryParameter(EntranceConsts.KEY_PLATFORM)
                    val platformName = PlatformUtils.getInstance(context).getPlatformName(platform)
                    val gameId = uri.getQueryParameter(EntranceConsts.KEY_GAMEID)
                    val packageMd5 = uri.getQueryParameter(EntranceConsts.KEY_PACKAGE_MD5)
                    val isQaFeedback = uri.getQueryParameter(EntranceConsts.KEY_IS_QA_FEEDBACK) == "true"
                    val content = if (TextUtils.isEmpty(gameId) || TextUtils.isEmpty(packageMd5)) String.format(
                        "%s—V%s—%s（V%s），",
                        uri.getQueryParameter(EntranceConsts.KEY_GAME_NAME),
                        uri.getQueryParameter(EntranceConsts.KEY_VERSION),
                        if (TextUtils.isEmpty(platformName)) platform else platformName,
                        uri.getQueryParameter(EntranceConsts.KEY_PLATFORM_VERSION),
                    ) else String.format(
                        "%s-%s-V%s\n游戏ID：%s\n游戏包MD5：%s\n",
                        uri.getQueryParameter(EntranceConsts.KEY_GAME_NAME),
                        if (TextUtils.isEmpty(platformName)) platform else platformName,
                        uri.getQueryParameter(EntranceConsts.KEY_VERSION), gameId, packageMd5
                    )
                    val qaId = uri.getQueryParameter("qa_id") ?: ""
                    val qaContentId = uri.getQueryParameter(EntranceConsts.KEY_QA_CONTENT_ID) ?: ""
                    val qaTitle = uri.getQueryParameter(EntranceConsts.KEY_QA_TITLE)
                    if (!TextUtils.isEmpty(qaId)) {
                        directToQa(context, qaTitle, qaId)
                    } else {
                        directToFeedback(
                            context,
                            content,
                            null,
                            isQaFeedback,
                            qaContentId,
                            EntranceConsts.ENTRANCE_BROWSER
                        )
                    }
                }

                EntranceConsts.HOST_HELP_AND_FEEDBACK -> {
                    val position = uri.getQueryParameter("position") ?: ""
                    DirectUtils.directToHelpAndFeedback(context, position.toInt())
                }

                EntranceConsts.HOST_HELP_DETAIL -> {
                    var url = uri.getQueryParameter("url")
                    if (!url.isNullOrEmpty()) {
                        context.startActivity(WebActivity.getIntent(context, url, false))
                    } else {
                        url = if (EnvHelper.isDevEnv) {
                            Constants.HELP_ADDRESS_DEV
                        } else {
                            Constants.HELP_ADDRESS
                        }
                        val id = uri.getQueryParameter("id")
                        val name = uri.getQueryParameter("name")
                        val qaCollectionId = uri.getQueryParameter("collection_id")
                        context.startActivity(
                            WebActivity.getIntent(
                                context,
                                "$url$id",
                                name,
                                true,
                                !qaCollectionId.isNullOrEmpty()
                            )
                        )
                    }
                }

                EntranceConsts.HOST_GAME_COLLECTION_DETAIL -> {
                    DirectUtils.directToGameCollectionDetail(context, id, entrance)
                }

                EntranceConsts.HOST_GAME_COLLECTION_SQUARE -> {
                    DirectUtils.directToGameCollectionSquare(context, entrance)
                }

                EntranceConsts.HOST_GAME_COLLECTION_EDIT -> {
                    val activityId = uri.getQueryParameter("activity_id") ?: ""
                    val activityName = uri.getQueryParameter("activity_name") ?: ""
                    val gameId = uri.getQueryParameter("game_id") ?: ""
                    context.startActivity(
                        GameCollectionEditActivity.getIntent(
                            context,
                            activityId,
                            activityName,
                            gameId,
                            entrance
                        )
                    )
                }

                EntranceConsts.HOST_GAME_LIBRARY -> {
                    DirectUtils.directToMainActivity(context)
                    EventBus.getDefault().post(EBSkip(MainActivity.EB_SKIP_MAIN, MainWrapperFragment.INDEX_GAME))
                }

                EntranceConsts.HOST_HOME_GAME_COLLECTION_SQUARE -> {
                    DirectUtils.directToMainActivity(context)
                    EventBus.getDefault()
                        .post(EBSkip(MainActivity.EB_SKIP_MAIN, MainWrapperFragment.INDEX_HOME))
                    EventBus.getDefault().post(EBReuse(host))
                }

                else -> {
                    if (bringAppToFront) {
                        DirectUtils.directToMainActivity(context)
                        if (!TextUtils.isEmpty(host)) {
                            AppExecutor.uiExecutor.executeWithDelay({
                                CurrentActivityHolder.getCurrentActivity()?.let {
                                    DialogHelper.showUpgradeDialog(it)
                                }
                            }, 200)
                        }
                    } else {
                        DialogHelper.showUpgradeDialog(context)
                    }
                }
            }
            return true
        } else if ("zhiqu" == uri.scheme) {
            if (PackageUtils.isInstalled(context, "com.beieryouxi.zqyxh")) {
                val intent = Intent()
                intent.data = Uri.parse(url)
                intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
                context.startActivity(intent)
            } else {
                Utils.toast(context, "请安装指趣游戏盒")
            }
        }

        if (url.startsWith("alipays:") || url.startsWith("alipay")) {
            try {
                context.startActivity(Intent("android.intent.action.VIEW", Uri.parse(url)))
            } catch (e: Exception) {
                ToastUtils.showToast("请安装支付宝客户端")
            }
            return true
        } else if (url.startsWith("weixin")) {
            try {
                context.startActivity(Intent("android.intent.action.VIEW", Uri.parse(url)))
            } catch (e: Exception) {
                ToastUtils.showToast("请安装微信客户端")
            }
            return true
        } else if (url.startsWith("mqqwpa")) {
            try {
                context.startActivity(Intent("android.intent.action.VIEW", Uri.parse(url)))
            } catch (e: Exception) {
                ToastUtils.showToast("请安装QQ客户端")
            }
            return true
        }

        // 处理内部页面逻辑
        if (transformNormalScheme(context, url, entrance)) {
            return true
        }

        if ("http" != uri.scheme && "https" != uri.scheme) return true
        return false
    }

    @JvmStatic
    fun transformNormalScheme(context: Context, url: String, entrance: String): Boolean {
        val b = transformNewNormalScheme(context, url, entrance)
        if (b) return b
        return transformOldNormalScheme(context, url, entrance)
    }

    @JvmStatic
    fun transformOldNormalScheme(context: Context, url: String, entrance: String): Boolean {
        val uri = Uri.parse(url)
        if (uri.host == "www.ghzs666.com"
            || uri.host == "www.ghzs.com"
            || uri.host == "ask.ghzs.com"
            || uri.host == "m.ghzs.com"
            || uri.host == "m.ghzs666.com"
        ) {
            Utils.log(uri.path)
            uri.path?.apply {
                when {
                    contains("game") -> {
                        val gameId = uri.getQueryParameter("gameId") ?: uri.pathSegments.last() ?: ""
                        DirectUtils.directToGameDetail(
                            context,
                            gameId,
                            entrance,
                            autoDownload = false,
                            traceEvent = null
                        )
                    }
                    contains("question") -> {
                        val questionId = split("/")[2]
                        val answerId = uri.getQueryParameter("answer")
                        if (answerId.isNullOrEmpty()) {
                            DirectUtils.directToQuestionDetail(context, questionId, entrance, "")
                        } else {
                            DirectUtils.directToAnswerDetail(context, answerId, entrance, "")
                        }
                    }
                    ((contains("bbs")) && contains("article") ||
                            (contains("communities")) && contains("article")) -> {
                        var communityId = ""
                        var type = ""
                        var typeId = ""
                        val split =
                            replace("/communities", "").replace("/bbs", "").replace(".html", "").split("/".toRegex())
                                .dropLastWhile { it.isEmpty() }
                                .toTypedArray()
                        for (text in split) {
                            if (TextUtils.isEmpty(communityId)) {
                                communityId = text
                                continue
                            }
                            if (TextUtils.isEmpty(type)) {
                                type = text
                                continue
                            }
                            if (TextUtils.isEmpty(typeId)) {
                                typeId = text
                            }
                        }
                        if ("articles" == type || "article" == type) {
                            DirectUtils.directToCommunityArticle(
                                context, typeId, communityId,
                                entrance, "文章链接"
                            )
                        }
                    }
                    contains("article") -> {
                        val articleId = split("/")[2].replace(".html", "")
                        if (entrance == "隐私政策") {
                            DirectUtils.directToArticle(context, articleId, true, entrance)
                        } else {
                            DirectUtils.directToArticle(context, articleId, entrance)
                        }
                    }
                    contains("columns") -> {
                        val columnsId = split("/")[3]
                        val id = uri.getQueryParameter("communityId") ?: ""
                        val name = uri.getQueryParameter("communityName") ?: ""
                        DirectUtils.directToCommunityColumn(context, CommunityEntity(id, name), columnsId, entrance, "")
                    }
                    contains("zone") && split("/").size > 2 -> {
                        val gameId = split("/")[2]
                        DirectUtils.directGameZone(context, gameId, url, entrance)
                    }
                    else -> return false
                }
            }
            return true
        }
        return false
    }

    @JvmStatic
    fun transformNewNormalScheme(context: Context, url: String, entrance: String): Boolean {
        val uri = Uri.parse(url)
        if (uri.host == "www.ghzs666.com"
            || uri.host == "www.ghzs.com"
            || uri.host == "ask.ghzs.com"
            || uri.host == "m.ghzs.com"
            || uri.host == "m.ghzs666.com"
            || uri.host == "dev-bbs-mobile.ghzs.com"
        ) {
            Utils.log(uri.path)
            uri.path?.apply {
                val splits = split("/")
                when {
                    //https://m.ghzs666.com/bbs/thread-帖子ID
                    splits.size >= 3 && splits[1] == "bbs" && splits[2].startsWith("thread-") -> {
                        val articleId = splits[2].substring(7)
                        DirectUtils.directToCommunityArticle(
                            context, articleId, "",
                            entrance, "文章链接"
                        )
                    }
                    //https://m.ghzs666.com/article/文章ID
                    splits.size >= 3 && splits[1] == "article" -> {
                        val articleId = splits[2]
                        DirectUtils.directToArticle(context, articleId, entrance)
                    }
                    //https://m.ghzs666.com/column/专题ID
                    splits.size >= 3 && splits[1] == "column" -> {
                        val columnId = splits[2]
                        DirectUtils.directToSubject(context, columnId, "", entrance)
                    }
                    //https://m.ghzs666.com/zone/游戏ID
                    splits.size >= 3 && splits[1] == "zone" -> {
                        DirectUtils.directToWebView(context, url, entrance)
                    }
                    //https://m.ghzs666.com/bbs/video-视频ID
                    splits.size >= 3 && splits[1] == "bbs" && splits[2].startsWith("video-") -> {
                        val videoId = splits[2].substring(6)
                        DirectUtils.directToVideoDetail(context, videoId, entrance)
                    }
                    else -> return false
                }
            }
            return true
        }
        return false
    }

    /**
     * 将 url 转换为 LinkEntity (实际只有 type 和 link 两个字段，仅供日志，不保证能用)
     */
    fun urlToLinkEntity(url: String): LinkEntity? {
        val uri = Uri.parse(url)
        if ("ghzhushou" == uri.scheme) {
            Utils.log("url = $url")
            Utils.log("url = " + uri.scheme!!)
            val host = uri.host
            val path = uri.path
            var id = ""
            if (!TextUtils.isEmpty(path)) {
                id = path!!.substring(1)
            }
            return LinkEntity(type = host, link = id)
        }
        return null
    }

}