import 'dart:async';
import 'dart:convert';

import 'package:EasyDartModule/EasyDartModule.dart';
import 'package:EasyDartModule/base/logger/Logger.dart';
import 'package:EasyDartModule/base/websocket/WebSocket.dart';
import 'package:ef/ef.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:fluwx/fluwx.dart';
import 'package:get_storage/get_storage.dart';
import 'package:localstorage/localstorage.dart';
import 'package:syncfusion_localizations/syncfusion_localizations.dart';
import 'package:vbvs_app/common/color/ServiceConstant.dart';
import 'package:vbvs_app/common/util/CheckNetwork.dart';
import 'package:vbvs_app/common/util/CommonVariables.dart';
import 'package:vbvs_app/common/util/Dio.dart';
import 'package:vbvs_app/common/util/FitTool.dart';
import 'package:vbvs_app/common/util/requestWithLog.dart';
import 'package:vbvs_app/controller/date/CalendarController.dart';
import 'package:vbvs_app/controller/device/blueteeth_bind_controller.dart';
import 'package:vbvs_app/controller/device/body_device_controller.dart';
import 'package:vbvs_app/controller/device/device_calibration_controller.dart';
import 'package:vbvs_app/controller/device/device_share_controller.dart';
import 'package:vbvs_app/controller/device/device_share_list_controller.dart';
import 'package:vbvs_app/controller/device/device_type_controller.dart';
import 'package:vbvs_app/controller/home/home_controller.dart';
import 'package:vbvs_app/controller/login/login_controller.dart';
import 'package:vbvs_app/controller/main_bottom/global_controller.dart';
import 'package:vbvs_app/controller/main_bottom/main_page_controller.dart';
import 'package:vbvs_app/controller/message/common_message_setting_controller.dart';
import 'package:vbvs_app/controller/message/message_controller.dart';
import 'package:vbvs_app/controller/message/message_review_controller.dart';
import 'package:vbvs_app/controller/message/message_setting_controller.dart';
import 'package:vbvs_app/controller/mh_controller/address_controller.dart';
import 'package:vbvs_app/controller/mh_controller/address_list_controller.dart';
import 'package:vbvs_app/controller/mh_controller/bedController.dart';
import 'package:vbvs_app/controller/mh_controller/book_info_controller.dart';
import 'package:vbvs_app/controller/mh_controller/device_list_controller.dart';
import 'package:vbvs_app/controller/mh_controller/experience_store_list_page.dart';
import 'package:vbvs_app/controller/mh_controller/find_password_controller.dart';
import 'package:vbvs_app/controller/mh_controller/issue_controller.dart';
import 'package:vbvs_app/controller/mh_controller/issue_preview_controller.dart';
import 'package:vbvs_app/controller/mh_controller/message_controller.dart';
import 'package:vbvs_app/controller/mh_controller/mh_language_controller.dart';
import 'package:vbvs_app/controller/mh_controller/mhdevice_share_controller.dart';
import 'package:vbvs_app/controller/mh_controller/my_experience_list_controller.dart';
import 'package:vbvs_app/controller/mh_controller/people_info_controller.dart';
import 'package:vbvs_app/controller/mh_controller/register_controller.dart';
import 'package:vbvs_app/controller/mh_controller/repair_info_controller.dart';
import 'package:vbvs_app/controller/mh_controller/repair_list_controller.dart';
import 'package:vbvs_app/controller/mh_controller/score_controller.dart';
import 'package:vbvs_app/controller/mh_controller/sleeping_habit_controller.dart';
import 'package:vbvs_app/controller/person/person_controller.dart';
import 'package:vbvs_app/controller/repair/repair_controller.dart';
import 'package:vbvs_app/controller/setting/language/language_controller.dart';
import 'package:vbvs_app/controller/setting/pdf/PrivacyPdfController.dart';
import 'package:vbvs_app/controller/setting/pdf/UserPdfController.dart';
import 'package:vbvs_app/controller/sleep/sleep_report_controller.dart';
import 'package:vbvs_app/controller/theme_controller/ThemeController.dart';
import 'package:vbvs_app/controller/time/countdown_controller.dart';
import 'package:vbvs_app/controller/weather/weather_controller.dart';
import 'package:vbvs_app/language/AppLanguage.dart';
import 'package:vbvs_app/model/CustomThemeColor.dart';
import 'package:vbvs_app/model/user_data.dart';
import 'package:vbvs_app/pages/mh_page/MattressControl.dart';
import 'package:vbvs_app/pages/mh_page/device/component/mht_device_calibration_controller.dart';
import 'package:vbvs_app/pages/mh_page/device/controller/mht_bluetooth_controller.dart';
import 'package:vbvs_app/pages/mh_page/homepage/controller/mht_home_controller.dart';
import 'package:vbvs_app/pages/mh_page/test/WebviewTestModel.dart';
import 'package:vbvs_app/pages/mh_page/user/controller/mht_login_controller.dart';
import 'package:vbvs_app/pages/mh_page/user/controller/mht_register_controller.dart';
import 'package:vbvs_app/pages/mh_page/user/controller/update_password_controller.dart';
import 'package:vbvs_app/routers/mh_routers.dart';

