package com.gh.gamecenter;

import static com.gh.gamecenter.common.constant.EntranceConsts.ENTRANCE_BROWSER;
import static com.gh.gamecenter.common.constant.EntranceConsts.HOST_ANSWER;
import static com.gh.gamecenter.common.constant.EntranceConsts.HOST_ARTICLE;
import static com.gh.gamecenter.common.constant.EntranceConsts.HOST_INVOKE_ONLY;
import static com.gh.gamecenter.common.constant.EntranceConsts.HOST_CATEGORY;
import static com.gh.gamecenter.common.constant.EntranceConsts.HOST_COLUMN;
import static com.gh.gamecenter.common.constant.EntranceConsts.HOST_COLUMN_COLLECTION;
import static com.gh.gamecenter.common.constant.EntranceConsts.HOST_COMMUNITY;
import static com.gh.gamecenter.common.constant.EntranceConsts.HOST_COMMUNITY_COLUMN;
import static com.gh.gamecenter.common.constant.EntranceConsts.HOST_COMMUNITY_COLUMN_DETAIL;
import static com.gh.gamecenter.common.constant.EntranceConsts.HOST_COMMUNITY_QUESTION_LABEL_DETAIL;
import static com.gh.gamecenter.common.constant.EntranceConsts.HOST_DOWNLOAD;
import static com.gh.gamecenter.common.constant.EntranceConsts.HOST_GAME;
import static com.gh.gamecenter.common.constant.EntranceConsts.HOST_GAME_COLLECTION_DETAIL;
import static com.gh.gamecenter.common.constant.EntranceConsts.HOST_GAME_COLLECTION_SQUARE;
import static com.gh.gamecenter.common.constant.EntranceConsts.HOST_LIBAO;
import static com.gh.gamecenter.common.constant.EntranceConsts.HOST_QQ;
import static com.gh.gamecenter.common.constant.EntranceConsts.HOST_QQ_GROUP;
import static com.gh.gamecenter.common.constant.EntranceConsts.HOST_QQ_QUN;
import static com.gh.gamecenter.common.constant.EntranceConsts.HOST_QUESTION;
import static com.gh.gamecenter.common.constant.EntranceConsts.HOST_SUGGESTION;
import static com.gh.gamecenter.common.constant.EntranceConsts.HOST_TOOLBOX;
import static com.gh.gamecenter.common.constant.EntranceConsts.HOST_UPLOAD_VIDEO;
import static com.gh.gamecenter.common.constant.EntranceConsts.HOST_USERHOME;
import static com.gh.gamecenter.common.constant.EntranceConsts.HOST_VIDEO;
import static com.gh.gamecenter.common.constant.EntranceConsts.HOST_VIDEO_COLLECTION;
import static com.gh.gamecenter.common.constant.EntranceConsts.HOST_VIDEO_MORE;
import static com.gh.gamecenter.common.constant.EntranceConsts.HOST_VIDEO_SINGLE;
import static com.gh.gamecenter.common.constant.EntranceConsts.HOST_VIDEO_STREAMING_DESC;
import static com.gh.gamecenter.common.constant.EntranceConsts.HOST_VIDEO_STREAMING_HOME;
import static com.gh.gamecenter.common.constant.EntranceConsts.HOST_WEB;
import static com.gh.gamecenter.common.constant.EntranceConsts.KEY_DATA;
import static com.gh.gamecenter.common.constant.EntranceConsts.KEY_GAME_NAME;
import static com.gh.gamecenter.common.constant.EntranceConsts.KEY_NAME;
import static com.gh.gamecenter.common.constant.EntranceConsts.KEY_PACKAGENAME;
import static com.gh.gamecenter.common.constant.EntranceConsts.KEY_PLATFORM;
import static com.gh.gamecenter.common.constant.EntranceConsts.KEY_TO;
import static com.gh.gamecenter.common.constant.EntranceConsts.KEY_TYPE;
import static com.gh.gamecenter.common.constant.EntranceConsts.KEY_VERSION;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Base64;

