package com.cooleshow.base.utils;

import android.text.TextUtils;

import com.alibaba.android.arouter.launcher.ARouter;
import com.cooleshow.base.bean.RouteBean;
import com.cooleshow.base.common.BaseApplication;
import com.cooleshow.base.common.WebConstants;
import com.cooleshow.base.constanst.Constants;
import com.cooleshow.base.constanst.RouteConstants;
import com.cooleshow.base.router.RouterPath;

import org.json.JSONObject;

/**
 * Author by pq, Date on 2022/6/29.
 */
public class JumpUtils {
    public static final String ACTION_H5 = "h5";
    public static final String ACTION_APP = "app";

    public static boolean jump(RouteBean routeBean) {
        if (routeBean == null) {
            return false;
        }
        try {

            String action = routeBean.action;

            if (TextUtils.equals(action, ACTION_APP)) {
                //跳转原生页面
                if (!TextUtils.isEmpty(routeBean.pageTag)) {
                    if (TextUtils.equals(routeBean.pageTag, RouteConstants.PAGE_TAG_BUY_PRACTICE) || TextUtils.equals(routeBean.pageTag, RouteConstants.PAGE_TAG_COURSE_REMIND)) {
                        //购买陪练课,跳转首页课表
                        ARouter.getInstance().build(RouterPath.APPCenter.PATH_HOME)
                                .withInt(Constants.MAIN_PAGE_SELECT_POTION_KEY, 1)
                                .navigation();
                        return true;
                    }

                    if (TextUtils.equals(routeBean.pageTag, RouteConstants.PAGE_TAG_EVALUATE)) {
                        //跳转评价页面
                        if (BaseApplication.Companion.isTeacherClient()) {
                            ARouter.getInstance().build(RouterPath.CommentCenter.TEACHER_RECEIVED_COMMENT)
                                    .navigation();
                        }
                    }

                    if (TextUtils.equals(routeBean.pageTag, RouteConstants.PAGE_TAG_HOMEWORK)) {
                        //原生跳作业详情页面
                        if (BaseApplication.Companion.isTeacherClient()) {
                            //老师端 区分琴房课和陪练课
                            String params = routeBean.params;
                            if (!TextUtils.isEmpty(params)) {
                                String courseId = getParams(params, "courseId");
                                String studentId = getParams(params, "studentId");
                                if (!TextUtils.isEmpty(courseId) && !TextUtils.isEmpty(studentId)) {
                                    ARouter.getInstance().build(RouterPath.WorkCenter.TEACHER_WORK_ASSIGN_HOMEWORK)
                                            .withString("course_id", courseId)
                                            .withString("student_id", studentId)
                                            .navigation();
                                }
                            }
                        } else {
                            //学生端直接跳作业详情页面
                            String params = routeBean.params;
                            if (!TextUtils.isEmpty(params)) {
                                String courseId = getParams(params, "courseId");
                                if (!TextUtils.isEmpty(courseId)) {
                                    ARouter.getInstance().build(RouterPath.WorkCenter.STUDENT_HOMEWORK_DETAIL)
                                            .withString("course_id", courseId)
                                            .navigation();
                                }
                            }
                        }
                    }

                    if (TextUtils.equals(routeBean.pageTag, RouteConstants.PAGE_TAG_MESSAGE)) {
                        //消息页面
                        ARouter.getInstance().build(RouterPath.MessageCenter.TEACHER_MESSAGE_MESSAGEBOX)
                                .navigation();
                    }
                }
            } else {
                //跳转H5页面
                if (TextUtils.isEmpty(routeBean.url)) {
                    return false;
                }
                ARouter.getInstance()
                        .build(RouterPath.WebCenter.ACTIVITY_HTML)
                        .withString(WebConstants.WEB_URL, routeBean.url)
                        .navigation();
                return true;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    private static String getParams(String paramsJson, String key) {
        try {
            paramsJson.replace("\\", "");
            JSONObject jsonObject = new JSONObject(paramsJson);
            return jsonObject.optString(key);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 跳转首页,选中目标页面
     * @param selectPosition
     */
    public static void jumpMain(int selectPosition){
        ARouter.getInstance().build(RouterPath.APPCenter.PATH_HOME)
                .withInt(Constants.MAIN_PAGE_SELECT_POTION_KEY, selectPosition)
                .navigation();
    }
}