import 'controller/mh_controller/apply_repair_controller.dart';
import 'controller/user_info_controller.dart';
import 'routers/routers.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  final Locale? deviceLocale = Get.deviceLocale;
  print('系统语言: ${deviceLocale?.languageCode}');

  // final selectapp = "th";
  final selectapp = "mht";
  // await AppLanguage().loadLanguage("en_US");
  await AppLanguage().loadLanguage("zh_CN", project: selectapp);
  await initLanguageSetting();
  WidgetsFlutterBinding.ensureInitialized();

  ApiService.init();
  await GetStorage.init();
  // 初始化登录
  await initLocalStorage();
  initEasyDartModule();
  await initLogin();
  await messageStatus();

  startMessagePolling();
  // 检查网络
  Checknetwork.checkNetwork();
  // 微信开放平台注册
  initWX();
  // // 初始化 flutter_xupdate android app 更新
  // initXUpdate();

  WidgetsFlutterBinding.ensureInitialized();
  SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp])
      .then((_) {
    runApp(MyApp());
  });
}

initLanguageSetting() {
  try {
    Get.put(LanguageController());
    LanguageController languageController = Get.find();
    languageController.initLanuageList();
  } catch (e) {
    print(e);
  }
}

// 初始化微信开放平台注册
Future<void> initWX() async {
  Fluwx fluwx = Fluwx();
  fluwx.registerApi(
      //请填写自己的微信appid
      appId: "wxeb2688220799e2c5",
      doOnAndroid: true,
      doOnIOS: true,
      universalLink: "https://app.he-info.com/theh/");
}

Timer? _messageTimer;
void startMessagePolling() {
  // 避免重复启动
  _messageTimer?.cancel();

  _messageTimer = Timer.periodic(Duration(seconds: 10), (timer) async {
    try {
      MessageController messageController = Get.find();
      messageController.getMessageStatus();
      // print("轮询消息状态成功");
    } catch (e) {
      print("轮询消息状态失败: $e");
    }
  });
}

messageStatus() async {
  Get.put(MessageController());
  MessageController messageController = Get.find();
  await messageController.getMessageStatus();
}

void initEasyDartModule() {
  try {
    EasyDartModule.init(
        loggerConfig:
            LoggerConfig(host: ServiceConstant.logService, serviceName: "web"),
        webSocketConfig:
            WebSocketConfig(ServiceConstant.webSocketService, (data) {
          // 接收到服务消息
          var json = jsonDecode(data);
          if (json['code'] != null && json['code'] != 200) {
            EasyDartModule.logger.error("websocket连接失败--》" + json);
          }
          if (json["path"] != null) {
            var call = CommonVariables.callMap[json["path"]];
            if (call != null) {
              try {
                call(json["data"]);
              } catch (e) {
                print(e);
              }
            } else {
              print("未找到当前路径: ${json["path"]} 回调函数");
            }
          }
          // print(data);
        }, onOpen: () {
          // 连接建立完毕
          // EasyDartModule.websocket
          //     .sendData(jsonEncode({"path": "/aa/bb", "type": 1}));
          print("object");
        }));
  } catch (e) {
    print(e);
  }
  //初始化
}

