import 'package:ef/ef.dart';
import 'package:flutter/cupertino.dart';
import 'package:vbvs_app/common/util/MyUtils.dart';
import 'package:vbvs_app/component/tool/TopSlideNotification.dart';
import 'package:vbvs_app/controller/user_info_controller.dart';
import 'package:vbvs_app/pages/device/BodyDeviceWidget.dart';
import 'package:vbvs_app/pages/device/component/MessageSetting.dart';
import 'package:vbvs_app/pages/device/device_detail.dart';
import 'package:vbvs_app/pages/device/instant_body_page.dart';
import 'package:vbvs_app/pages/device/message_review_page.dart';
import 'package:vbvs_app/pages/device_bind/MobileScannerTestPage.dart';
import 'package:vbvs_app/pages/device_bind/after/after_device_calibration_person.dart';
import 'package:vbvs_app/pages/device_bind/after/after_update_person_page.dart';
import 'package:vbvs_app/pages/device_bind/after/after_wifi_page_person.dart';
import 'package:vbvs_app/pages/device_bind/bind_device_success.dart';
import 'package:vbvs_app/pages/device_bind/blueteeth_device_page.dart';
import 'package:vbvs_app/pages/device_bind/device_calibration.dart';
import 'package:vbvs_app/pages/device_bind/device_calibration_person.dart';
import 'package:vbvs_app/pages/device_bind/device_share_list_page.dart';
import 'package:vbvs_app/pages/device_bind/device_share_page.dart';
import 'package:vbvs_app/pages/device_bind/device_type.dart';
import 'package:vbvs_app/pages/device_bind/device_type_list.dart';
import 'package:vbvs_app/pages/device_bind/wifi_page.dart';
import 'package:vbvs_app/pages/device_bind/wifi_page_person.dart';
import 'package:vbvs_app/pages/login/other_login.dart';
import 'package:vbvs_app/pages/main_bottom/e_page.dart';
import 'package:vbvs_app/pages/main_bottom/follow_page.dart';
import 'package:vbvs_app/pages/main_bottom/help_page.dart';
import 'package:vbvs_app/pages/main_bottom/home_page.dart';
import 'package:vbvs_app/pages/main_bottom/main_page_bottom_change.dart';
import 'package:vbvs_app/pages/main_bottom/message_page.dart';
import 'package:vbvs_app/pages/main_bottom/mine_page.dart';
import 'package:vbvs_app/pages/person/person_page.dart';
import 'package:vbvs_app/pages/person/update_person_page.dart';
import 'package:vbvs_app/pages/repair/apply_repair_page.dart';
import 'package:vbvs_app/pages/repair/apply_repair_success.dart';
import 'package:vbvs_app/pages/repair/repair_detail_page.dart';
import 'package:vbvs_app/pages/repair/repair_list_page.dart';
import 'package:vbvs_app/pages/setting/common_mesaage_setting.dart';
import 'package:vbvs_app/pages/setting/language_setting.dart';
import 'package:vbvs_app/pages/sleep_report/new_sleep_report_page.dart';
import 'package:vbvs_app/pages/sleep_report/sleep_report_page.dart';
import 'package:vbvs_app/pages/user/about_us_page.dart';
import 'package:vbvs_app/pages/user/privacy_scheme_page.dart';
import 'package:vbvs_app/pages/user/setting_page.dart';
import 'package:vbvs_app/pages/user/update_user_page.dart';
import 'package:vbvs_app/pages/user/user_scheme_page.dart';

