/*
 * @Author: wangyang 1147192855@qq.com
 * @Date: 2022-07-05 16:50:28
 * @LastEditors: wangyang 1147192855@qq.com
 * @LastEditTime: 2022-07-22 16:51:19
 * @FilePath: \marking_app\lib\routes\RouterManager.dart
 * @Description: 路由
 */

import 'dart:convert';

import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:marking_app/common/model/enum/marking_list_type.dart';
import 'package:marking_app/pages/common/startUpPage.dart';
import 'package:marking_app/pages/homework_correction/do_papers_job_exam.dart';
import 'package:marking_app/pages/homework_correction/index.dart';
import 'package:marking_app/pages/homework_correction/job_knowledge_points.dart';
import 'package:marking_app/pages/homework_correction/job_knowledge_points_detail.dart';
import 'package:marking_app/pages/homework_correction/job_personal_detail.dart';
import 'package:marking_app/pages/homework_correction/job_priority_review_set.dart';
import 'package:marking_app/pages/homework_correction/job_report.dart';
import 'package:marking_app/pages/homework_correction/job_student_group.dart';
import 'package:marking_app/pages/homework_correction/pages/job_favorite.dart';
import 'package:marking_app/pages/homework_correction/pages/job_list_participate_in_class.dart';
import 'package:marking_app/pages/homework_correction/review_job.dart';
import 'package:marking_app/pages/homework_correction/quick_check_personal.dart';
import 'package:marking_app/pages/homework_correction/quick_data_check.dart';
import 'package:marking_app/pages/login/index.dart';
import 'package:marking_app/pages/mainPage.dart';
import 'package:marking_app/pages/marking/do_papers.dart';
import 'package:marking_app/pages/homework_correction/do_papers_job.dart' as DoPapersHomework;
import 'package:marking_app/pages/marking/progress.dart';
import 'package:marking_app/pages/marking/progress_abnormal.dart';
import 'package:marking_app/pages/marking/review.dart';
import 'package:marking_app/pages/mine/index.dart';
import 'package:marking_app/pages/mine/other_pages/index.dart';
import 'package:marking_app/pages/other/agreement_page.dart';
import 'package:marking_app/pages/report_detail/index.dart';
import 'package:marking_app/pages/report_detail/report_history.dart';
import 'package:marking_app/pages/reports/report_class_teacher.dart';
import 'package:marking_app/pages/reports/report_personal_subject.dart';
import 'package:marking_app/pages/reports/report_subject_teacher.dart';
import 'package:marking_app/utils/const_text.dart';
import 'package:marking_app/utils/the_global.dart';

import '../utils/index.dart';

class RouterManager {
  static const String root = '/';
  static const String startUpPath = '/startUpPath';
  static const String loginPath = '/login';
  static const String markingProgressPath = '/marking/progress';
  static const String markingReviewPath = '/marking/review';
  static const String markingJobReviewPath = '/marking/job/review';
  static const String markingReviewAbnormalPath = '/marking/review/abnormal';
  static const String markingDoPath = '/marking/do_papers';
  static const String markingHomeworkDoPath = '/marking_homework/do_papers';
  static const String agreementPath = 'other/agreement_page';
  static const String ohterMainPagePath = 'other/index';

  // 作业
  static const String jobReportPagePath = '/job/report/index';
  // 作业 ==> 横屏考试
  static const String jobExamPagePath = '/job/exam/index';
  // 作业 ==> 列表 ==> 参与班级
  static const String jobListParticipateInClassPath = '/job/list/participateInClass';
  // 作业 ==> 收藏页面
  static const String jobFavoritePagePath = '/job/favorite/index';
// 作业 ==> 列表页面
  static const String jobMainListPagePath = '/job/mainList/index';

  static const String reportClassTeacherPath = 'report/details/reportClassTeacher';
  static const String reportSubjectTeacherPath = 'report/details/reportSubjectTeacher';
  static const String reportPersonalSubjectPath = 'report/details/reportPersonalSubject';
  static const String userMinePath = 'user/mine/index';
  static const String reportDetailPath = '/report_detail/index';
  static const String quickDataCheckPath = '/homework_correction/quick_data_check';
  static const String quickCheckPersonalPath = '/homework_correction/quick_check_personal';
  static const String jobPriorityReviewSetPath = '/homework_correction/job_priority_review_set';
  static const String jobStudentGroupPath = '/homework_correction/job_student_group';
  static const String jobPersonalDetailPath = '/homework_correction/job_personal_detail';
  static const String reportCardDialogPath = '/report_detail/widgets/report_card_dialog';
  static const String reportHistoryPath = '/report_detail/report_history';
  static const String jobKnowledgePointsPath = '/homework_correction/job_knowledge_points';
  static const String jobKnowledgePointsDetailPath = '/homework_correction/job_knowledge_points_detail';

