package com.ym.library.utils;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;


import com.ym.library.listener.IAdVideoListener;
import com.ym.library.module.NewsEntity;
import com.ym.library.module.ShaveListEntity;

import com.ym.library.router.RounterApi;
import com.ym.library.router.RounterBus;
import com.ym.library.rxbus.BusData;
import com.ym.library.rxbus.RxBusConstant;
import com.ym.library.rxbus.RxBusUtil;
import com.ym.modulecommon.AppliContext;
import com.ym.modulecommon.module.UserInfoEntity;

import org.jetbrains.annotations.Nullable;


/**
 * Created by gaoleichao on 2019/3/12
 */
public class JumpUtils {


    public static void activityJump(Intent intent) {
        try {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            AppliContext.get().startActivity(intent);
        } catch (Exception e) {
        }
    }

    public static void h5Jump(String title, String url,boolean showToolbar,Activity activity) {
        try {
            Intent intent = RounterBus.getRounter(RounterApi.class).getIntentActivityWeb(title, url,showToolbar);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//            intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
            if (activity!=null){
                activity.startActivity(intent);
            }
        } catch (Exception e) {
            Utils.showToast(AppliContext.get(), "该版本暂不支持，请更新版本！");
        }
    }

    public static void gatherJump(String CChannel, String pid, String albumId, String description, String area, String tvYear,
                                  String score, String contentType, String name, String image, boolean topVisibleFlag,
                                  String availableMediaNum, String playCnt) {
        try {
            Intent intent = RounterBus.getRounter(RounterApi.class).getIntentActivityGather(CChannel, pid, albumId,
                    description, area, tvYear, score, contentType, name, image, topVisibleFlag, availableMediaNum, playCnt);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            AppliContext.get().startActivity(intent);
        } catch (Exception e) {
            Utils.showToast(AppliContext.get(), "该版本暂不支持，请更新版本！");
        }
    }

    public static void starlistJump(String cchannel, String pid, String alumbid, String name) {
        try {
            Intent intent = RounterBus.getRounter(RounterApi.class).getIntentActivityStarList(cchannel, pid, alumbid, name);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            AppliContext.get().startActivity(intent);
        } catch (Exception e) {
            Utils.showToast(AppliContext.get(), "该版本暂不支持，请更新版本！");
        }
    }


    public static void baiduVideoJump(String url) {
        try {
            Intent intent = RounterBus.getRounter(RounterApi.class).getIntentBaiduVideo(url);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            AppliContext.get().startActivity(intent);
        } catch (Exception e) {
            Utils.showToast(AppliContext.get(), "该版本暂不支持，请更新版本！");
        }
    }

    public static void withdrawCash() {
        try {
            Intent intent = RounterBus.getRounter(RounterApi.class).getWithDrawCash();
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            AppliContext.get().startActivity(intent);
        } catch (Exception e) {
            Utils.showToast(AppliContext.get(), "该版本暂不支持，请更新版本！");
        }
    }


    public static void dialogPermissionSettings(Activity activity, int requestCode, String text1, String text2, boolean isNewUser) {
        try {
            Intent intent = RounterBus.getRounter(RounterApi.class).getDialogPermission();
            intent.putExtra("text1", text1);
            intent.putExtra("text2", text2);
            intent.putExtra("isNewUser", isNewUser);
//            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            activity.startActivityForResult(intent, requestCode);
        } catch (Exception e) {
            Utils.showToast(AppliContext.get(), "该版本暂不支持，请更新版本！");
        }
    }

    public static boolean isLogin(Boolean isJump) {
        String token =com.ym.modulecommon.utils.SettingPreference.getToken();
        Log.i("TAG", "isLogin: " + token);
        String uid = com.ym.modulecommon.utils.SettingPreference.getUid();
        UserInfoEntity entity = SettingPreference.getUserInfoData();
        if (TextUtils.isEmpty(token) || TextUtils.isEmpty(uid) || entity == null) {
            com.ym.modulecommon.utils.SettingPreference.setUid("");
            com.ym.modulecommon.utils.SettingPreference.setToken("");
            com.ym.modulecommon.utils.SettingPreference.saveUserInfo(null);
            return false;
        }
        return true;
    }

