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/ServiceConstant.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/home_page/SleepDataModuleWidget.dart';
import 'package:vbvs_app/component/home_page/SleepDateWidget.dart';
import 'package:vbvs_app/component/tool/ClickableContainer.dart';
import 'package:vbvs_app/controller/theme_controller/ThemeController.dart';

class DynamicReportDetailWidget extends StatelessWidget {
  final List<SleepDateWidget> sleepDateWidgets;
  final List<SleepDataModuleWidget> sleepDataModuleWidgets;
  final ThemeController themeController = Get.find();
  final Map targetDevice;

  DynamicReportDetailWidget({
    required this.sleepDateWidgets,
    required this.sleepDataModuleWidgets,
    required this.targetDevice,
  });

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsetsDirectional.fromSTEB(0, 25.rpx, 0, 25.rpx),
      child: Container(
        width: double.infinity,
        decoration: BoxDecoration(
          color: themeController.currentColor.sc5,
          borderRadius:
              BorderRadius.circular(AppConstants().normal_container_radius),
        ),
        child: Padding(
          padding:
              EdgeInsetsDirectional.fromSTEB(30.rpx, 30.rpx, 30.rpx, 30.rpx),
          child: Column(
            mainAxisSize: MainAxisSize.max,
            children: [
              _buildHeader(context),
              _buildSleepDateWidgets(),
              SizedBox(height: 20.rpx),
              _buildSleepDataModuleWidgets(),
            ],
          ),
        ),
      ),
    );
  }

  Widget _buildHeader(BuildContext context) {
    return Container(
      width: double.infinity,
      child: Row(
        mainAxisSize: MainAxisSize.max,
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text(
            '${targetDevice['person']?['name'] == null ? '未命名'.tr : targetDevice['person']['name']}',
            style: FlutterFlowTheme.of(context).bodyMedium.override(
                  fontFamily: 'Inter',
                  fontSize: 30.rpx,
                  letterSpacing: 0.0,
                  color: themeController.currentColor.sc3,
                ),
          ),
          ClickableContainer(
            backgroundColor: Colors.transparent,
            highlightColor: themeController.currentColor.sc3,
            borderRadius: 0,
            padding: EdgeInsets.zero,
            onTap: () {
              String mac = targetDevice['mac'];
              List<SleepDateWidget> selectedWidgets = sleepDateWidgets
                  .where(
                    (widget) => widget.isSelected == true,
                  )
                  .toList();
              DateTime dateTime = DateTime.fromMillisecondsSinceEpoch(
                  int.parse(selectedWidgets[0].time!));
              String time = MyUtils.formatBindTime(dateTime);
              String sleepReportUrl =
                  "${ServiceConstant.sleep_report_url}?mac=${mac}&token=${ServiceConstant.sleep_token}&date=${time}";
              Get.toNamed("/sleepReportPage", arguments: sleepReportUrl);
            },
            child: Row(
              mainAxisSize: MainAxisSize.max,
              children: [
                Text(
                  '首页.报告详情'.tr,
                  style: FlutterFlowTheme.of(context).bodyMedium.override(
                        fontFamily: 'Inter',
                        fontSize: 26.rpx,
                        letterSpacing: 0.0,
                        color: themeController.currentColor.sc3,
                      ),
                ),
                Padding(
                  padding: EdgeInsetsDirectional.fromSTEB(0, 6.rpx, 0, 0.rpx),
                  child: SvgPicture.asset(
                    'assets/img/icon/arrow_right.svg',
                    width: 14.rpx,
                    height: 14.rpx,
                    color: themeController.currentColor.sc3,
                  ),
                ),
              ].divide(SizedBox(width: 22.rpx)),
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildSleepDateWidgets() {
    return Container(
      width: double.infinity,
      decoration: BoxDecoration(),
      child: SingleChildScrollView(
        scrollDirection: Axis.horizontal,
        child: Row(
          mainAxisSize: MainAxisSize.max,
          children: sleepDateWidgets
              .map((widget) => widget)
              .toList()
              .divide(SizedBox(width: 20.rpx)),
        ),
      ),
    );
  }

  Widget _buildSleepDataModuleWidgets() {
    // bool hasData = sleepDataModuleWidgets.any((w) {
    //   final data = w.data;
    //   return data['state'] != null &&
    //       data['state'].toString().trim().isNotEmpty;
    // });
    bool hasData = sleepDataModuleWidgets.length > 0;

    if (!hasData) {
      return Container(
        height: 100.rpx,
        alignment: Alignment.center,
        child: Text(
          '暂无数据'.tr,
          style: FlutterFlowTheme.of(Get.context!).bodyMedium.override(
                fontFamily: 'Inter',
                fontSize: 28.rpx,
                color: themeController.currentColor.sc4,
              ),
        ),
      );
    }

    return Container(
      width: double.infinity,
      child: SingleChildScrollView(
        scrollDirection: Axis.horizontal,
        child: Row(
          mainAxisSize: MainAxisSize.max,
          children: sleepDataModuleWidgets
              .map((widget) => widget)
              .toList()
              .divide(SizedBox(width: 14.rpx)),
        ),
      ),
    );
  }
}
