package com.sheep.gamegroup.view.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.kfzs.duanduan.utils.NumberFormatUtils;
import com.sheep.gamegroup.model.entity.JPushMessage;
import com.sheep.gamegroup.util.ActionUtil;
import com.sheep.gamegroup.util.IDConstant;
import com.sheep.gamegroup.util.Jump2View;
import com.sheep.gamegroup.util.MainTab;
import com.sheep.jiuyan.samllsheep.BuildConfig;
import com.sheep.jiuyan.samllsheep.service.DownloadService;
import com.sheep.jiuyan.samllsheep.utils.G;

import java.util.Locale;

import cn.jpush.android.api.JPushInterface;
import rx.functions.Action1;

import static com.sheep.gamegroup.view.activity.CommitWxCodeAct.ALTER_CODE;
import static com.sheep.gamegroup.view.activity.CommitWxCodeAct.ALTER_INFO;
import static com.sheep.gamegroup.view.activity.CommitWxCodeAct.HAS_DEBLOCKED;

/**
 * Created by realicing on 2018/5/10.
 * realicing@sina.com
 */
public class MiddleAct extends Activity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            doAction();
        } catch (Exception e) {
            e.printStackTrace();
        }
        this.finish();
    }
    private void doAction(){
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            /*
               key = app,value = com.sheep.jiuyan.samllsheep
               key = cn.jpush.android.ALERT,value = 带有参数的通知
               key = cn.jpush.android.EXTRA,value = {"id":"976873","name":"zjb"}
               key = cn.jpush.android.NOTIFICATION_ID,value = 185860616
               key = cn.jpush.android.ALERT_TYPE,value = 7
               key = cn.jpush.android.NOTIFICATION_CONTENT_TITLE,value = 小绵羊
               key = cn.jpush.android.MSG_ID,value = 2150932953
               key = sdktype,value = JPUSH
             */
            for (String key : bundle.keySet()) {
                if (BuildConfig.DEBUG)
                    System.out.println(String.format(Locale.CHINA, "key = %s,value = %s", key, bundle.get(key)));
                switch (key) {
                    case JPushInterface.EXTRA_NOTIFICATION_ID:
//                    sb.append("\nkey:" + key + ", value:" + bundle.getInt(key));
                        break;
                    case JPushInterface.EXTRA_CONNECTION_CHANGE:
//                    sb.append("\nkey:" + key + ", value:" + bundle.getBoolean(key));
                        break;
                    case JPushInterface.EXTRA_EXTRA:
                        String data;
                        if (TextUtils.isEmpty((data = bundle.getString(JPushInterface.EXTRA_EXTRA)))) {
                            continue;
                        }
                        JPushMessage msg = JSONObject.parseObject(data, JPushMessage.class);
                        Activity activity = this;
                        gotoNext(activity, bundle, msg);
                        break;
                    default:
//                    sb.append("\nkey:" + key + ", value:" + bundle.getString(key));
                        break;
                }
            }
        }
    }

    //跳转发现中心
    public static final int GO_FIND_CENTER = 22;
    private void gotoNext(Activity activity, Bundle bundle, JPushMessage msg) {
        switch (msg.getType()) {
            case 11://试玩赚钱
                Jump2View.getInstance().goTryplayView(activity, null);
                break;
            case 12://任务详情: 任务的id
                Jump2View.getInstance().goTaskDetailView(activity, msg.getId());
                break;
            case 13://任务记录
                Jump2View.getInstance().goTaskList2View(activity, null);
                break;
            case IDConstant.JUMP_ASK_FRIEND_2://邀请赚钱
                Jump2View.getInstance().goAskGetMoney(activity, null);
                break;
            case 31://办卡赚钱
                Jump2View.getInstance().goCreditCardTaskList(activity, null);
                break;
            case 32://跳转到信用卡详情界面: id 信用卡任务的id
                Jump2View.getInstance().goCreditCardTaskDetail(activity, msg.getId());
                break;
            case 33://跳转到信用卡web申请界面; id 信用卡信息的id
                Jump2View.getInstance().tryGoCreditCardWeb(activity, 0, msg.getId(), new Action1<String>() {
                    @Override
                    public void call(String msg) {
                        if (msg != null) {
                            G.showToast(msg);
                        }
                    }
                });
                break;
            case 41://躺着赚钱
                Jump2View.getInstance().tryGoLyingView(activity, null);
                break;
            case 42://跳转到修改 挂机微信账号密码 到服务器 界面
                Jump2View.getInstance().goCommitWxCodeOnHookView(activity, msg.getId(), msg.getMsg(), ALTER_INFO);
                break;
            case 43://跳转到提交 挂机微信验证码 到服务器 界面
                Jump2View.getInstance().goCommitWxCodeOnHookView(activity, msg.getId(), msg.getMsg(), ALTER_CODE);
                break;
            case 44://跳转到提交 挂机微信账号已经解封 到服务器 界面
                Jump2View.getInstance().goCommitWxCodeOnHookView(activity, msg.getId(), msg.getMsg(), HAS_DEBLOCKED);
                break;
            case 51://微信辅助任务
                Jump2View.getInstance().tryGoWxfz(activity, null);
                break;
            case 61://个人中心
                Jump2View.getInstance().goPersonnalCenterView(activity, null);
                break;
            case 62://提现
                Jump2View.getInstance().tryGoWithdrawal(activity, null);
                break;
            case 621://提现结果页面
                Jump2View.getInstance().goWithdrawalResultview(activity, NumberFormatUtils.parseFloat(msg.getMsg()));
                break;
            case 63://充值Q币
                Jump2View.getInstance().goRechargeQAct(activity, "推送");
                break;
            case 64://充值绵羊币
                Jump2View.getInstance().goRechargeAct(activity, "推送");
                break;
            case 65://我的资产
                Jump2View.getInstance().goMyMoney(activity, null);
                break;
//                        case 66://安全中心
//                            Jump2View.getInstance().goAccountAndSecurit(activity,null);
//                            break;
            case 71://浏览器打开web界面: url h5链接
                Jump2View.getInstance().goWeb(activity, msg.getUrl());
                break;
            case 72://App web界面: url h5链接  title 标题
                Jump2View.getInstance().goWeb(activity, msg.getUrl(), TextUtils.isEmpty(msg.getTitle()) ? bundle.getString(JPushInterface.EXTRA_ALERT) : msg.getTitle());
                break;

            case 1://跳转到首页: id 0，小绵羊 1，游戏
                if (msg.getId() == 0)
                    Jump2View.getInstance().checkCommendApp(activity, msg.getId() % MainTab.values().length);
                else if (msg.getId() == 1)
                    Jump2View.getInstance().goMainGame(activity);
                break;
            case 2://公告: title 标题   msg 内容
                Jump2View.getInstance().goNotice(activity, TextUtils.isEmpty(msg.getTitle()) ? bundle.getString(JPushInterface.EXTRA_ALERT) : msg.getTitle(), msg.getMsg(), msg.getTime() == 0 ? System.currentTimeMillis() : msg.getTime());
                break;
            case 3://下载: url 下载链接
                if (msg.getUrl().startsWith("http"))
                    DownloadService.webDownload(activity, msg.getUrl());
                break;
            case 9://打卡赚钱
                Jump2View.getInstance().goSignCardAct(activity, null);
                break;
            case GO_FIND_CENTER://跳转发现中心
                ActionUtil.getInstance().addNextAction(ActMain.class.getSimpleName(), MainTab.Fgt_Find);
                Jump2View.getInstance().newTaskTopStartAct(activity, SplashAct.class, bundle);
                break;
            case IDConstant.JUMP_GMAE://游戏详情
                Jump2View.getInstance().goPlayGameDetail(activity, msg.getId());
                break;
            case IDConstant.JUMP_FIND_RECOMMEND://发现--推荐
            case IDConstant.JUMP_FIND_ACT://发现--活动
            case IDConstant.JUMP_FIND_INFORMATION://webView资讯
            case IDConstant.JUMP_FIND_GMAE://发现中的游戏详情
                Jump2View.getInstance().goFindItem(activity, msg.getId());
                break;
            case IDConstant.JUMP_WATCH_FOCUS://看点
                Jump2View.getInstance().goWatchFocus(activity, msg.getId());
                break;
            case 0://直接启动或者重启app
            default:
                Jump2View.getInstance().newTaskTopStartAct(activity, SplashAct.class, bundle);
                break;
        }
    }
}
