package com.sheep.gamegroup.util;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;

import com.alibaba.fastjson.JSON;
import com.kfzs.duanduan.ActMain;
import com.kfzs.duanduan.ActPicturesEnlarge;
import com.kfzs.duanduan.bean.KFIntentKeys;
import com.sheep.gamegroup.absBase.BaseActivity;
import com.sheep.gamegroup.model.entity.Agreement;
import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.entity.CreditCard;
import com.sheep.gamegroup.model.entity.DialogEntity;
import com.sheep.gamegroup.model.entity.NewbieTask;
import com.sheep.gamegroup.model.entity.NewbieTaskRecord;
import com.sheep.gamegroup.model.entity.TaskAcceptedEty;
import com.sheep.gamegroup.model.entity.TaskEty;
import com.sheep.gamegroup.model.entity.UserEntity;
import com.sheep.gamegroup.model.entity.WebviewEntity;
import com.sheep.gamegroup.model.util.SheepSubscriber;
import com.sheep.gamegroup.view.activity.AccountAndSecurityAct;
import com.sheep.gamegroup.view.activity.ActCreditCardTaskList;
import com.sheep.gamegroup.view.activity.ActCreditCardWeb;
import com.sheep.gamegroup.view.activity.ActGuideDeblocked;
import com.sheep.gamegroup.view.activity.ActGuideOnHook;
import com.sheep.gamegroup.view.activity.ActMainGame;
import com.sheep.gamegroup.view.activity.ActMyMoney;
import com.sheep.gamegroup.view.activity.ActNewbieTaskList;
import com.sheep.gamegroup.view.activity.ActNotice;
import com.sheep.gamegroup.view.activity.ActUnderstandSheep;
import com.sheep.gamegroup.view.activity.ActWeb;
import com.sheep.gamegroup.view.activity.AskGetMoneyAct;
import com.sheep.gamegroup.view.activity.BindDataAct;
import com.sheep.gamegroup.view.activity.BindOrChangeWeixinAct;
import com.sheep.gamegroup.view.activity.ChangeTelAct;
import com.sheep.gamegroup.view.activity.ChangeWxOrTelAct;
import com.sheep.gamegroup.view.activity.CommitWxAct;
import com.sheep.gamegroup.view.activity.CommitWxCodeAct;
import com.sheep.gamegroup.view.activity.DialogActivity;
import com.sheep.gamegroup.view.activity.DialogToastAct;
import com.sheep.gamegroup.view.activity.FeedbackAct;
import com.sheep.gamegroup.view.activity.LieMakeMoneyAct;
import com.sheep.gamegroup.view.activity.LoginAct;
import com.sheep.gamegroup.view.activity.PersionInfoAct;
import com.sheep.gamegroup.view.activity.PersonalCenterAct;
import com.sheep.gamegroup.view.activity.PhoneAct;
import com.sheep.gamegroup.view.activity.RealNameAuthenAct;
import com.sheep.gamegroup.view.activity.RechargeAct;
import com.sheep.gamegroup.view.activity.RechargeQAct;
import com.sheep.gamegroup.view.activity.RechargeResultAct;
import com.sheep.gamegroup.view.activity.TaskDetailAct;
import com.sheep.gamegroup.view.activity.TaskDetailAddQQAct;
import com.sheep.gamegroup.view.activity.TaskDetailCreditCardAct;
import com.sheep.gamegroup.view.activity.TaskListAct;
import com.sheep.gamegroup.view.activity.TryMakeMoneyact;
import com.sheep.gamegroup.view.activity.WebviewAct;
import com.sheep.gamegroup.view.activity.WithdrawalAct;
import com.sheep.gamegroup.view.activity.WithdrawalListAct;
import com.sheep.gamegroup.view.activity.WithdrawalResultAct;
import com.sheep.gamegroup.view.dialog.DialogAgreement;
import com.sheep.gamegroup.view.dialog.DialogNewbieTask1;
import com.sheep.gamegroup.view.dialog.DialogNewbieTaskRegistSuccess;
import com.sheep.jiuyan.samllsheep.BuildConfig;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.jiuyan.samllsheep.service.FloatService;
import com.sheep.jiuyan.samllsheep.service.MonitorAppService;
import com.sheep.jiuyan.samllsheep.utils.G;
import com.sheep.jiuyan.samllsheep.utils.OpenService;
import com.sheep.jiuyan.samllsheep.utils.SpUtils;