  // TheMine

  static final FluroRouter router = FluroRouter();

  static void initRouter() {
    _defineRoutes();
  }

  static Route<dynamic>? generator(RouteSettings routeSettings) {
    var context = TheGlobal.navigatorKey.currentContext;
    if (context != null) {
      var settings = ModalRoute.of(context)?.settings;
      if (settings != null) {
        print('路由信息：');
        print(settings.name);
      }
      print('ssssssssss');
    }
    return router.generator(routeSettings);
  }

  // 启动页
  static final _startUpPageHandler =
      Handler(handlerFunc: (BuildContext? context, Map<String, List<String>> params) => const StartUpPage());
  // 主页
  static final _mainPageHandler =
      Handler(handlerFunc: (BuildContext? context, Map<String, List<String>> params) => const TheMainPage());
  // 登录页
  static final _loginPageHandler =
      Handler(handlerFunc: (BuildContext? context, Map<String, List<String>> params) => const TheLogin());
  // 阅卷进度页面
  static final _progressPageHandler = Handler(
      handlerFunc: (BuildContext? context, Map<String, List<String>> params) =>
          Progress(int.parse(params['examSubjectId']![0]), params['name']![0]));
  // 回评页面
  static final _reviewPageHandler = Handler(handlerFunc: (BuildContext? context, Map<String, List<String>> params) {
    int markingUserId = int.parse(params['markingUserId']![0]);
    int examSubjectId = int.parse(params['examSubjectId']![0]);

    String examName = params['examName']![0];
    return Review(markingUserId, examSubjectId, examName);
  });
  static final _reviewJobPageHandler = Handler(handlerFunc: (BuildContext? context, Map<String, List<String>> params) {
    int markingUserId = int.parse(params['markingUserId']![0]);
    int examSubjectId = int.parse(params['examSubjectId']![0]);

    String examName = params['examName']![0];
    return ReviewJob(markingUserId, examSubjectId, examName);
  });

  // 回评异常页面
  static final _reviewAbnormalPageHandler = Handler(
      handlerFunc: (BuildContext? context, Map<String, List<String>> params) => ProgressAbnormal(
          int.parse(params['examSubjectId']![0]),
          params['markingId']![0],
          params['name']![0],
          params['questionNum']![0]));
  // 阅卷页面
  static final _markingDoPageHandler = Handler(handlerFunc: (BuildContext? context, Map<String, List<String>> params) {
    try {
      int markingtype = int.parse(params['markingtype']?[0] ?? '-1');
      int markingUserId = int.parse(params['markingUserId']![0]);
      int examSubjectId = int.parse(params['examSubjectId']![0]);
      // ignore: unrelated_type_equality_checks
      bool isReview = params['isReview']?[0] == '1';
      bool exceptional = params['exceptional']?[0] == '1';

      String? questionNum = params['questionNum']?[0];

      int? pageOper;
      if (params['pageOper'] != null && params['pageOper']![0] != null && params['pageOper']![0] != 'null') {
        pageOper = int.parse(params['pageOper']![0]);
      }

      int? detailId;
      if (params['detailId'] != null && params['detailId']![0] != null && params['detailId']![0] != 'null') {
        detailId = int.parse(params['detailId']![0]);
      }
      MarkingListType? markType;
      if (markingtype != -1) {
        markType = MarkingListType.values[markingtype];
      }

      return DoPapers(
        markingUserId,
        markingtype: markType,
        examSubjectId: examSubjectId,
        isReview: isReview,
        pageOper: pageOper ?? 2,
        exceptional: exceptional,
        markingUserDetailId: detailId ?? 0,
        questionNum: questionNum,
      );
    } catch (e) {
      toPrint(val: '进入跳转报错啦，$e');
    }
  });
// 批阅作业页面
  static final _markingHomeworkDoPageHandler =
      Handler(handlerFunc: (BuildContext? context, Map<String, List<String>> params) {
    try {
      int taskId = int.parse(params['taskId']![0]);
      int jobId = int.parse(params['jobId']![0]);
      String taskName = params['taskName']![0];
      String className = params['className']![0];
      return DoPapersHomework.DoPapersJob(taskId: taskId, jobId: jobId, taskName: taskName, className: className);
    } catch (e) {
      toPrint(val: '进入跳转报错啦，$e');
    }
  });
  // 作业报告页面
  static final _jobReportPageHandler = Handler(handlerFunc: (BuildContext? context, Map<String, List<String>> params) {
    int id = int.parse(params['id']![0]);
    String title = params['title']![0];
    int? schoolId;
    if (params['schoolId'] != null && params['schoolId']?[0] != null && params['schoolId']![0] != 'null') {
      schoolId = int.parse(params['schoolId']![0]);
    }
    int? gradeId;
    if (params['gradeId'] != null && params['gradeId']?[0] != null && params['gradeId']![0] != 'null') {
      gradeId = int.parse(params['gradeId']![0]);
    }
    String? className = params['className']?[0];
    return JobReport(id: id, title: title, schoolId: schoolId, gradeId: gradeId, className: className);
  });

