package com.gh.common.util

import android.os.Bundle
import androidx.fragment.app.Fragment
import com.gh.common.iinterface.ISuperiorChain
import com.gh.gamecenter.amway.AmwayFragment
import com.gh.gamecenter.category2.CategoryV2Fragment
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.common.entity.LinkEntity
import com.gh.gamecenter.common.provider.IHelpAndFeedbackProvider
import com.gh.gamecenter.discovery.DiscoveryFragment
import com.gh.gamecenter.entity.SubjectData
import com.gh.gamecenter.forum.detail.ForumDetailFragment
import com.gh.gamecenter.forum.home.CommunityHomeFragment
import com.gh.gamecenter.fragment.ReloadFragment
import com.gh.gamecenter.game.columncollection.detail.ColumnCollectionDetailFragment
import com.gh.gamecenter.game.commoncollection.detail.CustomCommonCollectionDetailFragment
import com.gh.gamecenter.gamecollection.hotlist.GameCollectionHotListWrapperFragment
import com.gh.gamecenter.gamecollection.square.GameCollectionSquareFragment
import com.gh.gamecenter.gamedetail.GameDetailWrapperFragment
import com.gh.gamecenter.info.InfoWrapperFragment
import com.gh.gamecenter.libao.LibaoDetailFragment
import com.gh.gamecenter.libao.LibaoFragment
import com.gh.gamecenter.newsdetail.NewsDetailFragment
import com.gh.gamecenter.personal.HaloPersonalFragment
import com.gh.gamecenter.qa.article.detail.ArticleDetailFragment
import com.gh.gamecenter.qa.questions.newdetail.NewQuestionDetailFragment
import com.gh.gamecenter.qa.subject.AskSubjectFragment
import com.gh.gamecenter.qa.video.detail.ForumVideoDetailFragment
import com.gh.gamecenter.servers.GameServersPublishFragment
import com.gh.gamecenter.servers.gametest2.GameServerTestV2Fragment
import com.gh.gamecenter.subject.SubjectFragment
import com.gh.gamecenter.tag.TagsActivity
import com.gh.gamecenter.tag.TagsListFragment
import com.gh.gamecenter.toolbox.ToolboxFragment
import com.gh.gamecenter.video.detail.HomeVideoFragment
import com.gh.gamecenter.wrapper.ToolbarWrapperFragment
import com.halo.assistant.fragment.WebFragment
import com.therouter.TheRouter

/**
 * 通用跳转Fragment方法
 * @see <a href="https://wiki.shanqu.cc/pages/viewpage.action?pageId=102828538">WIKI</a>
 */
object ViewPagerFragmentHelper {
    const val WRAPPER_FRAGMENT_NAME = "WrapperFragmentName"

    const val TYPE_MULTI_TAB_NAV = "multi_tab_nav"
    const val TYPE_CUSTOM_PAGE = "custom_page"

    const val TYPE_GAME = "game" // 游戏详情页
    const val TYPE_MY_HALO = "my_halo" // 我的光环
    const val TYPE_COMMUNITY_HOME = "community_home" // 社区首页
    const val TYPE_COMMUNITY = "community" // 论坛
    const val TYPE_COMMUNITY_SECTION = "bbs_section" // 论坛子版块
    const val TYPE_COMMUNITY_ARTICLE = "community_article" // 帖子
    const val TYPE_QUESTION = "question" // 提问
    const val TYPE_VIDEO_STREAM = "video_stream" // 视频信息流
    const val TYPE_VIDEO = "video" // 视频贴
    const val TYPE_AMWAY = "top_game_comment" // 安利墙
    const val TYPE_GAME_HOT_LIST = "game_hot_list" // 游戏单热搜榜
    const val TYPE_GAME_LIST = "game_list" // 游戏单广场
    const val TYPE_FEEDBACK = "feedback" // 帮助与反馈
    const val TYPE_COLUMN = "column" // 游戏专题详情页
    const val TYPE_QQ_MINI_GAME_COLUMN = "qq_mini_game_column_detail" // QQ小游戏专题详情页
    const val TYPE_WECHAT_GAME_COLUMN = "wechat_game_column_detail" // 微信小游戏专题详情页
    const val TYPE_WECHAT_GAME_CPM_COLUMN = "wechat_game_cpm_column_detail" // 微信小游戏CPM专题详情页
    const val TYPE_COLUMN_COLLECTION = "column_collection" // 专题合集详情页
    const val TYPE_SERVER = "server" // 开服表
    const val TYPE_COLUMN_TEST = "column_test_v2" // 新游开测
    const val TYPE_CATEGORY_V2 = "category_v2" // 分类2.0
    const val TYPE_COMMON_COLLECTION = "common_collection" // 通用内容合集详情页
    const val TYPE_WEB = "web" // web链接
    const val TYPE_EXPLORE = "game_explore" // 发现页
    const val TYPE_LIBAO = "libao" // 礼包详情页
    const val TYPE_LIBAO_CENTER = "libao_center" // 礼包中心
    const val TYPE_TAG = "tag" // 游戏标签详情
    const val TYPE_ARTICLE = "article" // 文章
    const val TYPE_COMMUNITY_COLUMN = "community_column" // 社区专题
    const val TYPE_ARTICLE_CENTER = "article_center" // 资讯中心
    const val TYPE_QA = "qa" // QA
    const val TYPE_TOOLKIT = "toolkit" // 工具箱