Future<void> initLogin() async {
  // 初始化控制器
  Get.put(UserInfoController());
  // Get.put(SettingController());

  // 获取设备信息,需要用户点击确认隐私协议与用户协议选择框时才能获取
  // Deviceconfig.initPlatformState();
  // 获取app版本号
  // SettingController settingController = Get.find();
  // settingController.model.appVersion = await Deviceconfig.getAppVersion();

  final box = GetStorage();
  UserInfoController userInfoController = Get.find();
  String? token = box.read('token');
  try {
    Map<String, dynamic>? userMap = box.read('user');
    if (userMap != null) {
      UserModel user = UserModel.fromJson(userMap);
      userInfoController.model.user = user;
    }
  } catch (e) {
    print(e);
  }

  if (token != null) {
    userInfoController.model.login = 1;
    // 根据token去请求
    // await userInfoController.autoLogin(token);
    EasyDartModule.dio.token = token;
  } else {
    // 如果没有 token，则将用户标记为未登录
    userInfoController.model.login = 0;
  }
}

//处理绑定未完成的数据
Future<void> dealBindProcess() async {
  UserInfoController userInfoController = Get.find();
  String serviceAddress = ServiceConstant.service_address;
  String serviceName = ServiceConstant.server_service;
  String serviceApi = ServiceConstant.device_list;
  String queryUrl = "${serviceAddress}${serviceName}${serviceApi}?bindType=1";
  userInfoController.device_bind_status = [];

  try {
    final res = await requestWithLog(
      logTitle: "查询设备绑定列表",
      method: MyHttpMethod.get,
      queryUrl: queryUrl,
    );

    List data = res.data ?? [];
    if (data.isNotEmpty) {
      // 使用 Future.wait 等待所有设备的查询完成
      await Future.wait(
        data.map((element) async {
          String mac = element['mac'];
          String type = "device_bind_status_$mac";
          String queryUrl =
              "${serviceAddress}${serviceName}${ServiceConstant.user_setting}?type=${type}";

          try {
            final res = await requestWithLog(
              logTitle: "查询绑定流程",
              method: MyHttpMethod.get,
              queryUrl: queryUrl,
            );

            if (res.data != null && res.data.isNotEmpty) {
              bool celibration = res.data['celibration'];
              bool person_info = res.data['person_info'];
              bool wifi = res.data['wifi'];
              if (!celibration || !person_info || !wifi) {
                res.data['device_info'] = element;
                userInfoController.device_bind_status.add(res.data);
              }
            }
          } catch (e) {
            print("查询绑定流程失败: $e");
          }
        }),
      );
    }
  } catch (e) {
    print("查询设备绑定列表失败: $e");
  }
}

final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();

class MyApp extends StatelessWidget {
  MyApp({super.key});
  final ThemeController themeController = Get.put(ThemeController());

  Widget buildmht(BuildContext context, BoxConstraints cons) {
    return GetMaterialApp(
        navigatorKey: navigatorKey, // ✅ 注入 navigatorKey
        translations: AppLanguage(),
        debugShowCheckedModeBanner: false,
        title: '',
        theme: themeController.currentTheme,
        localizationsDelegates: const [
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate,
          GlobalCupertinoLocalizations.delegate,
        ],
        locale: Locale('zh', 'CN'), // 设置默认语言为中文

        // home: MainPageBBottomChange(),
        // initialRoute: "/loginPage",
        initialRoute: "/mianPageBottomChange",
        onGenerateRoute: mhonGenerateRoute,
        initialBinding: BindingsBuilder(() => [
              // Get.lazyPut(() => UserInfoController()),
              Get.put(GlobalController()),
              Get.lazyPut(() => ControlCardController()),
              Get.lazyPut(() => RepairListController()),
              Get.lazyPut(() => RepairInfoController()),
              Get.lazyPut(() => UserInfoController()),
              Get.lazyPut(() => MhMessageController()),
              Get.lazyPut(() => ScoreController()),
              Get.lazyPut(() => ExperienceStoreListController()),
              Get.lazyPut(() => BookExperienceListController()),
              Get.lazyPut(() => IssuePreviewInfoController()),
              Get.lazyPut(() => IssueListController()),
              Get.lazyPut(() => ApplyRepairController()),
              Get.lazyPut(() => RegisterController()),
              Get.lazyPut(() => FindPasswordController()),
              Get.lazyPut(() => SleepingHabitController()),
              Get.lazyPut(() => PeopleInfoController()),
              Get.lazyPut(() => MainPageController()),
              Get.lazyPut(() => MHLanguageController()),
              Get.lazyPut(() => AddressListController()),
              Get.lazyPut(() => AddressController()),
              Get.lazyPut(() => MHLanguageController()),
              Get.lazyPut(() => BlueteethBindController()),
              Get.lazyPut(() => BookInfoController()),
              Get.lazyPut(() => PersonController()),
              Get.lazyPut(() => CountdownController()),
              Get.lazyPut(() => LoginController()),
              Get.lazyPut(() => DeviceTypeController()),
              Get.lazyPut(() => BodyDeviceController()),
              Get.lazyPut(() => HomeController()),
              Get.lazyPut(() => MHDeviceShareController()),
              Get.lazyPut(() => DeviceShareController()),
              Get.lazyPut(() => DeviceShareListController()),
              Get.lazyPut(() => DeviceCalibrationController()),
              Get.lazyPut(() => RepairController()),
              Get.lazyPut(() => MHTLoginController()),
              Get.lazyPut(() => MHTRegisterController()),
              Get.lazyPut(() => WeatherModelController()),
              Get.lazyPut(() => RegisterController()),
              Get.lazyPut(() => SleepingHabitController()),
              Get.lazyPut(() => BedController()),
              Get.lazyPut(() => UpdatePasswordController()),
              Get.lazyPut(() => IssueListController()),
              Get.lazyPut(() => DeviceListController()),
              Get.lazyPut(() => MHTBlueToothController()),
              Get.lazyPut(() => MHTHomeController()),
              Get.lazyPut(() => MHTDeviceCalibrationController()),
              Get.lazyPut(() => SleepReportController()),
              Get.lazyPut(() => CalendarController()),
              Get.lazyPut(() => UserPdfController()),
              Get.lazyPut(() => PrivacyPdfController()),
              Get.put(WebviewTestController()),
            ]));
  }