  // 作业报告页面
  static final _jobExamPageHandler = Handler(handlerFunc: (BuildContext? context, Map<String, List<String>> params) {
    int taskId = int.parse(params['taskId']![0]);
    int jobId = int.parse(params['JobId']![0]);
    String taskName = params['taskName']![0];
    String className = params['className']![0];
    return DoPapersJobExam(taskId: taskId, jobId: jobId, taskName: taskName, className: className);
  });

  static final _agreementPageHandler = Handler(handlerFunc: (BuildContext? context, Map<String, List<String>> params) {
    AGREEMENT_KEY key = AGREEMENT_KEY.values.byName(params['type']![0]);
    return AgreementPage(type: key);
  });
  static final _ohterMainPageHandler = Handler(
    handlerFunc: (BuildContext? context, Map<String, List<String>> params) => const OhterPage(),
  );

  static final _reportClassTeacherPageHandler =
      Handler(handlerFunc: (BuildContext? context, Map<String, List<String>> params) {
    int examId = int.parse(params['examId']![0]);

    return ReportClassTeacher(examId: examId);
  });

  static final _reportSubjectTeacherPageHandler = Handler(
    handlerFunc: (BuildContext? context, Map<String, List<String>> params) {
      int examId = int.parse(params['examId']![0]);
      int classId = int.parse(params['classId']![0]);
      int subject = int.parse(params['subject']![0]);
      String subjectName = params['subjectName']![0];
      return ReportSubjectTeacher(examId: examId, classId: classId, subject: subject, subjectName: subjectName);
    },
  );

  static final _userMinePageHandler = Handler(
    handlerFunc: (BuildContext? context, Map<String, List<String>> params) => TheMine(),
  );
  static final _reportDetailPath = Handler(
    handlerFunc: (BuildContext? context, Map<String, List<String>> params) {
      int examId = int.parse(params['examId']![0]);
      bool showGrade = params['showGrade']![0] == 'true';
      return ReportDetail(examId: examId, showGrade: showGrade);
    },
  );

  static final _reportPersonalSubjectPageHandler = Handler(
    handlerFunc: (BuildContext? context, Map<String, List<String>> params) {
      int examId = int.parse(params['examId']![0]);
      int userId = int.parse(params['userId']![0]);
      int subject = int.parse(params['subject']![0]);
      String subjectName = params['subjectName']![0];
      String studentName = params['studentName']![0];

      return ReportPersonalSubject(
        examId: examId,
        userId: userId,
        subject: subject,
        studentName: studentName,
        subjectName: subjectName,
      );
    },
  );

  static final _jobListParticipateInClassHandler = Handler(
    handlerFunc: (BuildContext? context, Map<String, List<String>> params) {
      int jobId = int.parse(params['jobId']![0]);
      String jobName = params['jobName']![0];
      String genderName = params['genderName']![0];
      // ignore: sdk_version_since
      bool completed = bool.parse(params['completed']?[0] ?? 'false');
      return JobListParticipateInClass(jobId: jobId, jobName: jobName, genderName: genderName, completed: completed);
    },
  );