import org.xutils.ex.DbException;

import java.util.ArrayList;
import java.util.List;

import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * Created by ljy on 2018/3/18.
 */

public class Jump2View {

    private static Jump2View jump2View;

    public static Jump2View getInstance(){
        if (jump2View == null){
            jump2View = new Jump2View();
        }
        return jump2View;
    }

    /**
     * 跳转到提现页面
     * @param context
     * @param o
     */
    public void goWithdrawal(Context context, Object o){
        UserEntity userEntity = null;
        try {
            userEntity = MyDbManager.getInstance().dbFindUser();
        } catch (DbException e) {
            e.printStackTrace();
        }
        if(userEntity == null){
            G.showToast("数据正在加载中，请稍候重试");
            return;
        }
        if(!isBindWeixin(context)){
            goBindOrChangeWeixinView(context, false);
            return;
        }
        if(TextUtils.isEmpty(userEntity.getReal_name()) || TextUtils.isEmpty(userEntity.getId_number())){//没有实名认证
            goRealNameAuther(context, userEntity);
            return;
        }
        Intent intent = new Intent(context, WithdrawalAct.class);
        context.startActivity(intent);
        UMConfigUtils.onEvent(UMConfigUtils.Event.SHEEP_TIXIAN);
    }


    /**
     * 跳到安全中心
     * @param context
     * @param o
     */
    public void goAccountAndSecurit(Context context, Object o){
        UMConfigUtils.onEvent(UMConfigUtils.Event.USER_ACCOUNT_SAFE);
        Intent intent = new Intent(context, AccountAndSecurityAct.class);
        context.startActivity(intent);
    }

    /**
     * 跳到登录页面
     * @param context
     * @param o
     */
    public synchronized void goLoginView(Context context, Object o){
        Intent intent = new Intent(context, LoginAct.class);
        if(o != null)
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }


    /**
     * 跳到邀请赚钱页面
     * @param context
     * @param o
     */
    public void goAskGetMoney(Context context, Object o){
        Intent intent = new Intent(context, AskGetMoneyAct.class);
        context.startActivity(intent);
        UMConfigUtils.onEvent(UMConfigUtils.Event.SHEEP_TASK_INVITE);
    }



    /**
     * 跳到首页面
     * @param context
     * @param o
     */
    public void goHomePageView(Context context, Object o){
        Intent intent = new Intent(context, ActMain.class);
        if(o instanceof Integer){
            intent.putExtra(ActMain.INTEN_GAME_ID, (Integer) o);
        }
        context.startActivity(intent);
    }

    /**
     * 跳到手机登录页面
     * @param context
     * @param o:  0:登录，1：旧手机号，2：新手机号
     */
    public void goPhoneLoginView(Context context, int o){
        Intent intent = new Intent(context, PhoneAct.class);
        intent.putExtra("show_notice",  o);
        context.startActivity(intent);
    }

    /**
     * 跳到个人中心页面
     * @param context
     * @param o
     */
    public void goPersonnalCenterView(Context context, Object o){
        Intent intent = new Intent(context, PersonalCenterAct.class);
        context.startActivity(intent);
    }


    /**
     * 跳到提现结果页面
     * @param context
     * @param o
     */
    public void goWithdrawalResultview(Context context, String o){
        Intent intent = new Intent(context, WithdrawalResultAct.class);
        intent.putExtra(" amount", o);
        context.startActivity(intent);
    }