    //设置
    public static void jumpSettingActivity() {
        try {
            Intent intent = RounterBus.getRounter(RounterApi.class).getIntentSetting();
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            AppliContext.get().startActivity(intent);
        } catch (Exception e) {
        }
    }

    //提现记录
    public static void jumpWithdrawalsRecordActivity() {
        try {
            Intent intent = RounterBus.getRounter(RounterApi.class).getIntentSWithdrawalsRecord();
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            AppliContext.get().startActivity(intent);
        } catch (Exception e) {
        }
    }

    //钻石提现
    public static void jumpDiamonCrashActivity() {
        try {
            Intent intent = RounterBus.getRounter(RounterApi.class).getIntentDimoneCrash();
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            AppliContext.get().startActivity(intent);
        } catch (Exception e) {
        }
    }


    //r任务页
    public static void jumpTaskCrashActivity() {
        try {
            Intent intent = RounterBus.getRounter(RounterApi.class).getIntentTaskCrash();
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            AppliContext.get().startActivity(intent);
        } catch (Exception e) {
        }
    }

    //金币提现
    public static void jumpCoinCrashActivity() {
        try {
            Intent intent = RounterBus.getRounter(RounterApi.class).getIntentCoinCrash();
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            AppliContext.get().startActivity(intent);
        } catch (Exception e) {
        }
    }


    //幸运抽奖
    public static void jumpLuckDrawActivity() {
        try {
            Intent intent = RounterBus.getRounter(RounterApi.class).getIntentLuckyDraw();
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            AppliContext.get().startActivity(intent);
        } catch (Exception e) {
        }
    }

    //老虎机
    public static void jumpSlotMachineActivity() {
        try {
            Intent intent = RounterBus.getRounter(RounterApi.class).getIntentSlotMachine();
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            AppliContext.get().startActivity(intent);
        } catch (Exception e) {
        }
    }


    // 刮刮卡
    public static void jumpShaveActivity() {
        try {
            Intent intent = RounterBus.getRounter(RounterApi.class).getIntentShave();
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            AppliContext.get().startActivity(intent);
        } catch (Exception e) {
        }
    }

    // 任务页
    public static void jumpTaskActivity() {
        try {
            Intent intent = RounterBus.getRounter(RounterApi.class).getIntentTask();
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            AppliContext.get().startActivity(intent);
        } catch (Exception e) {
        }
    }

    //看看赚跳转
    public static void jumpReadH5Activity(String url, String title, String slot_id, String sid) {
        try {
            Intent intent = RounterBus.getRounter(RounterApi.class).getIntentReadH5(url, title, slot_id, sid);
            intent.putExtra("url", url);
            intent.putExtra("title", title);
            intent.putExtra("slot_id", slot_id);
            intent.putExtra("sid", sid);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            AppliContext.get().startActivity(intent);
        } catch (Exception e) {
        }
    }


    //互动广告跳转
    public static void jumpAdWebActivity(String url, String title, String slot_id, String sid, String incentive_level, String alert, String alertRenderType, String timeTrigger, String delay, String deep_url_re) {
        try {
            Intent intent = RounterBus.getRounter(RounterApi.class)
                    .getIntentAdWeb(incentive_level, alert, alertRenderType, timeTrigger, delay, deep_url_re, url, title, slot_id, sid);
            intent.putExtra("url", url);
            intent.putExtra("title", title);
            intent.putExtra("slot_id", slot_id);
            intent.putExtra("sid", sid);
            intent.putExtra("deep_url_re", deep_url_re);
            intent.putExtra("delay", delay);
            intent.putExtra("timeTrigger", timeTrigger);
            intent.putExtra("alertRenderType", alertRenderType);
            intent.putExtra("alert", alert);
            intent.putExtra("incentive_level", incentive_level);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            AppliContext.get().startActivity(intent);
        } catch (Exception e) {
        }
    }