  // 数据快查
  static final _quickDataCheckPageHandler = Handler(
    handlerFunc: (BuildContext? context, Map<String, List<String>> params) {
      int jobId = int.parse(params['jobId']![0]);
      String className = params['className']![0];
      String gradeName = params['gradeName']![0];
      int? schoolId;
      if (params['schoolId'] != null && params['schoolId']?[0] != null && params['schoolId']![0] != 'null') {
        schoolId = int.parse(params['schoolId']![0]);
      }
      int? gradeId;
      if (params['gradeId'] != null && params['gradeId']?[0] != null && params['gradeId']![0] != 'null') {
        gradeId = int.parse(params['gradeId']![0]);
      }
      return QuickDataCheckPage(jobId: jobId, className: className,gradeName:gradeName,schoolId:schoolId,gradeId:gradeId);
    },
  );

  // 数据快查-个人信息
  static final _quickCheckPersonalPageHandler = Handler(
    handlerFunc: (BuildContext? context, Map<String, List<String>> params) {
      int jobId = int.parse(params['jobId']![0]);
      int studentId = int.parse(params['studentId']![0]);
      return QuickCheckPersonal(jobId: jobId, studentId: studentId);
    },
  );

  //优先配置
  static final _jobPriorityReviewSetPageHandler = Handler(
    handlerFunc: (BuildContext? context, Map<String, List<String>> params) {
      String groupId = params['groupId']![0];
      String title = params['title']![0];
      String? page = params['page']?[0];
      return JobPriorityReviewSet(groupId: groupId,title:title,page:page);
    },
  );

  //学生分组
  static final _jobStudentGroupPageHandler = Handler(
    handlerFunc: (BuildContext? context, Map<String, List<String>> params) {
      String page = params['page']![0];
      return JobStudentGroup(page:page);
    },
  );
  //作业收藏页面
  static final _jobFavoritePagePathHandler = Handler(
    handlerFunc: (BuildContext? context, Map<String, List<String>> params) {
      int jobId = int.parse(params['jobId']![0]);
      String jobName = params['jobName']![0];
      String? className = params['className']?[0];
      int? schoolId;
      if (params['schoolId'] != null && params['schoolId']?[0] != null && params['schoolId']![0] != 'null') {
        schoolId = int.parse(params['schoolId']![0]);
      }
      int? gradeId;
      if (params['gradeId'] != null && params['gradeId']?[0] != null && params['gradeId']![0] != 'null') {
        gradeId = int.parse(params['gradeId']![0]);
      }
      return JobFavorite(jobId: jobId, gradeId: gradeId, schoolId: schoolId, className: className,jobName:jobName);
    },
  );

  // 优先配置个人详情
  static final _jobPersonalDetailPathHandler = Handler(
    handlerFunc: (BuildContext? context, Map<String, List<String>> params) {
      String studentName = params['studentName']![0];
      int studentId = int.parse(params['studentId']![0]);
      return JobPersonalDetail(studentId: studentId,studentName:studentName);
    },
  );

  // 优先配置个人详情
  static final _jobMainListPathHandler = Handler(
    handlerFunc: (BuildContext? context, Map<String, List<String>> params) => HomeworkCorrection(),
  );

// 学生报告详情
  static final _reportCardDialogPathHandler = Handler(
    handlerFunc: (BuildContext? context, Map<String, List<String>> params) {
     /* String studentName = params['studentName']![0];
      int studentId = int.parse(params['studentId']![0]);
      return ReportCardDialog(studentId: studentId,studentName:studentName);*/
    },
  );
// 学生历史报告
  static final _reportHistoryPathHandler = Handler(
    handlerFunc: (BuildContext? context, Map<String, List<String>> params) {
     /* String studentName = params['studentName']![0];
      int studentId = int.parse(params['studentId']![0]);*/
      return ReportHistory();
    },
  );

  // 知识点掌握
  static final _jobKnowledgePointsPathHandler = Handler(
    handlerFunc: (BuildContext? context, Map<String, List<String>> params) {
      /* String studentName = params['studentName']![0];
      int studentId = int.parse(params['studentId']![0]);*/
      return JobKnowledgePoints();
    },
  );