    fun createFragment(parentFragment: Fragment?, bundle: Bundle, linkEntity: LinkEntity, isTabWrapper: Boolean): Fragment {
        val superiorChain = if (parentFragment is ISuperiorChain) parentFragment else null
        return when (linkEntity.type) {
            // 游戏详情页
            TYPE_GAME -> {
                bundle.putString(EntranceConsts.KEY_GAMEID, linkEntity.link)
                GameDetailWrapperFragment().with(bundle)
            }
            // 我的光环
            TYPE_MY_HALO -> {
                HaloPersonalFragment().setSuperiorChain(superiorChain).with(bundle)
            }
            // 社区首页
            TYPE_COMMUNITY_HOME -> {
                CommunityHomeFragment().setSuperiorChain(superiorChain).with(bundle)
            }
            // 视频信息流
            TYPE_VIDEO_STREAM -> {
                bundle.putBoolean(EntranceConsts.KEY_IS_HOME_VIDEO, true)
                HomeVideoFragment().with(bundle)
            }
            // 安利墙
            TYPE_AMWAY -> {
                AmwayFragment().with(bundle)
            }
            // 论坛
            TYPE_COMMUNITY -> {
                bundle.putString(EntranceConsts.KEY_BBS_ID, linkEntity.link)
                ForumDetailFragment().with(bundle)
            }
            // 论坛子版块
            TYPE_COMMUNITY_SECTION -> {
                bundle.putString(EntranceConsts.KEY_BBS_ID, linkEntity.community?.id)
                bundle.putString(EntranceConsts.KEY_BBS_SECTION_ID, linkEntity.link)
                ForumDetailFragment().with(bundle)
            }
            // 游戏单热搜榜
            TYPE_GAME_HOT_LIST -> {
                GameCollectionHotListWrapperFragment().with(bundle)
            }
            // 视频贴
            TYPE_VIDEO -> {
                bundle.putString(EntranceConsts.KEY_VIDEO_ID, linkEntity.link)
                ForumVideoDetailFragment().with(bundle)
            }
            // 帮助与反馈
            TYPE_FEEDBACK -> {
                val helpAndFeedbackProvider = TheRouter.get(IHelpAndFeedbackProvider::class.java)
                if (helpAndFeedbackProvider != null) {
                    helpAndFeedbackProvider.getHelpAndFeedbackFragment().with(bundle)
                } else {
                    // 纯粹是占位用的 fragment
                    AmwayFragment().with(bundle)
                }
            }
            // 帖子
            TYPE_COMMUNITY_ARTICLE -> {
                bundle.putString(EntranceConsts.KEY_COMMUNITY_ARTICLE_ID, linkEntity.link)
                bundle.putParcelable(EntranceConsts.KEY_COMMUNITY_DATA, linkEntity.community)
                ArticleDetailFragment().with(bundle)
            }
            // 提问
            TYPE_QUESTION -> {
                bundle.putString(EntranceConsts.KEY_QUESTIONS_ID, linkEntity.link)
                NewQuestionDetailFragment().with(bundle)
            }
            // 专题合集详情页
            TYPE_COLUMN_COLLECTION -> {
                bundle.putString(EntranceConsts.KEY_COLLECTION_ID, linkEntity.link)
                bundle.putInt(EntranceConsts.KEY_POSITION, 0)
                bundle.putString(EntranceConsts.KEY_COLUMNNAME, linkEntity.text)
                bundle.putBoolean(EntranceConsts.KEY_IS_COLUMN_COLLECTION, true)
                bundle.putString(EntranceConsts.KEY_SUBJECT_TYPE, "tab")
                ColumnCollectionDetailFragment().with(bundle)
            }
            // 其他原来带Toolbar的Fragment
            else -> createToolbarWrapperFragment(bundle, linkEntity, isTabWrapper)
        }
    }

