import 'package:ef/ef.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:vbvs_app/common/color/appConstants.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/pages/device_bind/componnet/bind_dialog.dart';
import 'package:vbvs_app/pages/sleep_report/chart/DataShowWidget.dart';
import 'package:EasyDartModule/EasyDartModule.dart' as es;

class HeartChangeWidget extends StatefulWidget {
  var sleepReport;
  HeartChangeWidget({super.key, required this.sleepReport});

  @override
  State<HeartChangeWidget> createState() => _HeartChangeWidgetState();
}

class _HeartChangeWidgetState extends State<HeartChangeWidget> {
  @override
  void setState(VoidCallback callback) {
    super.setState(callback);
  }

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    try {
      if (widget.sleepReport == null ||
          widget.sleepReport['hrvs'] == null ||
          widget.sleepReport['hrvs'].isEmpty) {
        return Container();
      }

      List dataList = widget.sleepReport['hrvs'];

      List<Map<String, dynamic>> data = transformHrvData(dataList);
      return Container(
        width: double.infinity,
        decoration: BoxDecoration(
          color: themeController.currentColor.sc5,
          borderRadius: BorderRadius.circular(
              AppConstants().normal_container_radius), // 你可以按需调整圆角半径
        ),
        child: Padding(
          padding:
              EdgeInsetsDirectional.fromSTEB(26.rpx, 29.rpx, 26.rpx, 45.rpx),
          child: Column(
            mainAxisSize: MainAxisSize.max,
            children: [
              Container(
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text(
                      "心率变异性（HRV）".tr,
                      style: TextStyle(
                          color: themeController.currentColor.sc3,
                          fontSize: AppConstants().title_text_fontSize),
                    ),
                    ClickableContainer(
                      backgroundColor: Colors.transparent,
                      highlightColor: Colors.white, // 或设置为你需要的水波纹颜色
                      padding: EdgeInsetsDirectional.fromSTEB(
                          14.rpx, 0.rpx, 14.rpx, 0), //
                      borderRadius: 0.rpx, // 圆形点击区域
                      onTap: () {
                        showTipDialog(
                          context,
                          Container(
                            child: Text(
                              "心率变异性（HRV）介绍".tr,
                              style: TextStyle(
                                fontSize: 26.rpx,
                                color: themeController.currentColor.sc3,
                              ),
                            ),
                          ),
                        );
                      },
                      child: Container(
                        padding: EdgeInsetsDirectional.fromSTEB(
                            0, 0.rpx, 0.rpx, 0), // 外部 padding 移到内部
                        width: 28.rpx,
                        height: 28.rpx,
                        child: SvgPicture.asset(
                          'assets/img/icon/explain.svg',
                          fit: BoxFit.cover,
                          color: themeController.currentColor.sc4,
                        ),
                      ),
                    ),
                  ],
                ),
              ),
              SizedBox(
                height: 31.rpx,
              ),
              Padding(
                padding:
                    EdgeInsetsDirectional.fromSTEB(0.rpx, 0.rpx, 0.rpx, 0.rpx),
                child: Column(
                  children: [
                    DataShowWidget(
                      alignment: MainAxisAlignment.center,
                      widget1: Text(
                        "名称",
                        style: TextStyle(
                          color: themeController.currentColor.sc4,
                          fontSize: AppConstants().normal_text_fontSize,
                        ),
                      ),
                      widget2: Text(
                        "测量值",
                        style: TextStyle(
                          color: themeController.currentColor.sc4,
                          fontSize: AppConstants().normal_text_fontSize,
                        ),
                      ),
                      widget3: Text(
                        "参考范围",
                        style: TextStyle(
                          color: themeController.currentColor.sc4,
                          fontSize: AppConstants().normal_text_fontSize,
                        ),
                      ),
                      widget4: Text(
                        "趋势",
                        style: TextStyle(
                          color: themeController.currentColor.sc4,
                          fontSize: AppConstants().normal_text_fontSize,
                        ),
                      ),
                    ),
                    Column(
                      children: data.map<Widget>((data) {
                        return DataShowWidget(
                          alignment: MainAxisAlignment.center,
                          widget1: Row(
                            children: [
                              Expanded(
                                child: Text(
                                  '${data['name']}',
                                  maxLines: 1,
                                  overflow: TextOverflow.ellipsis,
                                  style: TextStyle(
                                    color: themeController.currentColor.sc3,
                                    fontSize:
                                        AppConstants().normal_text_fontSize,
                                  ),
                                ),
                              ),
                              ClickableContainer(
                                backgroundColor: Colors.transparent,
                                highlightColor: Colors.white,
                                padding: EdgeInsetsDirectional.fromSTEB(
                                    14.rpx, 14.rpx, 14.rpx, 14.rpx),
                                borderRadius: 0.rpx,
                                onTap: () {
                                  // Get.toNamed("/deviceShareListPage", arguments: explain);
                                  showTipDialog(
                                    context,
                                    Container(
                                      child: Text(
                                        '${data['desc']}',
                                        style: TextStyle(
                                          fontSize: 26.rpx,
                                          color:
                                              themeController.currentColor.sc3,
                                        ),
                                      ),
                                    ),
                                  );
                                },
                                child: SizedBox(
                                  width: 17.rpx,
                                  height: 17.rpx,
                                  child: SvgPicture.asset(
                                    'assets/img/icon/explain.svg',
                                    fit: BoxFit.cover,
                                    color: Colors.white,
                                  ),
                                ),
                              ),
                            ],
                          ),
                          widget2: Text(
                            '${data['value']}',
                            maxLines: 1,
                            overflow: TextOverflow.ellipsis,
                            style: TextStyle(
                              color: themeController.currentColor.sc3,
                              fontSize: AppConstants().normal_text_fontSize,
                            ),
                          ),
                          widget3: Text(
                            '${data['range']}',
                            maxLines: 1,
                            overflow: TextOverflow.ellipsis,
                            style: TextStyle(
                              color: themeController.currentColor.sc3,
                              fontSize: AppConstants().normal_text_fontSize,
                            ),
                          ),
                          widget4: data['change'] == 0
                              ? Padding(
                                  padding: EdgeInsetsDirectional.fromSTEB(
                                      0, 0.rpx, 0, 0),
                                  child: Container(
                                    width: 22.rpx,
                                    height: 22.rpx,
                                    decoration: BoxDecoration(),
                                    child: SvgPicture.asset(
                                      'assets/img/icon/score_up.svg',
                                      // fit: BoxFit.cover,
                                    ),
                                  ),
                                )
                              : data['change'] == 1
                                  ? Padding(
                                      padding: EdgeInsetsDirectional.fromSTEB(
                                          0, 0.rpx, 0, 0),
                                      child: Container(
                                        width: 22.rpx,
                                        height: 22.rpx,
                                        decoration: BoxDecoration(),
                                        child: SvgPicture.asset(
                                          'assets/img/icon/score_down.svg',
                                          // fit: BoxFit.cover,
                                        ),
                                      ),
                                    )
                                  : Padding(
                                      padding: EdgeInsetsDirectional.fromSTEB(
                                          0, 0.rpx, 0, 0),
                                      child: Container(
                                        width: 22.rpx,
                                        height: 22.rpx,
                                        decoration: BoxDecoration(),
                                        child: SvgPicture.asset(
                                          'assets/img/icon/score_equal.svg',
                                          // fit: BoxFit.fill,
                                        ),
                                      ),
                                    ),
                        ).paddingOnly(bottom: 0.rpx); // 在每个组件下方添加间隔
                      }).toList(),
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
      );
    } catch (e) {
      es.EasyDartModule.logger.error("打鼾监测绘制异常${e}");
      return Container();
    }
  }

  List<Map<String, dynamic>> transformHrvData(List<dynamic> originalData) {
    return originalData.map((item) {
      return {
        "name": item["name"] ?? "未知指标".tr,
        "value": (item["value"] is num) ? item["value"].toDouble() : 0.0,
        "range": item["range"] ?? "0-0",
        "change": item["trend"] ?? 0,
        "desc": item["tips"]?.toString().isNotEmpty == true
            ? item["tips"].toString()
            : "未知数据".tr
      };
    }).toList();
  }
}
