import 'dart:async';

import 'package:EasyDartModule/EasyDartModule.dart' as edm;
import 'package:ef/ef.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:flutterflow_ui/flutterflow_ui.dart';
import 'package:vbvs_app/common/color/appConstants.dart';
import 'package:vbvs_app/common/util/CommonVariables.dart';
import 'package:vbvs_app/common/util/FitTool.dart';
import 'package:vbvs_app/common/util/MyUtils.dart';
import 'package:vbvs_app/component/tool/ClickableContainer.dart';
import 'package:vbvs_app/controller/device/blueteeth_bind_controller.dart';
import 'package:vbvs_app/controller/device/device_type_controller.dart';
import 'package:vbvs_app/controller/main_bottom/global_controller.dart';
import 'package:vbvs_app/controller/theme_controller/ThemeController.dart';
import 'package:vbvs_app/controller/user_info_controller.dart';
import 'package:vbvs_app/model/WebSocketMessage.dart';
import 'package:vbvs_app/pages/device/component/DeviceStatusInfoWidget.dart';

class InstantBodyPage extends StatefulWidget {
  var personInfo;
  InstantBodyPage({super.key, required this.personInfo});

  @override
  State<InstantBodyPage> createState() => _InstantBodyPageState();
}

class _InstantBodyPageState extends State<InstantBodyPage> {
  GlobalController globalController = Get.find();
  UserInfoController userInfoController = Get.find();
  BlueteethBindController blueteethBindController = Get.find();
  ThemeController themeController = Get.find();
  DeviceTypeController deviceTypeController = Get.find();

  int maxBodyMotion = 1;
  String breathState = "否";
  String inBed = "离床".tr;
  String onlineState = "离线".tr;
  Timer? _onlineTimer; // 添加 Timer 引用
  int bodyMotion = 0;
  int breathrate = 0;
  String snores = "否".tr;
  int heartrate = 0;

  @override
  void initState() {
    edm.EasyDartModule.websocket.sendData(jsonEncode(WebSocketMessage(
        path: "/vsbs/web/rt/marttress",
        type: 1,
        data: {"mac": widget.personInfo['mac']})));
    _startOnlineTimer(); // 初始化时启动定时器
    super.initState();
  }

  @override
  void dispose() {
    _onlineTimer?.cancel(); // 取消定时器，防止内存泄漏
    edm.EasyDartModule.websocket.sendData(
        jsonEncode(WebSocketMessage(path: "/vsbs/web/rt/marttress", type: 2)));
    super.dispose();
  }