    private fun createToolbarWrapperFragment(bundle: Bundle, entity: LinkEntity, isTabWrapper: Boolean): Fragment {
        var className = ReloadFragment::class.java.name

        when (entity.type) {
            // 游戏单广场
            TYPE_GAME_LIST -> {
                className = GameCollectionSquareFragment::class.java.name
            }
            // 游戏专题详情页/QQ游戏专题详情页
            TYPE_COLUMN, TYPE_QQ_MINI_GAME_COLUMN, TYPE_WECHAT_GAME_COLUMN, TYPE_WECHAT_GAME_CPM_COLUMN -> {
                val subjectType = when(entity.type) {
                    TYPE_QQ_MINI_GAME_COLUMN -> SubjectData.SubjectType.QQ_GAME
                    TYPE_WECHAT_GAME_COLUMN -> SubjectData.SubjectType.WECHAT_GAME
                    TYPE_WECHAT_GAME_CPM_COLUMN -> SubjectData.SubjectType.WECHAT_GAME_CPM
                    else -> SubjectData.SubjectType.NORMAL
                }
                className = SubjectFragment::class.java.name
                bundle.putParcelable(
                    EntranceConsts.KEY_SUBJECT_DATA,
                    SubjectData(entity.link, entity.text, false, subjectType = subjectType)
                )
                bundle.putString(EntranceConsts.KEY_SUBJECT_TYPE, "detail")
                bundle.putBoolean(EntranceConsts.KEY_SHOW_DOWNLOAD_MENU, !isTabWrapper)
            }
            // 开服表
            TYPE_SERVER -> {
                className = GameServersPublishFragment::class.java.name
                bundle.putBoolean(EntranceConsts.KEY_SHOW_DOWNLOAD_MENU, !isTabWrapper)
            }
            // 新游开测
            TYPE_COLUMN_TEST -> {
                className = GameServerTestV2Fragment::class.java.name
                bundle.putString(EntranceConsts.KEY_ID, entity.link)
                bundle.putString(EntranceConsts.KEY_NAME, entity.text)
            }
            // 分类2.0
            TYPE_CATEGORY_V2 -> {
                className = CategoryV2Fragment::class.java.name
                bundle.putString(EntranceConsts.KEY_PAGE_ID, entity.link)
                bundle.putString(EntranceConsts.KEY_PAGE_NAME, entity.text)
                bundle.putBoolean(EntranceConsts.KEY_SHOW_DOWNLOAD_MENU, !isTabWrapper)
            }
            // 通用内容合集详情页
            TYPE_COMMON_COLLECTION -> {
                className = CustomCommonCollectionDetailFragment::class.java.name
                bundle.putString(EntranceConsts.KEY_COLLECTION_ID, entity.link)
                bundle.putString(EntranceConsts.KEY_COLUMNNAME, entity.text)
            }
            // web链接
            TYPE_WEB -> {
                className = WebFragment::class.java.name
                bundle.putString(EntranceConsts.KEY_URL, entity.link)
                bundle.putBoolean(WebFragment.KEY_OPEN_NATIVE_PAGE, false)
                bundle.putBoolean(WebFragment.KEY_ENABLE_HORIZONTAL_SCROLL_DISPATCH, true)
                if (entity.link != null && entity.link!!.contains("leave_web_page_handle_back_pressed=true")) {
                    bundle.putBoolean(WebFragment.KEY_LEAVE_WEB_PAGE_TO_HANDLE_BACK_PRESSED, true)
                }
            }
            // 发现页
            TYPE_EXPLORE -> {
                className = DiscoveryFragment::class.java.name
            }
            // 礼包详情页
            TYPE_LIBAO -> {
                className = LibaoDetailFragment::class.java.name
                bundle.putString(EntranceConsts.KEY_ID, entity.link)
            }
            // 游戏标签详情
            TYPE_TAG -> {
                className = TagsListFragment::class.java.name
                bundle.putBoolean(EntranceConsts.KEY_SHOW_DOWNLOAD_MENU, !isTabWrapper)
                bundle.putString(TagsActivity.TAG, entity.text)
            }
            // 文章
            TYPE_ARTICLE -> {
                className = NewsDetailFragment::class.java.name
                bundle.putString(EntranceConsts.KEY_NEWSID, entity.link)
            }
            // 社区专题
            TYPE_COMMUNITY_COLUMN -> {
                className = AskSubjectFragment::class.java.name
                bundle.putString(EntranceConsts.KEY_COLUMN_ID, entity.link)
                bundle.putParcelable(EntranceConsts.KEY_COMMUNITY_DATA, entity.community)
            }
            // QA
            TYPE_QA -> {
                val helpAndFeedbackProvider = TheRouter.get(IHelpAndFeedbackProvider::class.java)

                helpAndFeedbackProvider?.let {
                    className = it.getHelpContainerFragmentClass()
                    bundle.putString(EntranceConsts.KEY_QA_ID, entity.link)
                    bundle.putString(EntranceConsts.KEY_NAVIGATION_TITLE, entity.text)
                }
            }
            // 资讯中心
            TYPE_ARTICLE_CENTER -> {
                className = InfoWrapperFragment::class.java.name
            }
            // 礼包中心
            TYPE_LIBAO_CENTER -> {
                className = LibaoFragment::class.java.name
            }
            // 工具箱
            TYPE_TOOLKIT -> {
                className = ToolboxFragment::class.java.name
            }
        }

        return if (isTabWrapper) {
            // 多Tab导航页不需要Toolbar
            (Class.forName(className).getDeclaredConstructor().newInstance() as Fragment).apply { arguments = bundle }
        } else {
            bundle.putString(WRAPPER_FRAGMENT_NAME, className)
            ToolbarWrapperFragment().with(bundle)
        }
    }
}