  Widget buildth(BuildContext context, BoxConstraints cons) {
    return GetMaterialApp(
        translations: AppLanguage(),
        // locale: const Locale("zh", "CN"),
        locale: AppLanguage().currentLocale, // ✅ 动态读取当前语言
        fallbackLocale: const Locale("zh", "CN"),
        localizationsDelegates: [
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate,
          GlobalCupertinoLocalizations.delegate,
          SfGlobalLocalizations.delegate,
        ],
        supportedLocales: [
          const Locale('zh', 'CN'), // 中文
          const Locale('en', 'US'), // ⚠️ 添加你支持的语言
        ],
        debugShowCheckedModeBanner: false,
        title: '',
        theme: themeController.currentTheme,
        // home: const MyHomePage(title: '智慧眠花糖 Home Page'),
        initialRoute: "/mianPageBottomChange",
        onGenerateRoute: onGenerateRoute,
        initialBinding: BindingsBuilder(() => [
              // Get.lazyPut(() => UserInfoController()),
              Get.put(GlobalController()),
              Get.put(WeatherModelController()),
              Get.lazyPut(() => MainPageController()),
              Get.lazyPut(() => BlueteethBindController()),
              Get.lazyPut(() => PersonController()),
              Get.lazyPut(() => CountdownController()),
              Get.lazyPut(() => LoginController()),
              Get.lazyPut(() => DeviceTypeController()),
              Get.lazyPut(() => BodyDeviceController()),
              Get.lazyPut(() => HomeController()),
              Get.lazyPut(() => DeviceShareController()),
              Get.lazyPut(() => DeviceShareListController()),
              Get.lazyPut(() => DeviceCalibrationController()),
              Get.lazyPut(() => RepairController()),
              Get.lazyPut(() => UserPdfController()),
              Get.lazyPut(() => PrivacyPdfController()),
              Get.lazyPut(() => CalendarController()),
              Get.lazyPut(() => SleepReportController()),
              Get.lazyPut(() => MessageReviewController()),
              Get.lazyPut(() => MessageSettingController()),
              Get.lazyPut(() => CommonMessageSettingController()),
            ]));
  }

  // final selectapp = "th";
  final selectapp = "mht";
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    var co = MediaQuery.sizeOf(context);
    FitTool.init(co.height);
    themeController.changeTheme(CustomThemeColor.dark);
    return LayoutBuilder(builder: (contxt, cons) {
      switch (selectapp) {
        case "th":
          return buildth(context, cons);
        case "mht":
          return buildmht(contxt, cons);
        default:
          return buildth(context, cons);
      }
    });
  }
}