var routes = {
  "/homePage": (contxt) => HomePage(),
  "/sleepReportPage": (contxt, {arguments}) =>
      SleepReportPage(sleepUri: arguments),
  "/ePage": (contxt) => EPage(),
  "/messagePage": (contxt) => MessagePage(),
  "/minePage": (contxt) => MinePage(),
  "/mianPageBottomChange": (contxt) => MainPageBottomChange(),
  // "/loginPage": (contxt) => LoginPage(),
  // "/otherLoginPage": (contxt) => OtherLoginPage(),
  "/loginPage": (contxt) => OtherLoginPage(),
  // "/otherLoginPage": (contxt) => OtherLoginPage(),
  "/deviceType": (contxt, {arguments}) => DeviceTypePage(),
  "/blueteethDevice": (contxt) => BlueteethDevicePage(),
  "/personPage": (contxt) => PersonPage(),
  "/bindDeviceSuccess": (contxt) => BindDeviceSuccess(), 
  "/wifiPage": (contxt, {arguments}) => WifiPage(type: arguments),
  "/wifiPagePerson": (contxt, {arguments}) => WifiPagePerson(type: arguments),
  "/updateUserPage": (contxt) => UpdateUserPage(),
  "/settingPage": (contxt) => SettingPage(),
  "/aboutUsPage": (contxt) => AboutUsPage(),
  "/bodyDevice": (contxt, {arguments}) => BodyDeviceWidget(type: arguments),
  "/deviceTypeList": (contxt) => DeviceTypeListPage(),
  "/deviceDetail": (contxt, {arguments}) => DeviceDetailPage(device: arguments),
  "/instantBodyPage": (contxt, {arguments}) =>
      InstantBodyPage(personInfo: arguments),
  "/qrView": (contxt) => MobileScannerTestPage(),
  "/updatePersonPage": (contxt, {arguments}) =>
      UpdatePersonPage(status: arguments),
  "/deviceSharePage": (contxt, {arguments}) =>
      DeviceSharePage(device: arguments),
  "/deviceShareListPage": (contxt, {arguments}) =>
      DeviceShareListPage(device: arguments),
  "/calibrationPage": (contxt, {arguments}) => CalibrationPage(type: arguments),
  "/calibrationPersonPage": (contxt, {arguments}) =>
      CalibrationPersonPage(type: arguments),
  "/applyRepairPage": (contxt) => ApplyRepairPage(),
  "/languageSetting": (contxt) => LanguageSetting(),
  "/helpPage": (contxt) => HelpPage(),
  "/followPage": (contxt) => FollowPage(),
  "/repairListPage": (contxt) => RepairListPage(),
  "/newSleepReportPage": (contxt, {arguments}) =>
      NewSleepReportPage(data: arguments),
  "/userSchemePage": (contxt) => UserSchemePage(),
  "/privacyPage": (contxt) => PrivacySchemePage(),
  "/repairDetailPage": (contxt, {arguments}) =>
      RepairDetailPage(data: arguments),
  "/applyRepairSuccess": (contxt) => ApplyRepairSuccess(),
  "/messageReviewPage": (contxt, {arguments}) =>
      MessageReviewPage(data: arguments),
  "/messageSettingPage": (contxt, {arguments}) =>
      MessageSettingPage(data: arguments),
  "/commonMessageSettingPage": (contxt) => CommonMessageSettingPage(),
  "/afterCalibrationPersonPage": (contxt) => AfterCalibrationPersonPage(),
  "/afterUpdatePersonPage": (contxt) => AfterUpdatePersonPage(),
  "/afterWifiPagePerson": (contxt, {arguments}) => AfterWifiPagePerson(
        type: arguments,
      ),
};

var onGenerateRoute = (RouteSettings settings) {
  final String? name = settings.name; // 获取路由名称，如 /news 或 /search
  final Function? pageContentBuilder = routes[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") {
        TopSlideNotification.show(
          Get.context!,
          text: "必须登录提示".tr,
          textColor: themeController.currentColor.sc9,
        );

        // 创建新的 RouteSettings，并修改 name 为 /loginPage
        final updatedSettings = RouteSettings(
          name: "/loginPage",
          arguments: settings.arguments,
        );

        // 创建一个新的路由，跳转到登录页面
        return CupertinoPageRoute(
          settings: updatedSettings,
          builder: (context) => pageContentBuilder!(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
};
