import 'package:ef/ef.dart';
import 'package:flutter/cupertino.dart';
import 'package:path/path.dart';
import 'package:vbvs_app/component/tool/TopSlideNotification.dart';

import 'package:vbvs_app/controller/theme_controller/ThemeController.dart';
import 'package:vbvs_app/controller/user_info_controller.dart';
import 'package:vbvs_app/main.dart';
import 'package:vbvs_app/pages/main_bottom/component/main_page_b_bottom_change.dart';
import 'package:vbvs_app/pages/mh_page/HomeDeviceType.dart';
import 'package:vbvs_app/pages/mh_page/LanguagePage.dart';
import 'package:vbvs_app/pages/mh_page/ShareDeviceDetailWidget.dart';
import 'package:vbvs_app/pages/mh_page/about_us.dart';
import 'package:vbvs_app/pages/mh_page/address_list_page.dart';
import 'package:vbvs_app/pages/mh_page/applyRepair/apply_repair_success.dart';
import 'package:vbvs_app/pages/mh_page/apply_repair_page.dart';
import 'package:vbvs_app/pages/mh_page/book_info_page.dart';
import 'package:vbvs_app/pages/mh_page/book_success_page.dart';
import 'package:vbvs_app/pages/mh_page/device/mht_bind_device_success.dart';
import 'package:vbvs_app/pages/mh_page/device/mht_bind_device_type.dart';
import 'package:vbvs_app/pages/mh_page/device/mht_blueteeth_device_page.dart';
import 'package:vbvs_app/pages/mh_page/device/mht_device_calibration.dart';
import 'package:vbvs_app/pages/mh_page/device/mht_people_info.dart';
import 'package:vbvs_app/pages/mh_page/device/mht_wifi_page.dart';
import 'package:vbvs_app/pages/mh_page/device_list.dart';
import 'package:vbvs_app/pages/mh_page/device_people_info.dart';
import 'package:vbvs_app/pages/mh_page/device_share_page.dart';
import 'package:vbvs_app/pages/mh_page/edit_bed.dart';
import 'package:vbvs_app/pages/mh_page/help/article.dart';
import 'package:vbvs_app/pages/mh_page/homepage/new_Home_page.dart';
import 'package:vbvs_app/pages/mh_page/privacy_policy.dart';
import 'package:vbvs_app/pages/mh_page/user/page/edit_userinfo_page.dart';
import 'package:vbvs_app/pages/mh_page/experience_store_page.dart';
import 'package:vbvs_app/pages/mh_page/issue_list_page.dart';
import 'package:vbvs_app/pages/mh_page/my_experience_page.dart';
import 'package:vbvs_app/pages/mh_page/people_info.dart';
import 'package:vbvs_app/pages/mh_page/user/page/register_page.dart';
import 'package:vbvs_app/pages/mh_page/repair_history_page.dart';
import 'package:vbvs_app/pages/mh_page/repair_list.dart';
import 'package:vbvs_app/pages/mh_page/room_picker.dart';
import 'package:vbvs_app/pages/mh_page/sleep_habit.dart';
import 'package:vbvs_app/pages/mh_page/smys.dart';
import 'package:vbvs_app/pages/mh_page/user/page/find_password_page.dart';
import 'package:vbvs_app/pages/mh_page/user/page/mht_login_page.dart';
import 'package:vbvs_app/pages/mh_page/user/page/rxhx_mht.dart';
import 'package:vbvs_app/pages/mh_page/user/page/update_password_page.dart';
import 'package:vbvs_app/pages/mh_page/user_agreement_page.dart';
import 'package:vbvs_app/pages/sleep_report/new_sleep_report_page.dart';
import '../pages/mh_page/bluetooth.dart';
import '../pages/mh_page/edit_address_page.dart';
import '../pages/mh_page/message/messageDetail.dart';
import '../pages/mh_page/message_page.dart';
import '../pages/mh_page/new_settingPage.dart';

ThemeController themeController = Get.find();