    //刮刮卡详情
    public static void jumpScratchCardActivity(ShaveListEntity.ShaveEntity entity) {
        try {
            Intent intent = RounterBus.getRounter(RounterApi.class).getIntentScratchCard();
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra("json",entity);
            AppliContext.get().startActivity(intent);
        } catch (Exception e) {
        }
    }

    //双色球
    public static void jumpDoubleColorActivity(Boolean ifnex) {
        try {
            Intent intent = RounterBus.getRounter(RounterApi.class).getIntentDoubleColor(ifnex);
            intent.putExtra("ifnex", ifnex);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            AppliContext.get().startActivity(intent);
        } catch (Exception e) {
        }
    }

    //双色球
    public static void jumpRunLotteryActivity() {
        try {
            Intent intent = RounterBus.getRounter(RounterApi.class).getIntentRunLottery();
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            AppliContext.get().startActivity(intent);

        } catch (Exception e) {
        }
    }

    //抽奖记录
    public static void jumpSlotMachineRecordActivity() {
        try {
            Intent intent = RounterBus.getRounter(RounterApi.class).getIntentSlotMachineRecord();
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            AppliContext.get().startActivity(intent);
        } catch (Exception e) {
        }
    }

    //金币明细
    public static void jumpGoldDetailsActivity() {
        try {
            Intent intent = RounterBus.getRounter(RounterApi.class).getIntentGoldDetails();
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            AppliContext.get().startActivity(intent);
        } catch (Exception e) {
        }
    }

    //小满
    public static void jumpXiaomanActivity(String title) {
        try {
            Intent intent = RounterBus.getRounter(RounterApi.class).getIntentXiaoMan(title);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            AppliContext.get().startActivity(intent);
        } catch (Exception e) {
        }
    }

    //福利中心
    public static void jumpWelfareActivity() {
        try {
            Intent intent = RounterBus.getRounter(RounterApi.class).getIntentWelfare();
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            AppliContext.get().startActivity(intent);
        } catch (Exception e) {
        }
    }

    //打卡提现
    public static void jumpClockInWithdrawActivity() {
        try {
            Intent intent = RounterBus.getRounter(RounterApi.class).getIntentClockInWithdraw();
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            AppliContext.get().startActivity(intent);
        } catch (Exception e) {
        }
    }