  void _startOnlineTimer() {
    _onlineTimer?.cancel(); // 取消之前的定时器
    _onlineTimer = Timer.periodic(Duration(seconds: 30), (timer) {
      if (mounted) {
        setState(() {
          onlineState = "离线".tr; // 30 秒内没有接收到数据，设置为离线
        });
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    Map device = widget.personInfo;
    CommonVariables.callMap["/vsbs/web/rt/marttress"] = (data) {
      inBed = data["inBed"];
      // 心率 呼吸 体动 呼吸暂停
      if ("离床" == inBed) {
        breathState = "否";
        data["breathRate"] = 0;
        data["heartRate"] = 0;
        data["bodyMotion"] = 0;
      } else {
        breathState = data["breathState"];
        bodyMotion = data['bodyMotion'];
        breathrate = data["breathRate"];
        heartrate = data['heartRate'];
        snores = data['snores'];
      }

      if (mounted) {
        setState(() {
          onlineState = "在线".tr; // 接收到数据，设置为在线
        });
      }

      _startOnlineTimer(); // 重置定时器
    };

    return LayoutBuilder(
      builder: (context, bodySize) => GestureDetector(
        onTap: () => FocusScope.of(context).unfocus(),
        child: Container(
          decoration: BoxDecoration(
            image: DecorationImage(
              image: AssetImage('assets/img/bgNoImg.png'),
              fit: BoxFit.fill,
            ),
          ),
          child: Scaffold(
            backgroundColor: Colors.transparent,
            appBar: AppBar(
              backgroundColor: themeController.currentColor.sc17,
              automaticallyImplyLeading: false,
              iconTheme: IconThemeData(color: themeController.currentColor.sc3),
              titleSpacing: 0.rpx,
              title: Container(
                width: double.infinity,
                height: 180.rpx,
                child: Stack(
                  alignment: Alignment.center,
                  children: [
                    RichText(
                      text: TextSpan(
                        style: FlutterFlowTheme.of(context).bodyMedium.override(
                              fontFamily: 'Readex Pro',
                              color: themeController.currentColor.sc3,
                              fontSize: 30.rpx,
                              letterSpacing: 0.0,
                            ),
                        children: [
                          TextSpan(
                            text: '实时体征.标题'.tr,
                          ),
                          TextSpan(
                            text: "（${onlineState}）",
                            style: TextStyle(
                              color: onlineState == '在线'
                                  ? themeController.currentColor.sc2
                                  : themeController
                                      .currentColor.sc9, // 👈 单独设置颜色
                            ),
                          ),
                        ],
                      ),
                    ),
                    Positioned(
                      left: 0.rpx,
                      child: returnIconButtom,
                    ),
                  ],
                ),
              ),
              actions: [],
              centerTitle: false,
            ),
            body: SafeArea(
              top: true,
              child: Padding(
                padding:
                    EdgeInsetsDirectional.fromSTEB(0.rpx, 29.rpx, 0.rpx, 0.rpx),
                child: SingleChildScrollView(
                  child: Column(
                    mainAxisSize: MainAxisSize.max,
                    children: [
                      Padding(
                        padding: EdgeInsetsDirectional.fromSTEB(
                            30.rpx, 0.rpx, 30.rpx, 120.rpx),
                        child: ClickableContainer(
                          backgroundColor: themeController.currentColor.sc5,
                          highlightColor:
                              themeController.currentColor.sc5, // 或你希望的点击水波纹颜色
                          borderRadius: AppConstants()
                              .normal_container_radius, // 如果你想加圆角可以设置 eg. 12.rpx
                          padding: EdgeInsets.zero,
                          onTap: () {
                            print('点击了体征卡片');
                          },
                          child: Row(
                            mainAxisSize: MainAxisSize.max,
                            children: [
                              Expanded(
                                flex: 1,
                                child: Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    Row(
                                      children: [
                                        Column(
                                          crossAxisAlignment:
                                              CrossAxisAlignment.end,
                                          children: [
                                            Text(
                                              '实时体征.姓名'.tr,
                                              style:
                                                  FlutterFlowTheme.of(context)
                                                      .bodyMedium
                                                      .override(
                                                        fontFamily: 'Inter',
                                                        fontSize: 26.rpx,
                                                        letterSpacing: 0.0,
                                                        color: themeController
                                                            .currentColor.sc4,
                                                      ),
                                            ),
                                            Text(
                                              '实时体征.年龄'.tr,
                                              style:
                                                  FlutterFlowTheme.of(context)
                                                      .bodyMedium
                                                      .override(
                                                        fontFamily: 'Inter',
                                                        fontSize: 26.rpx,
                                                        letterSpacing: 0.0,
                                                        color: themeController
                                                            .currentColor.sc4,
                                                      ),
                                            ),
                                          ].divide(SizedBox(height: 34.rpx)),
                                        ),
                                        Column(
                                          crossAxisAlignment:
                                              CrossAxisAlignment.start,
                                          children: [
                                            Text(
                                              '${device['person']?['name'] ?? '未命名'.tr}',
                                              style:
                                                  FlutterFlowTheme.of(context)
                                                      .bodyMedium
                                                      .override(
                                                        fontFamily: 'Inter',
                                                        fontSize: 26.rpx,
                                                        letterSpacing: 0.0,
                                                        color: themeController
                                                            .currentColor.sc3,
                                                      ),
                                            ),
                                            Text(
                                              '${MyUtils.getAgeByDate(MyUtils.formatBirthdayTime(device['person']?['birthday'])) ?? '未知数据'.tr}',
                                              style:
                                                  FlutterFlowTheme.of(context)
                                                      .bodyMedium
                                                      .override(
                                                        fontFamily: 'Inter',
                                                        fontSize: 26.rpx,
                                                        letterSpacing: 0.0,
                                                        color: themeController
                                                            .currentColor.sc3,
                                                      ),
                                            ),
                                          ].divide(SizedBox(height: 34.rpx)),
                                        ),
                                      ]
                                          .divide(SizedBox(width: 33.rpx))
                                          .addToStart(SizedBox(width: 37.rpx)),
                                    ),
                                  ]
                                      .addToStart(SizedBox(height: 36.rpx))
                                      .addToEnd(SizedBox(height: 36.rpx)),
                                ),
                              ),
                              Expanded(
                                flex: 1,
                                child: Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    Row(
                                      children: [
                                        Column(
                                          crossAxisAlignment:
                                              CrossAxisAlignment.end,
                                          children: [
                                            Text(
                                              '实时体征.设备ID'.tr,
                                              style:
                                                  FlutterFlowTheme.of(context)
                                                      .bodyMedium
                                                      .override(
                                                        fontFamily: 'Inter',
                                                        fontSize: 26.rpx,
                                                        letterSpacing: 0.0,
                                                        color: themeController
                                                            .currentColor.sc4,
                                                      ),
                                            ),
                                            Text(
                                              '实时体征.体重'.tr,
                                              style:
                                                  FlutterFlowTheme.of(context)
                                                      .bodyMedium
                                                      .override(
                                                        fontFamily: 'Inter',
                                                        fontSize: 26.rpx,
                                                        letterSpacing: 0.0,
                                                        color: themeController
                                                            .currentColor.sc4,
                                                      ),
                                            ),
                                          ].divide(SizedBox(height: 34.rpx)),
                                        ),
                                        Column(
                                          crossAxisAlignment:
                                              CrossAxisAlignment.start,
                                          children: [
                                            Text(
                                              '${device['code'] ?? '未知数据'.tr}',
                                              // "D11250300003",
                                              style:
                                                  FlutterFlowTheme.of(context)
                                                      .bodyMedium
                                                      .override(
                                                        fontFamily: 'Inter',
                                                        fontSize: 26.rpx,
                                                        letterSpacing: 0.0,
                                                        color: themeController
                                                            .currentColor.sc3,
                                                      ),
                                            ),
                                            Text(
                                              '${device['person']?['weight'] ?? '未知数据'.tr}kg',
                                              style:
                                                  FlutterFlowTheme.of(context)
                                                      .bodyMedium
                                                      .override(
                                                        fontFamily: 'Inter',
                                                        fontSize: 26.rpx,
                                                        letterSpacing: 0.0,
                                                        color: themeController
                                                            .currentColor.sc3,
                                                      ),
                                            ),
                                          ].divide(SizedBox(height: 34.rpx)),
                                        ),
                                      ]
                                          .divide(SizedBox(width: 33.rpx))
                                          .addToStart(SizedBox(width: 37.rpx)),
                                    ),
                                  ]
                                      .addToStart(SizedBox(height: 36.rpx))
                                      .addToEnd(SizedBox(height: 36.rpx)),
                                ),
                              ),
                            ],
                          ),
                        ),
                      ),
                      Padding(
                        padding: EdgeInsetsDirectional.fromSTEB(
                            66.rpx, 0, 66.rpx, 0),
                        child: Container(
                          decoration: BoxDecoration(
                            image: DecorationImage(
                              image: AssetImage(
                                  'assets/img/body_black.gif'), // 本地图片
                              fit: BoxFit.cover,
                            ),
                          ),
                          child: Column(
                            children: [
                              Row(
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                children: [
                                  DeviceStatusInfoWidget(
                                    title: "在离床".tr,
                                    iconAsset: "assets/img/icon/bed_status.svg",
                                    value: inBed,
                                  ),
                                  DeviceStatusInfoWidget(
                                    title: "体动".tr,
                                    iconAsset: "assets/img/icon/bodymotion.svg",
                                    value: "${bodyMotion}" ?? "未知数据".tr,
                                  ),
                                ],
                              ),
                              Row(
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                children: [
                                  DeviceStatusInfoWidget(
                                    title: "心率".tr,
                                    iconAsset: "assets/img/icon/heart.svg",
                                    value: "${heartrate}",
                                  ),
                                  DeviceStatusInfoWidget(
                                    title: "打鼾".tr,
                                    iconAsset: "assets/img/icon/snore.svg",
                                    value: '${snores}'.tr,
                                  ),
                                ],
                              ),
                              Row(
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                children: [
                                  DeviceStatusInfoWidget(
                                    title: "呼吸".tr,
                                    iconAsset: "assets/img/icon/breathe.svg",
                                    value: '${breathrate}',
                                  ),
                                  DeviceStatusInfoWidget(
                                    title: "呼吸暂停".tr,
                                    iconAsset:
                                        "assets/img/icon/breathe_pause.svg",
                                    value: '${breathState}',
                                  ),
                                ],
                              ),
                            ].divide(SizedBox(height: 49.rpx)),
                          ),
                        ),
                      ),
                      Padding(
                        padding: EdgeInsetsDirectional.fromSTEB(
                            0.rpx, 67.rpx, 0.rpx, 0.rpx),
                        child: Container(
                          height: 40.rpx,
                          child: Text(
                            bodyMotion >= maxBodyMotion ? '请保持静止'.tr : "",
                            style: FlutterFlowTheme.of(context)
                                .bodyMedium
                                .override(
                                  fontFamily: 'Inter',
                                  fontSize: 26.rpx,
                                  letterSpacing: 0.0,
                                  color: themeController.currentColor.sc9,
                                ),
                          ),
                        ),
                      ),
                      SizedBox(
                        height: 207.rpx,
                      ),
                      ClickableContainer(
                        backgroundColor: Colors.transparent, // 可自定义背景色
                        highlightColor: Colors.white, // 点击涟漪颜色
                        borderRadius: 16.rpx, // 圆角大小，可按需调整
                        padding: EdgeInsetsDirectional.fromSTEB(
                            30.rpx, 0.rpx, 30.rpx, 0.rpx),
                        onTap: () {},
                        child: Container(
                          padding: EdgeInsetsDirectional.fromSTEB(
                              26.rpx, 26.rpx, 26.rpx, 26.rpx),
                          decoration: BoxDecoration(
                            // color: FlutterFlowTheme.of(context)
                            //     .primaryBackground
                            //     .withOpacity(0.6), // 半透明背景
                            borderRadius: BorderRadius.circular(16.rpx),
                            border: Border.all(
                              // 设置边框颜色和宽度
                              color: themeController.currentColor.sc4, // 边框颜色
                              width: 2.rpx, // 边框宽度
                            ),
                          ),
                          child: Row(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Padding(
                                padding: EdgeInsetsDirectional.fromSTEB(
                                    0, 8.rpx, 0, 0),
                                child: Container(
                                  width: 23.rpx,
                                  height: 23.rpx,
                                  // width: double.infinity,
                                  decoration: BoxDecoration(),
                                  child: SvgPicture.asset(
                                    'assets/img/icon/tips.svg',
                                    fit: BoxFit.cover,
                                    color: themeController.currentColor.sc3,
                                  ),
                                ),
                              ),
                              Expanded(
                                child: Text(
                                  '实时体征.提示'.tr,
                                  style: FlutterFlowTheme.of(context)
                                      .bodyMedium
                                      .override(
                                        fontFamily: 'Inter',
                                        letterSpacing: 0.0,
                                        color: themeController.currentColor.sc4,
                                      ),
                                ),
                              ),
                            ].divide(SizedBox(width: 23.rpx)),
                          ),
                        ),
                      ),
                      SizedBox(
                        height: 26.rpx,
                      ),
                    ],
                  ),
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }
}