var mhroutes = {
  "/mianPageBottomChange": (contxt) => MainPageBBottomChange(),
  "/homePage": (context) => NewHomePage(),
  "/homeDeviceType": (contxt) => HomeDeviceType(),
  "/editUserInfoPage": (contxt) => EditUserPage(),
  "/peopleInfoPage": (contxt, {arguments}) => PeopleInfoPage(
        data: arguments,
      ),
  "/repairHistoryListPage": (contxt, {arguments}) =>
      RepairHistoryListPage(data: arguments),
  "/deviceRepairPage": (contxt) => DeviceRepairPage(),
  "/settingPage": (contxt) => SettingPage(),
  "/issueListpage": (contxt) => IssueListPage(),
  "/messagePage": (contxt) => MessagePage(),
  "/addressListPage": (contxt) => AddressListPage(),
  "/editAddressPage": (contxt) => EditAddressPage(),
  "/languagePage": (context) => LanguagePage(),
  '/bluetoothPage': (context, {arguments}) => BluetoothPage(data: arguments),
  "/devicePeopleInfo": (context, {arguments}) =>
      DevicePeopleInfo(data: arguments),
  "/sleepHabitPage": (context) => SleepHabitPage(),
  '/syms': (context) => Smys(),
  "/applyRepairPage": (context) => ApplyRepairPage(),
  "/roomPickerPage": (context, {arguments}) => RoomPickerPage(data: arguments),
  "/editBedPage": (context, {arguments}) => EditBedPage(data: arguments),
  "/experienceStorePage": (context) => ExperienceStorePage(),
  "/bookInfoPage": (context, {arguments}) =>
      BookInfoPage(data: arguments["data"]),
  "/bookSuccessPage": (contxt, {arguments}) =>
      BookSuccessPage(data: arguments["data"]),
  "/myExperiencePage": (context) => MyExperiencePage(),
  "/registerPage": (context) => RegisterPage(),
  "/findPasswordPage": (context) => FindPasswordPage(),
  "/loginPage": (context) => MHTLoginPage(),
  "/updatePasswordPage": (context) => UpdatePasswordPage(),
  "/deviceSharePage": (context, {arguments}) =>
      ShareDeviceWidget(data: arguments),
  "/deviceListPage": (context) => DeviceListPage(),
  "/mHTDeviceTypePage": (context, {arguments}) => MHTBindDeviceTypePage(),
  "/mHTBlueteethDevicePage": (context, {arguments}) =>
      MHTBlueteethDevicePage(deviceType: arguments),
  "/mHTwifiPage": (contxt, {arguments}) => MHTWifiPage(deviceInfo: arguments),
  "/calibrationPage": (contxt) => MHTCalibrationPage(),
  "/bindDeviceSuccess": (contxt) => MHTBindDeviceSuccess(),
  "/newSleepReportPage": (contxt, {arguments}) =>
      NewSleepReportPage(data: arguments),
  "/rxhxMht": (context) => RxhxMht(),
  "/helpArticle": (contxt, {arguments}) => HelpArticle(article: arguments),
  "/mHTPeopleInfoPage": (contxt) => MHTPeopleInfoPage(),
  "/applyRepairSuccess": (contxt) => ApplyRepairSuccess(),
  "/aboutUs": (contxt) => MhAboutUsPage(),
  "/userAgreementPage": (context) => UserAgreementPage(),
  "/privacyPolicyPage": (context) => PrivacyPolicyPage(),
  "/shareDeviceDetail": (context) => ShareDeviceDetailWidget(),
  "/messageDetail": (context, {arguments}) =>
      MessageDetailPage(data: arguments),
};
var mhonGenerateRoute = (RouteSettings settings) {
  final String? name = settings.name; // 获取路由名称，如 /news 或 /search
  final Function? pageContentBuilder = mhroutes[name]; // 根据路由名称获取对应的页面构建器

  if (pageContentBuilder != null) {
    UserInfoController userInfoController = Get.find();
    int loginStatus = userInfoController.model.login!;

    // 检查用户是否已登录，如果未登录且路由不是登录相关页面，跳转到登录页面

    if (loginStatus != 1) {
      if (settings.name != "/loginPage" &&
          settings.name != "/otherLoginPage" &&
          settings.name != "/mianPageBottomChange" &&
          settings.name != "/registerPage" &&
          settings.name != "/findPasswordPage") {
        // TopSlideNotification.show(
        //   navigatorKey.currentContext!,
        //   text: "必须登录提示".tr,
        //   textColor: themeController.currentColor.sc9,
        // );
        return CupertinoPageRoute(
          settings: RouteSettings(
            name: "/loginPage",
            arguments: settings.arguments,
          ),
          builder: (context) => mhroutes["/loginPage"]!(context),
        );
      }
    }

    // 如果有路由参数，传递参数
    if (settings.arguments != null) {
      return CupertinoPageRoute(
        settings: settings,
        builder: (context) =>
            pageContentBuilder!(context, arguments: settings.arguments),
      );
    } else {
      // 没有路由参数时，直接构建页面
      return CupertinoPageRoute(
        settings: settings,
        builder: (context) => pageContentBuilder!(context),
      );
    }
  }

  return null; // 如果路由名称没有匹配的页面，返回 null
};