    //广告跳转
    public static void adJump(NewsEntity data,Activity activity) {
        Log.d("JumpUtils", "data.title =" + data.getTitle());
        Log.d("JumpUtils", "data.openType =" + data.getOpentype());
        Log.d("JumpUtils", "data.sid =" + data.getSid());
        Log.d("JumpUtils", "data.appmodel_id =" + data.getAppmodel_id());

        String sid = String.valueOf(data.getSid());
        int incentiveLevel = data.getIncentive_level();
        int appmodelId = data.getAppmodel_id();

        if (data.getOpentype() == NewsEntity.AD_TYPE_URL) {
            if (incentiveLevel == 9 || incentiveLevel == 3 || incentiveLevel == 7 || incentiveLevel == 8) {
                JumpUtils.jumpReadH5Activity(
                        data.getUrl(),
                        data.getTitle(),
                        data.getSlot_id(),
                        sid
                );
            } else if (TextUtils.equals(sid, "2079")) {
                //签到提醒奖励
            } else if (TextUtils.equals(sid, "2152")) {
                //绑定微信号跳转
            } else if (TextUtils.equals("2080", sid + "")) {
                //签到提现任务
            } else if (incentiveLevel > 0) {
                if (sid.equals("2325")) {
                    //成就殿堂
                    EventUtils.INSTANCE.onEvent("task_daily_goto_medal", "日常任务-成就殿堂入口");
                } else if (sid.equals("2326")) {
                    //金币钱包
                    EventUtils.INSTANCE.onEvent("task_daily_goto_10w", "日常任务-10万任务入口");
                } else if (sid.equals("2290")) {
                    //现金红包
                }
                JumpUtils.jumpAdWebActivity(
                        data.getUrl(),
                        data.getTitle(),
                        data.getSlot_id(),
                        sid,
                        String.valueOf(incentiveLevel),
                        data.getAlert(),
                        data.getAlertRenderType(),
                        data.getTimeTrigger(),
                        String.valueOf(data.getDelay()),
                        data.getDeep_url_re()
                );
            } else {
                h5Jump(data.getTitle(), data.getUrl(),false,activity);
            }
        } else if (data.getOpentype() == NewsEntity.AD_TYPE_APPMODEL && data.getStatus() != 2) {
            if (appmodelId == 59) {
                //看视频赚金币
//                EventUtils.INSTANCE.onEvent("task_daily_ad", "日常任务-广告视频按钮");
//                RxBus.get().post(Constant.OPEN_MAIN_PAGE, 0);
            } else if (appmodelId == 60) {
                //听音乐领金币
//                EventUtils.INSTANCE.onEvent("task_daily_goto_voice", "日常任务-铃声入口");
//                RxBus.get().post(Constant.OPEN_MAIN_PAGE, 1);
            } else if (appmodelId == 36) {
//                RxBus.get().post(Constant.OPEN_MAIN_PAGE, 2);
            } else if (TextUtils.equals(sid, "2151")) {
                //绑定手机号跳转
            } else if (appmodelId == 27) {
                //提现跳转
                EventUtils.INSTANCE.onEvent("新手任务-第一次提现-去完成", "task_new_goto_money_unfinish");
                withdrawCash();
            } else if (appmodelId == 54) {
                //红包雨
                activityJump(RounterBus.getRounter(RounterApi.class).getIntentActivityRedPackage());
                EventUtils.INSTANCE.onEvent("task_daily_active_game_3", "日常任务-互动游戏位3");
            } else if (appmodelId == 37) {
                //刮刮卡
                jumpShaveActivity();
                EventUtils.INSTANCE.onEvent("task_daily_active_game_1", "日常任务-互动游戏位1");
            } else if (appmodelId == 33 || TextUtils.equals(sid, "2004")) {
                //幸运抽奖
                jumpLuckDrawActivity();
                EventUtils.INSTANCE.onEvent("task_daily_active_game_2", "日常任务-互动游戏位2");
            } else if (appmodelId == 46) {
                //福利大抽奖
                jumpSlotMachineActivity();
            }
        } else if (data.getOpentype() == NewsEntity.AD_TYPE_SDK) {
            if (TextUtils.equals(sid, "2077")) {
                //看视频加金币
            }
            showAd(activity,data);

        }
    }

    public static void showAd(Activity activity,NewsEntity data) {
        AdUtils.INSTANCE.playRewardAd(activity, "box_click", new IAdVideoListener() {
            @Override
            public void onAdClose() { }

            @Override
            public void onError(@Nullable String errorMsg) { }
        });
    }

    public static void jumpPage(String pageName) {
        switch (pageName) {
            case "GuessFragment"://猜歌
                RxBusUtil.getDefault().send(RxBusConstant.RX_MAIN_ACTIVITY_TAB, new BusData("0", "GuessFragment"));
                break;
            case "RewardFragment"://娱乐
                RxBusUtil.getDefault().send(RxBusConstant.RX_MAIN_ACTIVITY_TAB, new BusData("1", "RewardFragment"));
                break;
            case "TaskFragment"://任务
                RxBusUtil.getDefault().send(RxBusConstant.RX_MAIN_ACTIVITY_TAB, new BusData("2", "TaskFragment"));
                break;
            case "MineFragment"://我的
                RxBusUtil.getDefault().send(RxBusConstant.RX_MAIN_ACTIVITY_TAB, new BusData("3", "MineFragment"));
                break;
            case "CampaignFragment"://小满
                RxBusUtil.getDefault().send(RxBusConstant.RX_MAIN_ACTIVITY_TAB, new BusData("4", "CampaignFragment"));
                break;
            default:

                break;
        }
    }


}