import com.gh.gamecenter.common.base.activity.BaseActivity;
import com.gh.common.util.CheckLoginUtils;
import com.gh.common.util.DirectUtils;
import com.gh.gamecenter.common.constant.EntranceConsts;
import com.gh.common.util.EntranceUtils;
import com.gh.common.util.PlatformUtils;
import com.gh.gamecenter.core.utils.CurrentActivityHolder;
import com.gh.gamecenter.core.utils.GsonUtils;
import com.gh.gamecenter.common.entity.CommunityEntity;
import com.gh.gamecenter.common.entity.LinkEntity;
import com.gh.gamecenter.entity.SimpleGameEntity;
import com.gh.gamecenter.entity.SubjectRecommendEntity;
import com.gh.gamecenter.entity.VideoLinkEntity;
import com.gh.gamecenter.video.detail.VideoDetailContainerViewModel;
import com.gh.gamecenter.video.videomanager.VideoManagerActivity;
import com.halo.assistant.HaloApp;
import com.lightgame.config.CommonDebug;
import com.lightgame.utils.Utils;

/**
 * Created by LGT on 2016/11/16.
 * 链接跳转用
 */
public class SkipActivity extends BaseActivity {

    @Override
    protected int getLayoutId() {
        return R.layout.activity_shell;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Uri uri = getIntent().getData();
        Bundle bundle;
        if (uri != null) {
            if ("ghzhushou".equals(uri.getScheme())) {
                if (CommonDebug.IS_DEBUG) {
                    Utils.log("SkipActivity:: Uri=>" + uri.toString());
                }
                String host = uri.getHost();
                String path = uri.getPath();

                String to = uri.getQueryParameter("to");
                String type = uri.getQueryParameter("type");
                String name = uri.getQueryParameter("name");
                String referer = uri.getQueryParameter("referer");
                String id = uri.getQueryParameter("id");
                if (!TextUtils.isEmpty(path)) {
                    path = path.substring(1);
                }

                if (host != null) {
                    Intent intent;
                    switch (host) {
                        case HOST_INVOKE_ONLY:
                            // 仅唤起光环助手，不进入光环页面。用于类似 ContentProvider 的用途
                            break;
                        case HOST_ARTICLE:
                            DirectUtils.directToArticle(this, path, ENTRANCE_BROWSER);
                            break;
                        case HOST_GAME:
                            DirectUtils.directToGameDetail(this, path, ENTRANCE_BROWSER, "true".equals(uri.getQueryParameter("auto_download")), to, null);
                            break;
                        case HOST_COLUMN:
                            DirectUtils.directToSubject(this, path, uri.getQueryParameter(KEY_NAME), ENTRANCE_BROWSER, null);
                            break;
                        case HOST_SUGGESTION:
                            String platform = uri.getQueryParameter(KEY_PLATFORM);
                            String platformName = PlatformUtils.getInstance(this).getPlatformName(platform);
                            String gameId = uri.getQueryParameter(EntranceConsts.KEY_GAMEID);
                            String packageMd5 = uri.getQueryParameter(EntranceConsts.KEY_PACKAGE_MD5);
                            String isQaFeedbackString = uri.getQueryParameter(EntranceConsts.KEY_IS_QA_FEEDBACK);
                            String suggestionType = uri.getQueryParameter(KEY_TYPE);
                            boolean isQaFeedback = !TextUtils.isEmpty(isQaFeedbackString) && isQaFeedbackString.equals("true");
                            String content = (TextUtils.isEmpty(gameId) || TextUtils.isEmpty(packageMd5)) ?
                                    String.format("%s-%s-V%s，",
                                            uri.getQueryParameter(KEY_GAME_NAME),
                                            TextUtils.isEmpty(platformName) ? platform : platformName,
                                            uri.getQueryParameter(KEY_VERSION)) :
                                    String.format("%s-%s-V%s\n游戏ID：%s\n游戏包MD5：%s\n",
                                            uri.getQueryParameter(KEY_GAME_NAME),
                                            TextUtils.isEmpty(platformName) ? platform : platformName,
                                            uri.getQueryParameter(KEY_VERSION), gameId, packageMd5);
                            String qaId = uri.getQueryParameter("qa_id");
                            String qaContentId = uri.getQueryParameter(EntranceConsts.KEY_QA_CONTENT_ID);
                            String qaTitle = uri.getQueryParameter(EntranceConsts.KEY_QA_TITLE);
                            if (!TextUtils.isEmpty(qaId)) {
                                DirectUtils.directToQa(this, qaTitle, qaId);
                            } else if ("vgame".equals(suggestionType)) {
                                DirectUtils.directToFeedbackCompat(this, content, "game", isQaFeedback, qaContentId, EntranceConsts.ENTRANCE_BROWSER);
                            } else {
                                DirectUtils.directToFeedback(this, content, null, isQaFeedback, qaContentId, EntranceConsts.ENTRANCE_BROWSER);
                            }
                            break;
                        case HOST_DOWNLOAD:
                            DirectUtils.directToDownloadManagerAndStartUpdate(this, path, uri.getQueryParameter(KEY_PACKAGENAME), ENTRANCE_BROWSER);
                            break;
                        case HOST_ANSWER:
                            DirectUtils.directToAnswerDetail(this, path, ENTRANCE_BROWSER, "浏览器");
                            break;
                        case HOST_QUESTION:
                            DirectUtils.directToQuestionDetail(this, path, ENTRANCE_BROWSER, "浏览器");
                            break;
                        case HOST_TOOLBOX:
                            DirectUtils.directToToolbox(this, uri.getQueryParameter("gameId"), uri.getQueryParameter("toolboxUrl"), ENTRANCE_BROWSER);
                            break;
                        case HOST_COMMUNITY:
                            // 把切换放到 MainActivity 处理
                            if (HaloApp.getInstance().isRunningForeground) {
                                intent = new Intent(this, MainActivity.class);
                                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                intent.putExtra(MainActivity.SWITCH_TO_COMMUNITY, true);
                            } else {
                                bundle = new Bundle();
                                bundle.putBoolean(MainActivity.SWITCH_TO_COMMUNITY, true);
                                intent = SplashScreenActivity.getSplashScreenIntent(this, bundle);
                            }
                            startActivity(intent);
                            break;
                        // 社区文章格式一
                        case "community.article":
                            DirectUtils.directToCommunityArticle(this, uri.getQueryParameter("articleId"), uri.getQueryParameter("communityId"), ENTRANCE_BROWSER, "浏览器");
                            break;
                        // 社区文章格式二
                        case "communities":
                            String communityId = "";
                            String typeId = "";
                            String[] split = path.split("/");
                            for (String text : split) {
                                if (TextUtils.isEmpty(communityId)) {
                                    communityId = text;
                                    continue;
                                }
                                if (TextUtils.isEmpty(type)) {
                                    type = text;
                                    continue;
                                }
                                if (TextUtils.isEmpty(typeId)) {
                                    typeId = text;
                                }
                            }
                            if ("articles".equals(type)) {
                                DirectUtils.directToCommunityArticle(this, typeId, communityId, ENTRANCE_BROWSER, "浏览器");
                                break;
                            }
                            break;
                        case HOST_VIDEO:
                            DirectUtils.directToVideoDetail(this, path, VideoDetailContainerViewModel.Location.HOTTEST_GAME_VIDEO.getValue(),
                                    false, id, ENTRANCE_BROWSER, "浏览器", TextUtils.isEmpty(referer) ? "" : referer);
                            break;
                        case HOST_UPLOAD_VIDEO://跳转上传视频
                            String titleParameter = uri.getQueryParameter("title");
                            String title = TextUtils.isEmpty(titleParameter) ? "" : "#" + titleParameter + "#";
                            String categoryId = uri.getQueryParameter("category_id");
                            String link = uri.getQueryParameter("link");
                            gameId = uri.getQueryParameter("gameId");
                            String gameName = uri.getQueryParameter("gameName");
                            String tagActivityId = uri.getQueryParameter("tagActivityId");
                            String tagActivityName = uri.getQueryParameter("tagActivityName");
                            VideoLinkEntity linkEntity = new VideoLinkEntity(title, categoryId, link, tagActivityId, tagActivityName);

                            SimpleGameEntity simpleGameEntity = new SimpleGameEntity(gameId != null ? gameId : "", gameName != null ? gameName : "", "");
                            Bundle nextToBundle = VideoManagerActivity.getVideoManagerBundle(linkEntity, simpleGameEntity, EntranceConsts.ENTRANCE_BROWSER, "");
                            CheckLoginUtils.checkLogin(this, nextToBundle, true, EntranceConsts.ENTRANCE_BROWSER, () ->
                                    DirectUtils.directToVideoManager(SkipActivity.this, linkEntity, simpleGameEntity, EntranceConsts.ENTRANCE_BROWSER, "浏览器"));
                            break;
                        case HOST_VIDEO_SINGLE:
                            DirectUtils.directToVideoDetail(this, path, VideoDetailContainerViewModel.Location.SINGLE_VIDEO.getValue(),
                                    false, "", ENTRANCE_BROWSER, "浏览器", TextUtils.isEmpty(referer) ? "" : referer);
                            break;
                        case HOST_VIDEO_MORE:
                            gameId = uri.getQueryParameter("gameId");
                            String act = uri.getQueryParameter("act");
                            String fieldId = uri.getQueryParameter("fieldId");
                            String sectionName = uri.getQueryParameter("sectionName");
                            String paginationType = uri.getQueryParameter("paginationType");//活动分页方式 page filter
                            String location;
                            if (!TextUtils.isEmpty(act)) {
                                location = VideoDetailContainerViewModel.Location.VIDEO_ACTIVITY.getValue();
                            } else if (!TextUtils.isEmpty(fieldId)) {
                                location = VideoDetailContainerViewModel.Location.GAME_ZONE.getValue();
                            } else {
                                location = path;
                            }
                            DirectUtils.directToLegacyVideoDetail(this, path, location,
                                    false, TextUtils.isEmpty(gameId) ? "" : gameId, ENTRANCE_BROWSER, "浏览器", TextUtils.isEmpty(referer) ? "" : referer,
                                    TextUtils.isEmpty(type) ? "" : type, TextUtils.isEmpty(act) ? "" : act, TextUtils.isEmpty(paginationType) ? "page" : paginationType, TextUtils.isEmpty(fieldId) ? "" : fieldId,
                                    TextUtils.isEmpty(sectionName) ? "" : sectionName);
                            break;
                        case HOST_VIDEO_STREAMING_HOME:
                            // 把切换放到 MainActivity 处理
                            if (HaloApp.getInstance().isRunningForeground) {
                                intent = new Intent(this, MainActivity.class);
                                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                intent.putExtra(MainActivity.SWITCH_TO_VIDEO, true);
                            } else {
                                bundle = new Bundle();
                                bundle.putBoolean(MainActivity.SWITCH_TO_VIDEO, true);
                                intent = SplashScreenActivity.getSplashScreenIntent(this, bundle);
                            }
                            startActivity(intent);
                            break;
                        case HOST_VIDEO_STREAMING_DESC:
                            DirectUtils.directToGameDetailVideoStreaming(this, path, ENTRANCE_BROWSER);
                            break;
                        case HOST_VIDEO_COLLECTION:
                            DirectUtils.directToGameVideo(this, path, ENTRANCE_BROWSER, "浏览器");
                            break;
                        case HOST_QQ:
                            bundle = new Bundle();
                            bundle.putString(KEY_TO, HOST_QQ);
                            bundle.putString(KEY_DATA, path);
                            EntranceUtils.jumpActivity(this, bundle);
                            break;
                        case HOST_QQ_GROUP:
                            bundle = new Bundle();
                            bundle.putString(KEY_TO, HOST_QQ_GROUP);
                            bundle.putString(KEY_DATA, path);
                            EntranceUtils.jumpActivity(this, bundle);
                            break;
                        case HOST_QQ_QUN:
                            String key = uri.getQueryParameter("key");
                            bundle = new Bundle();
                            bundle.putString(KEY_TO, HOST_QQ_GROUP);
                            bundle.putString(KEY_DATA, key);
                            EntranceUtils.jumpActivity(this, bundle);
                            break;
                        case HOST_WEB:
                            bundle = new Bundle();
                            bundle.putString(KEY_TO, HOST_WEB);
                            bundle.putString(KEY_DATA, to);
                            bundle.putString(KEY_TYPE, type);
                            EntranceUtils.jumpActivity(this, bundle);
                            break;
                        case EntranceConsts.HOST_VIDEO_DETAIL:
                            DirectUtils.directToVideoDetail(this, path, ENTRANCE_BROWSER, "");
                            break;
                        case HOST_LIBAO:
                            DirectUtils.directToGiftDetail(this, path, ENTRANCE_BROWSER);
                            break;
                        case HOST_USERHOME:
                            String position = uri.getQueryParameter("position");
                            String subTypeString = uri.getQueryParameter("sub_type");
                            String subGameType = uri.getQueryParameter("sub_game_type");

                            DirectUtils.directToHomeActivity(this, path, subTypeString, subGameType, TextUtils.isEmpty(position) ? -1 : Integer.parseInt(position), ENTRANCE_BROWSER, "浏览器");
                            break;
                        case HOST_COMMUNITY_COLUMN:
                            CommunityEntity community = new CommunityEntity();
                            community.setId(uri.getQueryParameter("community_id"));
                            community.setName(uri.getQueryParameter("community_name"));
                            String columnId = uri.getQueryParameter("column_id");
                            DirectUtils.directToCommunityColumn(this, community, columnId, ENTRANCE_BROWSER, "");
                            break;

                        case HOST_CATEGORY:
                            title = uri.getQueryParameter("title");
                            DirectUtils.directCategoryDirectory(this, path, title, ENTRANCE_BROWSER, "浏览器");
                            break;
                        case HOST_COLUMN_COLLECTION:
                            DirectUtils.directToColumnCollection(this, path, -1, ENTRANCE_BROWSER, "浏览器");
                            break;
                        case HOST_COMMUNITY_QUESTION_LABEL_DETAIL:
                            community = new CommunityEntity();
                            community.setId(uri.getQueryParameter("community_id"));
                            community.setName(uri.getQueryParameter("community_name"));
                            String tag = uri.getQueryParameter("tag");
                            DirectUtils.directAskColumnLabelDetail(this, tag, community, ENTRANCE_BROWSER, "浏览器");
                            break;
                        case HOST_COMMUNITY_COLUMN_DETAIL:
                            community = new CommunityEntity();
                            community.setId(uri.getQueryParameter("community_id"));
                            community.setName(uri.getQueryParameter("community_name"));
                            columnId = uri.getQueryParameter("column_id");
                            DirectUtils.directAskColumnDetail(this, columnId, community, ENTRANCE_BROWSER, "浏览器");
                            break;
                        case EntranceConsts.HOST_BLOCK:
                            name = uri.getQueryParameter("name");
                            SubjectRecommendEntity entity = new SubjectRecommendEntity();
                            entity.setLink(path);
                            entity.setName(name);
                            entity.setText(name);
                            DirectUtils.directToBlock(this, entity, mEntrance);
                            break;

                        case EntranceConsts.HOST_SERVER_BLOCK:
                            DirectUtils.directToGameServers(this, ENTRANCE_BROWSER, "浏览器");
                            break;

                        case EntranceConsts.HOST_AMWAY_BLOCK:
                            DirectUtils.directToAmway(this, null, ENTRANCE_BROWSER, "浏览器");
                            break;

                        case EntranceConsts.HOST_HELP:
                            name = uri.getQueryParameter("name");
                            DirectUtils.directToQa(this, name, path);
                            break;

                        case EntranceConsts.HOST_HELP_COLLECTION:
                            name = uri.getQueryParameter("name");
                            DirectUtils.directToQaCollection(this, name, path);
                            break;
                        case EntranceConsts.HOST_GAME_UPLOAD:
                            DirectUtils.directGameUpload(this, ENTRANCE_BROWSER, "浏览器");
                            break;
                        case EntranceConsts.HOST_GAME_ZONE:
                            String zoneUrl = uri.getQueryParameter("url");
                            DirectUtils.directGameZone(this, path, zoneUrl, ENTRANCE_BROWSER);
                            break;
                        case EntranceConsts.HOST_LINK:
                            try {
                                String dataString = uri.getQueryParameter("data");
                                if (!TextUtils.isEmpty(dataString)) {
                                    byte[] linkData = Base64.decode(dataString, Base64.DEFAULT);
                                    String linkDataString = new String(linkData, "UTF-8");
                                    LinkEntity le = GsonUtils.INSTANCE.getGson().fromJson(linkDataString, LinkEntity.class);
                                    DirectUtils.directToLinkPage(this, le, ENTRANCE_BROWSER, "");
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                                toast(e.getMessage());
                            }
                            break;
                        case EntranceConsts.HOST_REAL_NAME:
                            DirectUtils.directToRealName(this);
                            break;
                        case EntranceConsts.HOST_GAME_NEWS:
                            DirectUtils.directToGameNews(
                                    this,
                                    uri.getQueryParameter(EntranceConsts.KEY_GAME_ID),
                                    uri.getQueryParameter(EntranceConsts.KEY_GAME_NAME),
                                    EntranceConsts.ENTRANCE_BROWSER);
                            break;
                        case EntranceConsts.HOST_GAME_CALENDAR:
                            DirectUtils.directToGameServerCalendar(this, uri.getQueryParameter(EntranceConsts.KEY_GAME_ID));
                            break;
                        case EntranceConsts.HOST_HISTORY_APK:
                            DirectUtils.directToHistoryApk(this, uri.getQueryParameter(EntranceConsts.KEY_GAME_ID));
                            break;
                        case EntranceConsts.HOST_FORUM_DETAIL:
                            DirectUtils.directForumDetail(this, id, ENTRANCE_BROWSER);
                            break;
                        case EntranceConsts.HOST_GAME_RATING_DETAIL:
                            DirectUtils.directToGameRatingDetail(this, uri.getQueryParameter(EntranceConsts.KEY_GAME_ID), uri.getQueryParameter(EntranceConsts.KEY_COMMENT_ID), ENTRANCE_BROWSER);
                            break;
                        case EntranceConsts.HOST_FORUM:
                            position = uri.getQueryParameter("position");
                            DirectUtils.directToForum(this, TextUtils.isEmpty(position) ? 0 : Integer.parseInt(position));
                            break;
                        case EntranceConsts.HOST_HELP_AND_FEEDBACK:
                            position = uri.getQueryParameter("position");
                            DirectUtils.directToHelpAndFeedback(this, TextUtils.isEmpty(position) ? 0 : Integer.parseInt(position));
                            break;
                        case HOST_GAME_COLLECTION_DETAIL:
                            DirectUtils.directToGameCollectionDetail(this, path, ENTRANCE_BROWSER, "");
                            break;
                        case HOST_GAME_COLLECTION_SQUARE:
                            DirectUtils.directToGameCollectionSquare(this, ENTRANCE_BROWSER, "", "", "");
                            break;
                        default:
                            EntranceUtils.jumpActivity(this, new Bundle()); // 跳转至首页
                            return;
                    }
                }
            } else if ("market".equals(uri.getScheme())) {
                String host = uri.getHost();
                String id = "";
                try {
                    id = uri.getQueryParameter("id");
                } catch (UnsupportedOperationException e) {
                    e.printStackTrace();
                }
                if (host != null) {
                    if ("details".equals(host)) {
                        bundle = new Bundle();
                        bundle.putString(KEY_TO, EntranceConsts.KEY_MARKET_DETAILS);
                        bundle.putString(KEY_DATA, id);
                        EntranceUtils.jumpActivity(this, bundle);
                    } else {
                        EntranceUtils.jumpActivity(this, new Bundle()); // 跳转至首页
                    }
                }
            }
        }

        finish();
    }
}
