package com.sheep.gamegroup.view.activity;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.kfzs.cfyl.share_library.util.Constant;
import com.kfzs.duanduan.utils.ApkUtils;
import com.sheep.gamegroup.model.api.BaseMessageConverter;
import com.sheep.gamegroup.model.entity.HomeListEntity;
import com.sheep.gamegroup.model.entity.SlideshowEty;
import com.sheep.gamegroup.util.ActionUtil;
import com.sheep.gamegroup.util.CommonUtil;
import com.sheep.gamegroup.util.Jump2View;
import com.sheep.gamegroup.util.TestUtil;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.bean.UserCenterModule;
import com.sheep.jiuyan.samllsheep.utils.G;
import com.sheep.jiuyan.samllsheep.utils.PackageUtil;

import java.util.ArrayList;

/**
 * Created by realicing on 2018/5/10.
 * realicing@sina.com
 */
public class MiddleSchemeAct extends Activity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash_activity);
        checkScheme(getIntent());
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        checkScheme(intent);
    }

    private void checkScheme(Intent intent) {
        String action;
        if (intent == null || (action = intent.getAction()) == null)
            return;
        switch (action) {
            case Intent.ACTION_VIEW:
                Uri uri = intent.getData();
                if (uri != null) {
                    // 完整的url信息
                    if (TestUtil.isTest())
                        G.showToast(uri.toString());
                    parseUri(uri);
                }
                finish();
                break;
        }
    }

    private void parseUri(Uri uri) {
        String type = uri.getQueryParameter("type");
        if(type == null){
//            尝试获取game_id
            //sheep://small.kfzs.com/xmy?game_id=1385&code=976873
            tryGoSplashAct(uri);
            return;
        }
        //尝试解密数据
        String json = BaseMessageConverter.decrypt(uri.getQueryParameter("data"));
        switch (type){
            default://尝试获取game_id
                tryGoSplashAct(uri);
                break;
            //sheep://small.kfzs.com/sheep?type=jhl&data={"Jump":"1"}
            case "jump_home_list"://跳转homeList
            case "jhl":
                try {
                    HomeListEntity item = JSONObject.parseObject(json, HomeListEntity.class);
                    CommonUtil.getInstance().goNative(this, item, "view");
                } catch (Exception e) {
                    e.printStackTrace();
                    if(TestUtil.isTest() && TextUtils.isEmpty(e.getMessage()))
                        G.showToast(e.getMessage());
                }
                break;
            //sheep://small.kfzs.com/sheep?type=jum&data={"jump":1}
            case "jump_user_module"://跳转个人中心列表功能模块
            case "jum":
                try {
                    UserCenterModule item = JSONObject.parseObject(json, UserCenterModule.class);
                    CommonUtil.getInstance().goNative(this, item, "view");
                } catch (Exception e) {
                    e.printStackTrace();
                    if(TestUtil.isTest() && TextUtils.isEmpty(e.getMessage()))
                        G.showToast(e.getMessage());
                }
                break;
            //sheep://small.kfzs.com/sheep?type=jss&data={"Type":1}
            case "jump_slide_show"://跳转banner(主页banner或者游戏banner)
            case "jss":
                try {
                    SlideshowEty item = JSONObject.parseObject(json, SlideshowEty.class);
                    CommonUtil.getInstance().clickBanner(this, item);
                } catch (Exception e) {
                    e.printStackTrace();
                    if(TestUtil.isTest() && TextUtils.isEmpty(e.getMessage()))
                        G.showToast(e.getMessage());
                }
                break;
            //sheep://small.kfzs.com/sheep?type=run_app&packageName=com.realicing.android.upgrade.sheep&className=com.realicing.android.upgrade.sheep.MainActivity
            case "run_app":
            case "run":
                String packageName = uri.getQueryParameter("packageName");
                String className = uri.getQueryParameter("className");
                boolean packageNameIsEmpty = TextUtils.isEmpty(packageName);
                boolean classNameIsEmpty = TextUtils.isEmpty(className);
                if(packageNameIsEmpty && classNameIsEmpty){
                    tryGoSplashAct(uri);
                } else if(packageNameIsEmpty){
                    runApp(getPackageName(), className);
                } else if(classNameIsEmpty) {
                    PackageUtil.startApp(this, packageName);
                } else {
                    runApp(packageName, className);
                }
                break;
        }
//                    LogUtil.println("ActMain", "checkScheme","url: " + url);
//                    // scheme部分
//                    String scheme = uri.getScheme();
//                    LogUtil.println("ActMain", "checkScheme","scheme: " + scheme);
//                    // host部分
//                    String host = uri.getHost();
//                    LogUtil.println("ActMain", "checkScheme","host: " + host);
//                    //port部分
//                    int port = uri.getPort();
//                    LogUtil.println("ActMain", "checkScheme","port: " + port);
//                    // 访问路劲
//                    String path = uri.getPath();
//                    LogUtil.println("ActMain", "checkScheme","path: " + path);
//                    List<String> pathSegments = uri.getPathSegments();
//                    LogUtil.println("ActMain", "checkScheme","pathSegments:", JSON.toJSONString(pathSegments));
//                    // Query部分
//                    String query = uri.getQuery();
//                    LogUtil.println("ActMain", "checkScheme","query: " + query);
//                    //获取指定参数值
//                    String code = uri.getQueryParameter("code");
//                    LogUtil.println("ActMain", "checkScheme","code: " + code);
//                    if(TextUtils.equals(code, "0")){
//                        finish();
//                    }
    }
    private void runApp(String packageName, String className){
        try {
            Intent intent = new Intent();
            intent.setClassName(packageName, className);
            startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
            if(TestUtil.isTest() && TextUtils.isEmpty(e.getMessage()))
                G.showToast(e.getMessage());
        }
    }

    /**
     * 进入主页，可以设置 game_id 用于展示会长推广弹出框
     * sheep://small.kfzs.com/xmy?game_id=1385&code=976873
     * ActMain checkScheme scheme: sheep
     * ActMain checkScheme host: small.kfzs.com
     * ActMain checkScheme path: /xmy
     * ActMain checkScheme pathSegments: ["xmy"]
     * ActMain checkScheme query: code=976873
     * ActMain checkScheme code: 976873
     */
    private void tryGoSplashAct(Uri uri) {
        String game_id = uri.getQueryParameter("game_id");
        if(!TextUtils.isEmpty(game_id))//有game_id时设置game_id的回调到主页，用于展示会长推广弹出框
            ActionUtil.getInstance().addNextAction(SHOW_DIALOG_GAME_OR_GIFT_TASK, game_id);
        Jump2View.getInstance().newTaskTopStartAct(this, SplashAct.class, null);

    }

    //展示会长推广弹出框的key
    public static final String SHOW_DIALOG_GAME_OR_GIFT_TASK = "show_dialog_game_or_gift_task";
}