  // 知识点掌握详情
  static final _jobKnowledgePointsDetailPathHandler = Handler(
    handlerFunc: (BuildContext? context, Map<String, List<String>> params) {
       String knowledgeName = params['knowledgeName']![0];
      int knowledgeId = int.parse(params['knowledgeId']![0]);
      return JobKnowledgePointsDetail(knowledgeName:knowledgeName,knowledgeId:knowledgeId);
    },
  );
  // 开始阅卷页面
  // static final _doMarkingPapers = Handler(handlerFunc: (BuildContext? context, Map<String, List<String>> params) => MarkingPapers());

  //未授权页面处理器
  // static final permissionDeniedHandler =
  //     Handler(handlerFunc: (BuildContext context, Map<String, dynamic> params) {
  //   return PermissionDenied();
  // });

  static void _defineRoutes() {
    router.define(root, handler: _mainPageHandler, transitionType: TransitionType.material);
    router.define(startUpPath, handler: _startUpPageHandler, transitionType: TransitionType.material);
    router.define(loginPath, handler: _loginPageHandler);
    router.define(markingProgressPath, handler: _progressPageHandler, transitionType: TransitionType.material);
    router.define(markingReviewPath, handler: _reviewPageHandler, transitionType: TransitionType.material);
    router.define(markingJobReviewPath, handler: _reviewJobPageHandler, transitionType: TransitionType.material);
    router.define(markingDoPath, handler: _markingDoPageHandler, transitionType: TransitionType.material);
    router.define(markingHomeworkDoPath,
        handler: _markingHomeworkDoPageHandler, transitionType: TransitionType.material);
    router.define(markingReviewAbnormalPath,
        handler: _reviewAbnormalPageHandler, transitionType: TransitionType.material);
    router.define(agreementPath, handler: _agreementPageHandler, transitionType: TransitionType.material);
    router.define(ohterMainPagePath, handler: _ohterMainPageHandler, transitionType: TransitionType.material);

    router.define(reportClassTeacherPath,
        handler: _reportClassTeacherPageHandler, transitionType: TransitionType.material);
    router.define(reportSubjectTeacherPath,
        handler: _reportSubjectTeacherPageHandler, transitionType: TransitionType.material);
    router.define(reportPersonalSubjectPath,
        handler: _reportPersonalSubjectPageHandler, transitionType: TransitionType.material);
    router.define(userMinePath, handler: _userMinePageHandler, transitionType: TransitionType.material);
    router.define(reportDetailPath, handler: _reportDetailPath, transitionType: TransitionType.material);
    router.define(jobReportPagePath, handler: _jobReportPageHandler, transitionType: TransitionType.material);
    router.define(jobExamPagePath, handler: _jobExamPageHandler, transitionType: TransitionType.material);
    router.define(jobListParticipateInClassPath,
        handler: _jobListParticipateInClassHandler, transitionType: TransitionType.material);
    router.define(quickDataCheckPath, handler: _quickDataCheckPageHandler, transitionType: TransitionType.material);
    router.define(quickCheckPersonalPath,
        handler: _quickCheckPersonalPageHandler, transitionType: TransitionType.material);
    router.define(jobPriorityReviewSetPath,
        handler: _jobPriorityReviewSetPageHandler, transitionType: TransitionType.material);
    router.define(jobStudentGroupPath, handler: _jobStudentGroupPageHandler, transitionType: TransitionType.material);
    router.define(jobFavoritePagePath, handler: _jobFavoritePagePathHandler, transitionType: TransitionType.material);
    router.define(jobPersonalDetailPath, handler: _jobPersonalDetailPathHandler, transitionType: TransitionType.material);
    router.define(reportCardDialogPath, handler: _reportCardDialogPathHandler, transitionType: TransitionType.material);
    router.define(reportHistoryPath, handler: _reportHistoryPathHandler, transitionType: TransitionType.material);
    router.define(jobMainListPagePath, handler: _jobMainListPathHandler, transitionType: TransitionType.material);
    router.define(jobKnowledgePointsPath, handler: _jobKnowledgePointsPathHandler, transitionType: TransitionType.material);
    router.define(jobKnowledgePointsDetailPath, handler: _jobKnowledgePointsDetailPathHandler, transitionType: TransitionType.material);

// getTransition()

    router.notFoundHandler = Handler(handlerFunc: (BuildContext? context, Map<String, List<String>> params) {
      toPrint(val: "ROUTE WAS NOT FOUND !!!");
      return;
    });
  }
}
