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/GradientLine.dart';
import 'package:vbvs_app/pages/sleep_report/chart/SnoreWaveform.dart';
import 'package:EasyDartModule/EasyDartModule.dart' as es;

//睡眠规律性
class SleepViewWidget extends StatefulWidget {
  var sleepReport;
  SleepViewWidget({super.key, required this.sleepReport});

  @override
  State<SleepViewWidget> createState() => _SleepViewWidgetState();
}

class _SleepViewWidgetState extends State<SleepViewWidget> {
  @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 is! Map ||
          widget.sleepReport.isEmpty) {
        return Container();
      }

      // List showLabel = widget.sleepReport['sleepData']['type'];
      List showLabel = widget.sleepReport['sleepData']['type']
          .where((item) => item['show'] != false)
          .toList();

      List snoreValues = [];
      List lightSnore = widget.sleepReport['ssp']['data'][0];
      List heavySnore = widget.sleepReport['ssp']['data'][1];


      snoreValues = [...lightSnore, ...heavySnore];


      snoreValues.sort((a, b) {
        return a['st'].compareTo(b['st']);
      });
      Map time = MyUtils.diffHoursMinutesMap(
          widget.sleepReport['startTime'], widget.sleepReport['endTime']);
      int hour = time['hours'];
      int minutes = time['minutes'];

      List stages = widget.sleepReport['sleepData']['stages'];
      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(
                      "睡眠规律性".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(
                              "睡眠规律性介绍。",
                              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: 83.rpx,
              ),
              Padding(
                padding: EdgeInsetsDirectional.fromSTEB(
                    30.rpx, 0.rpx, 30.rpx, 0.rpx),
                child: Row(
                  crossAxisAlignment: CrossAxisAlignment.end,
                  children: [
                    // 左侧 - 入睡时间
                    Column(
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        Image.asset(
                          "assets/img/moon.png",
                          width: 43.rpx,
                          height: 43.rpx,
                          fit: BoxFit.cover,
                        ),
                        SizedBox(height: 33.rpx),
                        Container(
                          height: 40.rpx,
                          child: Text(
                            "${MyUtils.formatToHHmm(widget.sleepReport['startTime'])}",
                            style: TextStyle(
                              color: themeController.currentColor.sc3,
                              fontSize: AppConstants().normal_text_fontSize,
                            ),
                          ),
                        ),
                        SizedBox(height: 20.rpx),
                        Text(
                          "入睡时间".tr,
                          style: TextStyle(
                            color: themeController.currentColor.sc4,
                            fontSize: AppConstants().normal_text_fontSize,
                          ),
                          maxLines: 1,
                          overflow: TextOverflow.ellipsis,
                        ),
                      ],
                    ),

                    // 中间 - 渐变线 + 时间
                    Expanded(
                      child: Column(
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          Container(
                              height: 43.rpx,
                              child: Column(
                                mainAxisAlignment: MainAxisAlignment.center,
                                children: [
                                  GradientLine(
                                    height: 1,
                                    color: Colors.white60,
                                  ),
                                ],
                              )),
                          SizedBox(height: 33.rpx), // 与图标对齐
                          Text.rich(
                            TextSpan(
                              children: [
                                TextSpan(
                                  text: "$hour",
                                  style: TextStyle(
                                    color: themeController
                                        .currentColor.sc2, // 小时数字颜色
                                    fontSize: 36.rpx, // 小时数字字号
                                  ),
                                ),
                                TextSpan(
                                  text: "小时",
                                  style: TextStyle(
                                    color: themeController.currentColor.sc3,
                                    fontSize:
                                        AppConstants().title_text_fontSize,
                                  ),
                                ),
                                TextSpan(
                                  text: "$minutes",
                                  style: TextStyle(
                                    color: themeController
                                        .currentColor.sc2, // 小时数字颜色
                                    fontSize: 36.rpx, // 分钟数字字号
                                  ),
                                ),
                                TextSpan(
                                  text: "分钟",
                                  style: TextStyle(
                                    color: themeController.currentColor.sc3,
                                    fontSize:
                                        AppConstants().title_text_fontSize,
                                  ),
                                ),
                              ],
                            ),
                            maxLines: 1,
                            overflow: TextOverflow.ellipsis,
                          ),
                          SizedBox(height: 20.rpx),
                          Text(
                            "睡眠时长".tr,
                            style: TextStyle(
                              color: themeController.currentColor.sc4,
                              fontSize: AppConstants().normal_text_fontSize,
                            ),
                            maxLines: 1,
                            overflow: TextOverflow.ellipsis,
                          ),
                        ],
                      ),
                    ),

                    // 右侧 - 起床时间图标
                    Column(
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        Image.asset(
                          "assets/img/sun.png",
                          width: 43.rpx,
                          height: 43.rpx,
                          fit: BoxFit.cover,
                        ),
                        SizedBox(height: 33.rpx),
                        Container(
                          height: 40.rpx,
                          child: Text(
                            "${MyUtils.formatToHHmm(widget.sleepReport['endTime'])}",
                            style: TextStyle(
                              color: themeController.currentColor.sc3,
                              fontSize: AppConstants().normal_text_fontSize,
                            ),
                          ),
                        ),
                        SizedBox(height: 20.rpx),
                        Text(
                          "起床时间".tr,
                          style: TextStyle(
                            color: themeController.currentColor.sc4,
                            fontSize: AppConstants().normal_text_fontSize,
                          ),
                          maxLines: 1,
                          overflow: TextOverflow.ellipsis,
                        ),
                      ],
                    ),
                  ],
                ),
              ),
              SizedBox(
                height: 49.rpx,
              ),
              Padding(
                padding: EdgeInsetsDirectional.fromSTEB(
                    26.rpx, 0.rpx, 26.rpx, 0.rpx),
                child: SnoreChartContainer(
                  snoreValues: snoreValues,
                  barData: stages,
                  startTime: widget.sleepReport['startTime'],
                  endTime: widget.sleepReport['endTime'],
                  showLabel: showLabel,
                ),
              ),
              SizedBox(
                height: 70.rpx,
              ),
              Wrap(
                spacing: 55.rpx,
                runSpacing: 20.rpx,
                children: showLabel.map<Widget>((item) {
                  return Container(
                    padding: EdgeInsets.all(5.rpx),
                    child: Row(
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        Container(
                          width: 20.rpx,
                          height: 20.rpx,
                          decoration: BoxDecoration(
                            color: item["color"] == null || item["color"] == ""
                                ? Colors.transparent
                                : stringToColor(item["color"]),
                            borderRadius: BorderRadius.circular(10.rpx),
                          ),
                        ),
                        SizedBox(width: 17.rpx),
                        Text(
                          item["name"],
                          style: TextStyle(
                            color: Colors.white,
                            fontSize: 24.rpx,
                          ),
                        ),
                      ],
                    ),
                  );
                }).toList(),
              ),
            ],
          ),
        ),
      );
    } catch (e) {
        es.EasyDartModule.logger.error("打鼾监测绘制异常${e}");
      return Container();
    }
  }
}