    /**
     * 跳到绑定微信页面
     * @param context
     * @param o false:绑定   true：切换
     */
    public void goBindOrChangeWeixinView(Context context, boolean o){
        if(!isBindWeixin(context) && !o){
            goNoBindWx(context, o);
            return;
        }
        Intent intent = new Intent(context, BindOrChangeWeixinAct.class);
        intent.putExtra("type",o);
        context.startActivity(intent);
    }

    /**
     * 跳到试玩界面
     * @param context
     * @param o
     */
    public void goTryplayView(Context context, Object o){
        Intent intent = new Intent(context, TryMakeMoneyact.class);
        try{
            intent.putExtra("is_succession", o == null? 0: Integer.valueOf(o+ ""));
        }catch (Exception e){
            e.printStackTrace();
        }
        context.startActivity(intent);
        UMConfigUtils.onEvent(UMConfigUtils.Event.SHEEP_TASK_TRY);
    }

    /**
     * 跳到分享界面
     * @param context
     * @param o
     */
    public void goInvitationView(Context context, Object o){
        Intent intent = new Intent(context, BindOrChangeWeixinAct.class);
        context.startActivity(intent);
    }

    /**
     * 跳到躺着界面
     * @param context
     * @param o
     */
    public void goLyingView(Context context, Object o){
        UMConfigUtils.onEvent(UMConfigUtils.Event.SHEEP_TASK_LIE);
        Intent intent = new Intent(context, LieMakeMoneyAct.class);
        context.startActivity(intent);
    }
    /**
     * 先判断服务器是否打开躺着赚钱功能，再跳到躺着界面
     * @param context
     * @param o
     */
    public void tryGoLyingView(final Context context, final Object o){
        if(context instanceof BaseActivity){
            ((BaseActivity) context).showProgress();
        }
        SheepApp.getInstance().getNetComponent().getApiService().getWebchatIsOpen()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SheepSubscriber<BaseMessage>(context) {
                    @Override
                    public void onNext(BaseMessage baseMessage) {
                        if(context instanceof BaseActivity){
                            ((BaseActivity) context).hideProgress();
                        }
                        if (BuildConfig.DEBUG)
                            System.out.println("baseMessage onNext " + JSON.toJSONString(baseMessage));
                        Boolean wechatIsOpen;
                        if (baseMessage.getData() instanceof Boolean)
                            wechatIsOpen = (Boolean) baseMessage.getData();
                        else
                            wechatIsOpen = false;
                        if(wechatIsOpen){
                            goLoginView(context, o);
                        } else {
                            G.showToast(R.string.coming_soon);
                        }
                    }

                    @Override
                    public void onError(BaseMessage baseMessage) {
                        if(context instanceof BaseActivity){
                            ((BaseActivity) context).hideProgress();
                        }
                        if (BuildConfig.DEBUG)
                            System.out.println("baseMessage onError " + JSON.toJSONString(baseMessage));
                        G.showToast(R.string.coming_soon);
                    }
                });
    }
    /**
     * 先获取服务器配置的微信辅助功能的地址，再跳到微信辅助功能界面
     * @param context
     * @param o
     */
    public void tryGoWxfz(final Context context, final Object o){
        if(context instanceof BaseActivity){
            ((BaseActivity) context).showProgress();
        }
        SheepApp.getInstance().getNetComponent().getApiService().wxfzGetUrl()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SheepSubscriber<BaseMessage>(context) {
                    @Override
                    public void onNext(BaseMessage baseMessage) {
                        if(context instanceof BaseActivity){
                            ((BaseActivity) context).hideProgress();
                        }
                        if(BuildConfig.DEBUG)
                            System.out.println("baseMessage onNext "+JSON.toJSONString(baseMessage));
                        Jump2View.getInstance().goWeb(context, baseMessage.getData().toString(), "微信二维码辅助好友注册任务");
                    }

                    @Override
                    public void onError(BaseMessage baseMessage) {
                        if(context instanceof BaseActivity){
                            ((BaseActivity) context).hideProgress();
                        }
                        if(BuildConfig.DEBUG)
                            System.out.println("baseMessage onError "+JSON.toJSONString(baseMessage));
                        G.showToast(TextUtils.isEmpty(baseMessage.getMsg()) ? "服务器错误，请稍候再试" : baseMessage.getMsg());
                    }
                });
    }
    /**
     * 跳到提交挂机微信验证码界面
     * @param context
     * @param id
     * @param account
     * @param type
     */
    public void goCommitWxCodeOnHookView(Context context, int id, String account, int type){
        Intent intent = new Intent(context, CommitWxCodeAct.class);
        intent.putExtra("type", type);
        intent.putExtra("id", id);
        intent.putExtra("account", account);
        context.startActivity(intent);
    }
    /**
     * 跳到引导用户  添加  微信小号H5界面
     * @param context
     * @param o
     */
    public void goGuideOnHook(Context context, Object o){
        Intent intent = new Intent(context, ActGuideOnHook.class);
//        extraUrl = getIntent().getStringExtra(KFIntentKeys.EXTRA_WEBVIEW_URL);
//        extraTitle = getIntent().getStringExtra(KFIntentKeys.EXTRA_WEBVIEW_TITLE);
        intent.putExtra(KFIntentKeys.EXTRA_WEBVIEW_URL, "https://qiniu.kuaifazs.com/weixinxiaohaoliuc.html");
        intent.putExtra(KFIntentKeys.EXTRA_WEBVIEW_TITLE, "挂机引导");
        context.startActivity(intent);
    }
    /**
     * 跳到引导用户   解封   微信小号H5界面
     * @param context
     * @param o
     */
    public void goGuideDeArchive(Context context, Object o){
        Intent intent = new Intent(context, ActGuideDeblocked.class);
//        extraUrl = getIntent().getStringExtra(KFIntentKeys.EXTRA_WEBVIEW_URL);
//        extraTitle = getIntent().getStringExtra(KFIntentKeys.EXTRA_WEBVIEW_TITLE);
        intent.putExtra(KFIntentKeys.EXTRA_WEBVIEW_URL, "https://qiniu.kuaifazs.com/jiefeng.html");
        intent.putExtra(KFIntentKeys.EXTRA_WEBVIEW_TITLE, "解封引导");
        context.startActivity(intent);
    }
    /**
     * 跳到提交微信号挂机赚钱界面
     * @param context
     * @param o
     */
    public void goCommitWxView(Context context, Object o){
        Intent intent = new Intent(context, CommitWxAct.class);
        context.startActivity(intent);
    }

    /**
     * 跳到绑定数据页面
     * @param context
     * @param o
     */
    public void goBindDataView(Context context, Object o) {
        Intent intent = new Intent(context, BindDataAct.class);
        intent.putExtra("stype", Integer.valueOf(o.toString()));
        ((Activity)context).startActivityForResult(intent, 0);
    }

    /**
     * 跳出弹框页面
     * @param context
     * @param curTask
     * @param runTask
     */
    public synchronized void goDialogActivityView(Context context, TaskEty curTask, TaskAcceptedEty runTask){
        Intent intent = new Intent(context, DialogActivity.class);
        intent.putExtra("task_entity", curTask);
        intent.putExtra("task_accepted_entity", runTask);
        context.startActivity(intent);
    }

    /**
     * 跳转到信用卡任务详情界面
     * @param context
     * @param id
     */
    public synchronized void goCreditCardTaskDetail(Context context, int id){
        Intent intent = new Intent(context, TaskDetailCreditCardAct.class);
        intent.putExtra("id", id);
        context.startActivity(intent);
        UMConfigUtils.onEvent(UMConfigUtils.Event.SHEEP_CREDIT_CARD_DETAIL);
    }
    /**
     * 跳转到新手加QQ群任务详情界面
     * @param context
     * @param task
     */
    public synchronized void goAddQQTaskDetail(Context context, NewbieTask task){
        Intent intent = new Intent(context, TaskDetailAddQQAct.class);
        intent.putExtra("task_entity", task);
        context.startActivity(intent);
        UMConfigUtils.onEvent(UMConfigUtils.Event.SHEEP_NEWBIE_TASK_ADD_QQ);
    }
    public synchronized void goTaskDetailView(Context context, Object o){
        Intent intent = new Intent(context, TaskDetailAct.class);
        if(o instanceof Integer){
            intent.putExtra("task_id", (Integer) o);
        } else if(o instanceof TaskAcceptedEty){
            intent.putExtra("task_entity", (TaskAcceptedEty)o);
        }

        context.startActivity(intent);
    }

    /**
     * 跳到已做过任务列表页面
     * @param context
     * @param o
     */
    public void goTaskList2View(Context context, TaskEty o){
        UMConfigUtils.onEvent(UMConfigUtils.Event.USER_TASK_RECORD);
        Intent intent = new Intent(context, TaskListAct.class);
        intent.putExtra("task_entity", o);
        context.startActivity(intent);}

    /**
     * 跳到已提现的列表
     * @param context
     * @param o
     */
    public void goWithdrawallistView(Context context, TaskEty o){
        Intent intent = new Intent(context, WithdrawalListAct.class);
        intent.putExtra("task_entity", o);
        context.startActivity(intent);
    }


    /**
     * 是否绑定微信号
     */
    public boolean isBindWeixin(Context context){
        try{
//            UserEntity userEntity = MyDbManager.getInstance().dbFindUser();
//            if(userEntity != null){
//                if(!TextUtils.isEmpty(userEntity.getWx_openid())){
//                    return true;
//                }
//            }
            String weix = SpUtils.getWeixin(context);
            if(!TextUtils.isEmpty(weix)){
                return true;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }


    /**
     * 跳转到个人中心界面
     * @param context
     * @param o
     */
    public void goPersionInfo(Context context, UserEntity o){
        Intent i = new Intent(context, PersionInfoAct.class);
        i.putExtra("info", o);
        context.startActivity(i);
    }


    /**
     * 跳转到验证界面
     * @param context
     * @param o
     */
    public void goRealNameAuther(Context context, UserEntity o){
        Intent i = new Intent(context, RealNameAuthenAct.class);
        i.putExtra("info", o);
        context.startActivity(i);
    }
    /**
     * 跳转到未绑定微信界面
     * @param context
     * @param o
     */
    public void goNoBindWx(Context context, Object o){
        Intent i = new Intent(context, ChangeWxOrTelAct.class);
        context.startActivity(i);
    }
    /**
     * 跳转到绑定手机页面
     * @param context
     * @param o
     */
    public void goBindPhone(Context context, Object o){
        Intent i = new Intent(context, ChangeTelAct.class);
        context.startActivity(i);
    }
    /**
     * 跳转到非wifi网络提示
     * @param context
     * @param o
     */
    public void goNoticeAct(Context context, DialogEntity o){
        Intent i = new Intent(context, DialogToastAct.class);
        i.putExtra("dialog_entity", o);
        if(context instanceof Activity){
            ((Activity)context).startActivityForResult(i,0);
        }else {
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i);
        }

    }

    /**
     * 跳转到webview
     */
    public void goWebview(Context context, WebviewEntity webviewEntity){
        Intent intent = new Intent(context, WebviewAct.class);
        intent.putExtra("webview_entity", webviewEntity);
        context.startActivity(intent);
    }
    /**
     * 跳转到 一分钟快速了解小绵羊 界面
     */
    public void goUnderstandSheep(Context context, NewbieTaskRecord newbieTaskRecord){
        UMConfigUtils.onEvent(UMConfigUtils.Event.SHEEP_NEWBIE_TASK_UNDERSTAND_SHEEP);
        Intent intent = new Intent(context, ActUnderstandSheep.class);
        if(newbieTaskRecord != null)
            intent.putExtra(NewbieTaskRecord.class.getSimpleName(), newbieTaskRecord);
        context.startActivity(intent);
    }

    /**
     * 跳转到 一分钟快速了解小绵羊 界面
     */
    public void goNewbieTaskList(Context context, Object o){
        UMConfigUtils.onEvent(UMConfigUtils.Event.SHEEP_NEWBIE_TASK_LIST);
        Intent intent = new Intent(context, ActNewbieTaskList.class);
        context.startActivity(intent);
    }

    /**
     * 跳到信用卡任务界面界面
     * @param context
     * @param o
     */
    public void goCreditCardTaskList(Context context, Object o){
        Intent intent = new Intent(context, ActCreditCardTaskList.class);
        context.startActivity(intent);
        UMConfigUtils.onEvent(UMConfigUtils.Event.SHEEP_CREDIT_CARD);
    }

    /**
     * 跳到银行web页面
     * @param context
     * @param creditCard
     */
    public void goCreditCardWeb(Context context, CreditCard creditCard){
        Intent intent = new Intent(context, ActCreditCardWeb.class);
        intent.putExtra(CreditCard.class.getSimpleName(), creditCard);
        context.startActivity(intent);
        UMConfigUtils.onEvent(UMConfigUtils.Event.SHEEP_CREDIT_CARD_DETAIL_WEB);
    }
    /**
     * 尝试跳到银行web页面
     * @param context
     * @param id
     */
    public void tryGoCreditCardWeb(final Context context, int id, final Action1<String> action1){
        SheepApp.getInstance().getNetComponent().getApiService().getCredit(id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SheepSubscriber<BaseMessage>(context) {
                    @Override
                    public void onNext(BaseMessage baseMessage) {
                        final CreditCard creditCard = baseMessage.getData(CreditCard.class);
                        Jump2View.getInstance().goCreditCardWeb(context, creditCard);
                        if(BuildConfig.DEBUG)
                            System.out.println("baseMessage onNext "+JSON.toJSONString(baseMessage));
                        if(action1 != null)
                            action1.call(null);
                    }

                    @Override
                    public void onError(BaseMessage baseMessage) {
                        if(BuildConfig.DEBUG)
                            System.out.println("baseMessage onError "+JSON.toJSONString(baseMessage));
                        if(action1 != null)
                            action1.call(TextUtils.isEmpty(baseMessage.getMsg()) ? "服务器错误，请稍候再试" : baseMessage.getMsg());
                    }
                });
    }
    /**
     * 浏览器打开web页面
     * @param context
     * @param url
     */
    public void goWeb(Context context, String url){
        Intent intent = new Intent();
        intent.setAction("android.intent.action.VIEW");
        Uri content_url = Uri.parse(url);
        intent.setData(content_url);
        context.startActivity(intent);
    }
    /**
     * 跳到web页面
     * @param context
     * @param url
     * @param title
     */
    public void goWeb(Context context, String url, String title){
        Intent intent = new Intent(context, ActWeb.class);
        intent.putExtra("url", url);
        intent.putExtra("title", title);
        context.startActivity(intent);
    }
    /**
     * 跳到我的资产界面
     * @param context
     * @param o
     */
    public void goMyMoney(Context context, Object o){
        Intent intent = new Intent(context, ActMyMoney.class);
        context.startActivity(intent);
    }











    /**
     * 跳到充值结果页面
     * @param context
     * @param resultStatus 支付宝支付结果码
     */
    public void goRechargeResultAct(Context context, String resultStatus){
        Intent intent = new Intent(context, RechargeResultAct.class);
        intent.putExtra("payCode",resultStatus);
        context.startActivity(intent);
    }

    /**
     * 跳到充值页面
     * @param context
     * @param o
     */
    public void goRechargeAct(Context context, Object o){
        Intent intent = new Intent(context, RechargeAct.class);
        context.startActivity(intent);
    }

    /**
     * 跳到绵羊币充值充值页面
     * @param context
     * @param o
     */
    public void goRechargeQAct(Context context, Object o){
        if(!SheepApp.getInstance().isShowQB()){
            goRechargeAct(context, o);
        }else {
            Intent intent = new Intent(context, RechargeQAct.class);
            context.startActivity(intent);
        }

    }

    /**
     * 添加flags Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP启动一个Activity
     * @param context
     * @param actClass
     * @param bundle
     */
    public void newTaskTopStartAct(Context context, Class<? extends Activity> actClass, Bundle bundle) {
        Intent i = new Intent(context, actClass);
        if(bundle != null)
            i.putExtras(bundle);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );
        context.startActivity(i);
    }

    /**
     * 展示公告
     * @param context
     * @param title
     * @param content
     */
    public void goNotice(Context context, String title, String content, long time) {
        Intent intent = new Intent(context, ActNotice.class);
        intent.putExtra("title", title);
        intent.putExtra("content", content);
        intent.putExtra("time", time);
        context.startActivity(intent);
    }

    /**
     * 意见反馈
     * @param context
     */
    public void goFeedbackAct(Context context) {
        Intent intent = new Intent(context, FeedbackAct.class);
        context.startActivity(intent);
    }

    /**
     * 游戏列表界面
     * @param context
     */
    public void goMainGame(Context context) {
        Intent intent = new Intent(context, ActMainGame.class);
        context.startActivity(intent);
    }

    /**
     * 显示多张图片
     * @param context
     * @param position
     * @param imgs 多张图片地址，用;分隔
     */
    public void showImgs(Context context, int position, String imgs) {
        Intent picEnlargeIntent = new Intent(context, ActPicturesEnlarge.class);
        picEnlargeIntent.putExtra(KFIntentKeys.EXTRA_CURRENT_PICTURE_INDEX, (position));
        picEnlargeIntent.putExtra("imgs", imgs);
        context.startActivity(picEnlargeIntent);
    }




    /**
     * 启动小绵羊悬浮窗
     * @param context
     */
    public void startFloat(Context context, boolean isShow) {
        Intent intent = new Intent(context, FloatService.class);
        intent.putExtra("isShow", isShow);
        context.startService(intent);
    }
    private AlertDialog tipToOpernAccessibilityDialog;
    public void clearAlertDialog(){
        tipToOpernAccessibilityDialog = null;
    }
    /**
     * 启动小绵羊辅助
     * @param context
     */
    public boolean startAccessibility(final Context context) {
        if(MonitorAppService.isAccessibilitySettingsOn(SheepApp.getInstance()) && new OpenService().isAccessibilitySettingsOn(context))
            return true;
        if(tipToOpernAccessibilityDialog != null){
            return false;
        }
        AlertDialog.Builder  builder = new AlertDialog.Builder(context)
                .setTitle("温馨提示！")
                .setMessage("亲！要赚钱需要手动开启小绵羊辅助功能哟！")
                .setPositiveButton("去开启", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        context.startActivity(intent);

//                        Intent intent2 = new Intent(context, MonitorAppService.class);
//                        context.startService(intent2);
                    }
                });
        builder.setCancelable(false);
        tipToOpernAccessibilityDialog = builder.create();
        if(tipToOpernAccessibilityDialog.getWindow() != null) {
            tipToOpernAccessibilityDialog.getWindow().setType(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N ? WindowManager.LayoutParams.TYPE_PHONE : WindowManager.LayoutParams.TYPE_TOAST);
            tipToOpernAccessibilityDialog.show();
        }
        return false;
    }

    /**
     * 获取协议列表，如果没有协议，则直接通过，否则显示协议对话框
     * @param activity
     * @param action1
     */
    public void tryShowAgreement(final Activity activity, final Action1<String> action1) {
        SheepApp.getInstance().getNetComponent().getApiService().getAgreement()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SheepSubscriber<BaseMessage>(activity) {
                    @Override
                    public void onNext(BaseMessage baseMessage) {
                        LogUtil.println("baseMessage onNext "+ JSON.toJSONString(baseMessage));
                        List<Agreement> agreementList = baseMessage.getDatas(Agreement.class);
                        if(agreementList.isEmpty()){
                            if(action1 != null)
                                action1.call(null);
                        } else {
                            DialogAgreement.showDialog(activity, agreementList, action1);
                        }
                    }

                    @Override
                    public void onError(BaseMessage baseMessage) {
                        LogUtil.println("baseMessage onError "+JSON.toJSONString(baseMessage));
                        if(action1 != null){
                            String msg = TextUtils.isEmpty(baseMessage.getMsg()) ? "服务器错误，请稍候再试" : baseMessage.getMsg();
                            msg = baseMessage.getCode() == 404 ? "404" : msg;
                            action1.call(msg);
                        }
                    }
                });
    }

    /**
     * 先尝试显示用户协议与劳务协议等协议对话框，再尝试显示新手任务对话框，如果协议没有通过，直接退出，无论原因
     * @param activity
     * @param userEntity
     * @param newbie_task
     */
    public void tryShowAgreementOrNewbieTask(final Activity activity, final UserEntity userEntity, final View newbie_task) {
        tryShowAgreement(activity, new Action1<String>() {
            @Override
            public void call(String msg) {
                if(msg == null || TextUtils.equals(msg, "404")) {
                    tryShowNewbieTask(activity, userEntity, newbie_task);
                }else {
                    G.showToast(msg);
                    SheepApp.getInstance().quit();
                }
            }
        });
    }

    public void tryShowNewbieTask(final Activity activity, final UserEntity userEntity, final View newbie_task) {
        if (userEntity.getIs_new() == 2) {//1新用户 2老用户
            newbie_task.setVisibility(View.GONE);
        } else {
            NewbieTaskRecord newbie_task_record = null;
            try {
                newbie_task_record = MyDbManager.getInstance().dbFindNewbieTaskRecord(userEntity.getInvitation_code());
            } catch (DbException e) {
                e.printStackTrace();
            }
            if (newbie_task_record == null) {
                newbie_task_record = new NewbieTaskRecord();
                newbie_task_record.setUser_id(userEntity.getInvitation_code());
                MyDbManager.getInstance().saveOrUpdate(newbie_task_record);
            }
            boolean dontShowNewbieTaskDialog = newbie_task_record.isDontShowNewbieTaskDialog();//默认可以显示对话框
            if (
//                !BuildConfig.DEBUG &&
                    dontShowNewbieTaskDialog) {
                newbie_task.setVisibility(View.VISIBLE);
                return;
            }
            final NewbieTaskRecord finalNewbie_task_record = newbie_task_record;
            SheepApp.getInstance().getNetComponent().getApiService().getNewbieTask()
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new SheepSubscriber<BaseMessage>(activity) {
                        @Override
                        public void onNext(BaseMessage baseMessage) {
                            newbie_task.setVisibility(View.VISIBLE);
                            List<NewbieTask> newbieTaskList = baseMessage.getDatas(NewbieTask.class);
                            if (newbieTaskList != null && !newbieTaskList.isEmpty() && newbieTaskList.size() > 1 && !newbieTaskList.get(1).getFinished()) {//了解小绵羊任务未完成时
                                DialogNewbieTaskRegistSuccess.showDialog(activity, finalNewbie_task_record, newbieTaskList.get(0));
                            } else if (newbieTaskList != null && !newbieTaskList.isEmpty()) {
                                finalNewbie_task_record.setDontShowNewbieTaskDialog(true);
                                MyDbManager.getInstance().saveOrUpdate(finalNewbie_task_record);//设置为不再显示新手对话框
                                List<NewbieTask> taskList = new ArrayList<>();
                                NewbieTask task = null;
                                for (NewbieTask newbieTask : newbieTaskList) {
                                    if (newbieTask.getFinished()) {//已经完成
                                        task = newbieTask;
                                    } else {
                                        taskList.add(newbieTask);
                                    }
                                }
                                DialogNewbieTask1.showDialog(activity, taskList, task);
                            } else {
                                if (BuildConfig.DEBUG)
                                    G.showToast("测试：服务器数据错误,新手任务数据为空");
                            }
                        }

                        @Override
                        public void onError(BaseMessage baseMessage) {
                            newbie_task.setVisibility(View.GONE);
                            System.out.println("baseMessage onError " + JSON.toJSONString(baseMessage));
                        }
                    });
        }
    }
}
