import 'package:ef/ef.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:flutterflow_ui/flutterflow_ui.dart';

import 'package:vbvs_app/common/color/appFontsize.dart';
import 'package:vbvs_app/common/util/DailyLogUtils.dart';
import 'package:vbvs_app/common/util/FitTool.dart';
import 'package:vbvs_app/common/util/MyUtils.dart';
import 'package:vbvs_app/component/tool/TopSlideNotification.dart';
import 'package:vbvs_app/controller/main_bottom/global_controller.dart';
import 'package:vbvs_app/controller/mh_controller/device_list_controller.dart';
import 'package:vbvs_app/pages/main_bottom/component/main_page_b_bottom_change.dart';
import 'package:vbvs_app/pages/mh_page/test/WebviewTestModel.dart';

class DeviceInfoWidget extends GetView {
  int index;
  DeviceListController deviceListController;

  DeviceInfoWidget({required this.index, required this.deviceListController}) {}

  @override
  Widget build(BuildContext context) {
    var device = deviceListController.model.deviceList[index];
    GlobalController globalController = Get.find();
    return Padding(
      padding: EdgeInsetsDirectional.fromSTEB(30.rpx, 0, 30.rpx, 0),
      child: Container(
        // width: MediaQuery.sizeOf(context).width,
        height: 381.rpx,
        // constraints: BoxConstraints(
        //   minHeight: 140,
        // ),
        decoration: BoxDecoration(
          color: Color(0xFF003058),
          borderRadius: BorderRadius.circular(16.rpx),
        ),
        child: Padding(
          padding: EdgeInsetsDirectional.fromSTEB(26.rpx, 33.rpx, 26.rpx, 0),
          child: Container(
            // width: MediaQuery.sizeOf(context).width,
            height: 381.rpx,
            // constraints: BoxConstraints(
            //   minHeight: 106,
            // ),

            child: Container(
              // width: MediaQuery.sizeOf(context).width,
              height: 381.rpx,
              constraints: BoxConstraints(
                minHeight: 130,
              ),

              child: Column(
                mainAxisSize: MainAxisSize.max,
                mainAxisAlignment: MainAxisAlignment.start,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    deviceListController.model.deviceList[index]['name'] ??
                        'SWES系列 01号智能一键入眠床',
                    style: TextStyle(
                      fontFamily: 'Readex Pro',
                      color: Colors.white,
                      fontSize: 30.rpx,
                      letterSpacing: 0,
                    ),
                    maxLines: 1, // 限制显示一行
                    overflow: TextOverflow.ellipsis, // 超出部分显示省略号
                  ),
                  Container(
                    margin: EdgeInsets.only(top: 36.rpx),
                    width: MediaQuery.sizeOf(context).width,
                    // height: MediaQuery.sizeOf(context).height * 0.065,
                    constraints: BoxConstraints(
                      minHeight: 55,
                    ),

                    child: Row(
                      mainAxisSize: MainAxisSize.max,
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Expanded(
                          child: InkWell(
                            onTap: () async {
                              // GlobalController globalController = Get.find();
                              // var tmp = globalController.model.deviceMain;
                              // BedController bedController = Get.find();
                              // bedController.websocketSend(2);
                              // globalController.model.deviceMain = device;
                              // globalController.updateAll();
                              // bedController.websocketSend(1);
                              // var result = await Get.toNamed("/bodyChart");
                              // bedController.websocketSend(2);
                              // globalController.model.deviceMain = tmp;
                            },
                            child: Container(
                              child: Column(
                                mainAxisSize: MainAxisSize.max,
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  _buildInfoRow(
                                      '设备ID',
                                      deviceListController
                                              .model.deviceList[index]["_id"] ??
                                          '',
                                      valueColor: (deviceListController
                                                      .model.deviceList[index]
                                                  ["status"]?["status"]) ==
                                              1
                                          ? Color(0xFF929699)
                                          : null),
                                  _buildInfoRow(
                                      '房间',
                                      deviceListController.model
                                              .deviceList[index]["roomName"] ??
                                          '',
                                      valueColor: (deviceListController
                                                      .model.deviceList[index]
                                                  ["status"]?["status"]) ==
                                              1
                                          ? Color(0xFF929699)
                                          : null),
                                  _buildInfoRow(
                                      '设备状态',
                                      (deviceListController
                                                      .model.deviceList[index]
                                                  ["status"]?["status"]) ==
                                              1
                                          ? '在线'
                                          : '离线',
                                      valueColor: (deviceListController
                                                      .model.deviceList[index]
                                                  ["status"]?["status"]) ==
                                              1
                                          ? Color(0xFF6BFDAC)
                                          : Color(0xFFFF7159)),
                                  // Row(
                                  //   mainAxisSize: MainAxisSize.max,
                                  //   children: [
                                  //     Expanded(
                                  //       child: RichText(
                                  //         text: TextSpan(
                                  //           children: [
                                  //             TextSpan(
                                  //               text: '设备ID：' +
                                  //                   (deviceListController.model
                                  //                               .deviceList[
                                  //                           index]['_id'] ??
                                  //                       ''),
                                  //               style: TextStyle(
                                  //                   fontFamily: 'Readex Pro',
                                  //                   color: Color(0xFFC8CBD2),
                                  //                   fontSize: 26.rpx,
                                  //                   letterSpacing: 0,
                                  //                   height: 1),
                                  //             ),
                                  //           ],
                                  //         ),
                                  //         maxLines: 1, // 限制显示一行
                                  //         overflow: TextOverflow
                                  //             .ellipsis, // 超出部分显示省略号
                                  //       ),
                                  //     ),
                                  //   ],
                                  // ),
                                  // Align(
                                  //   alignment: AlignmentDirectional(-1, 0),
                                  //   child: Text(
                                  //     '房间：' +
                                  //         (deviceListController
                                  //                     .model.deviceList[index]
                                  //                 ['roomName'] ??
                                  //             ''),
                                  //     style: TextStyle(
                                  //         fontFamily: 'Readex Pro',
                                  //         color: Color(0xFFC8CBD2),
                                  //         fontSize: 26.rpx,
                                  //         letterSpacing: 0,
                                  //         height: 1),
                                  //   ),
                                  // ),
                                  // Align(
                                  //   alignment: AlignmentDirectional(-1, 0),
                                  //   child: Text(
                                  //     '设备状态：${(deviceListController.model.deviceList[index]["status"]?["status"]) == 1 ? '在线' : '离线'}',
                                  //     style: TextStyle(
                                  //         fontFamily: 'Readex Pro',
                                  //         color: Color(0xFFC8CBD2),
                                  //         fontSize: 26.rpx,
                                  //         letterSpacing: 0,
                                  //         height: 1),
                                  //   ),
                                  // )
                                ].divide(SizedBox(height: 36.rpx)),
                              ),
                            ),
                          ),
                        ),
                        Align(
                          alignment: AlignmentDirectional(0, 0),
                          child: Container(
                            width: 150.rpx,
                            height: 90.rpx,
                            child: FFButtonWidget(
                              onPressed: () async {
                                try {
                                  WebviewTestController webviewTestController =
                                      Get.find();
                                  var future = webviewTestController
                                      .web.jsbridge?.dart
                                      .appToHtmlDevice(device);
                                  Future.delayed(Duration(seconds: 5), () {
                                    return;
                                  });
                                  await future;
                                  await webviewTestController.web.jsbridge?.dart
                                      .pageActive(false);
                                  MainPageBBottomChange.jumpTo(2);
                                  Get.until((route) =>
                                      Get.currentRoute ==
                                      "/mianPageBottomChange");
                                } catch (e) {
                                  DailyLogUtils.writeError("发生异常: $e");
                                }
                              },
                              text: '控制',
                              options: FFButtonOptions(
                                color: Color(0XFF85F5FF),
                                textStyle: TextStyle(
                                  fontFamily: 'Readex Pro',
                                  color: Color(0xFF011D33),
                                  fontSize: 28.rpx,
                                  letterSpacing: 0,
                                ),
                                elevation: 0,
                                borderSide: BorderSide(
                                  color: Colors.transparent,
                                  width: 1,
                                ),
                                borderRadius: BorderRadius.circular(16.rpx),
                              ),
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                  Padding(
                    padding: EdgeInsetsDirectional.fromSTEB(0, 36.rpx, 0, 0),
                    child: Container(
                      width: MediaQuery.sizeOf(context).width,
                      height: 0.5.rpx,
                      decoration: BoxDecoration(
                        color: Color(0xFF929699),
                      ),
                    ),
                  ),
                  Flexible(
                    child: Container(
                      margin: EdgeInsets.only(right: 26.rpx),
                      width: MediaQuery.sizeOf(context).width,
                      height: MediaQuery.sizeOf(context).height * 1,
                      constraints: BoxConstraints(
                        minHeight: 60,
                      ),
                      child: Row(
                        mainAxisSize: MainAxisSize.max,
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: [
                          InkWell(
                            onTap: () async {
                              await Get.toNamed("/deviceSharePage",
                                  arguments: deviceListController
                                      .model.deviceList[index]);
                              // if (device['type'] == 1) {
                              //   globalController.model.deviceMain = device;
                              //   await Get.toNamed("/deviceSharePage");
                              //   globalController.getDeviceList();
                              // }
                            },
                            child: Container(
                              height: MediaQuery.sizeOf(context).height * 0.1,
                              child: Row(
                                mainAxisSize: MainAxisSize.max,
                                mainAxisAlignment: MainAxisAlignment.start,
                                children: [
                                  Align(
                                      alignment: AlignmentDirectional(1, 0),
                                      child: Container(
                                        width: 42.rpx,
                                        height: 42.rpx,
                                        child: SvgPicture.asset(
                                          'assets/images/group.svg',
                                          color: Color(0xFF85F5FF),
                                        ),
                                      )),
                                  Row(
                                    mainAxisSize: MainAxisSize.max,
                                    mainAxisAlignment: MainAxisAlignment.start,
                                    children: [
                                      if (device['bind_type'] == 1)
                                        Text(
                                          '已分享：',
                                          style: TextStyle(
                                            fontFamily: 'Readex Pro',
                                            fontSize:
                                                AppFontsize.normal_text_size,
                                            letterSpacing: 0,
                                            color: Colors.white,
                                          ),
                                        ),
                                      if (device['bind_type'] == 1)
                                        Padding(
                                          padding:
                                              EdgeInsetsDirectional.fromSTEB(
                                                  5, 0, 5, 0),
                                          child: Text(
                                            (deviceListController.model
                                                            .deviceList[index]
                                                        ['shareNum'] ??
                                                    0)
                                                .toString(),
                                            style: TextStyle(
                                              fontFamily: 'Readex Pro',
                                              color: Color(0xFF85F5FF),
                                              fontSize:
                                                  AppFontsize.normal_text_size,
                                              letterSpacing: 0,
                                            ),
                                          ),
                                        ),
                                      if (device['bind_type'] == 1)
                                        Text(
                                          '人',
                                          style: TextStyle(
                                            fontFamily: 'Readex Pro',
                                            fontSize:
                                                AppFontsize.normal_text_size,
                                            letterSpacing: 0,
                                            color: Colors.white,
                                          ),
                                        ),
                                      if (device['bind_type'] == 2)
                                        Text(
                                          '来自',
                                          style: TextStyle(
                                            fontFamily: 'Readex Pro',
                                            fontSize:
                                                AppFontsize.normal_text_size,
                                            letterSpacing: 0,
                                            color: Colors.white,
                                          ),
                                        ),
                                      if (device['bind_type'] == 2)
                                        Padding(
                                          padding:
                                              EdgeInsetsDirectional.fromSTEB(
                                                  5, 0, 5, 0),
                                          child: Text(
                                            '${device['suname']?.length > 5 ? device['suname'].substring(0, 5) + '...' : device['suname']}',
                                            style: TextStyle(
                                              fontFamily: 'Readex Pro',
                                              color: Color(0xFF85F5FF),
                                              fontSize:
                                                  AppFontsize.normal_text_size,
                                              letterSpacing: 0,
                                            ),
                                          ),
                                        ),
                                      if (device['bind_type'] == 2)
                                        Text(
                                          '的分享',
                                          style: TextStyle(
                                            fontFamily: 'Readex Pro',
                                            fontSize: 21.rpx,
                                            letterSpacing: 0,
                                            color: Colors.white,
                                          ),
                                        ),
                                    ],
                                  ),
                                ].divide(SizedBox(width: 6)),
                              ),
                            ),
                          ),
                          InkWell(
                            onTap: () {
                              TopSlideNotification.show(
                                context,
                                text: "功能开发中...",
                              );
                              // var a = [device["bindMacA"]];
                              // if ("${device["bindMacB"]}".length > 6) {
                              //   a.add(device["bindMacB"]);
                              // }
                              // Get.toNamed("/sleepWebview", arguments: a);
                            },
                            child: Container(
                              height: MediaQuery.sizeOf(context).height * 0.1,
                              constraints: BoxConstraints(
                                minHeight: 25,
                              ),
                              alignment: Alignment.center,
                              child: Row(
                                mainAxisSize: MainAxisSize.max,
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                children: [
                                  Align(
                                    alignment: AlignmentDirectional(0, 0),
                                    child: SvgPicture.asset(
                                      "assets/images/table.svg",
                                      width: 24.rpx,
                                      height: 24.rpx,
                                      color: Color(0xFF85F5FF),
                                    ),
                                  ),
                                  SizedBox(width: 5),
                                  Text(
                                    '睡眠报告',
                                    style: TextStyle(
                                      fontFamily: 'Readex Pro',
                                      color: Colors.white,
                                      fontSize: 21.rpx,
                                      letterSpacing: 0,
                                    ),
                                  ),
                                ],
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }

  Widget _buildInfoRow(String label, String value, {Color? valueColor}) {
    return Row(
      mainAxisSize: MainAxisSize.min,
      children: [
        Text(
          '$label  ',
          style:
              TextStyle(color: Color(0XFF929699), fontSize: 26.rpx, height: 1),
        ),
        Flexible(
          child: Text(
            value,
            style: TextStyle(
                color: valueColor ?? Colors.white, fontSize: 26.rpx, height: 1),
            overflow: TextOverflow.ellipsis,
          ),
        ),
      ],
    );
  }
}
