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/component/tool/TopSlideNotification.dart';
import 'package:vbvs_app/controller/theme_controller/ThemeController.dart';

class DynamicReportDetailWidget extends StatefulWidget {
  final List<SleepDateWidget> sleepDateWidgets;
  final List<SleepDataModuleWidget> sleepDataModuleWidgets;
  final Map targetDevice;

  const DynamicReportDetailWidget({
    Key? key,
    required this.sleepDateWidgets,
    required this.sleepDataModuleWidgets,
    required this.targetDevice,
  }) : super(key: key);

  @override
  State<DynamicReportDetailWidget> createState() =>
      _DynamicReportDetailWidgetState();
}

class _DynamicReportDetailWidgetState extends State<DynamicReportDetailWidget> {
  final ThemeController themeController = Get.find();
  final ScrollController _scrollController = ScrollController();
  bool _hasScrolled = false;

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

    WidgetsBinding.instance.addPostFrameCallback((_) {
      Future.delayed(Duration(milliseconds: 500), () {
        if (!_hasScrolled &&
            _scrollController.hasClients &&
            _scrollController.position.maxScrollExtent > 0) {
          _scrollController.animateTo(
            _scrollController.position.maxScrollExtent,
            duration: Duration(milliseconds: 300),
            curve: Curves.easeOut,
          );
          _hasScrolled = true;
        }
      });
    });
  }

  @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, widget.targetDevice),
              SizedBox(height: 33.rpx),
              _buildSleepDateWidgets(),
              SizedBox(height: 20.rpx),
              _buildSleepDataModuleWidgets(),
            ],
          ),
        ),
      ),
    );
  }

  Widget _buildHeader(BuildContext context, Map targetDevice) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        ClickableContainer(
          backgroundColor: Colors.transparent,
          highlightColor: themeController.currentColor.sc3.withOpacity(0.2),
          borderRadius: 0,
          padding: EdgeInsets.zero,
          onTap: () async {
            await Get.toNamed("/bodyDevice", arguments: targetDevice);
          },
          child: Text(
            '${targetDevice['person']?['name'] == null ? '未命名'.tr : targetDevice['person']['name']}',
            style: TextStyle(
              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 = widget.sleepDateWidgets
                .where((w) => w.isSelected == true)
                .toList();
            if (selectedWidgets.isNotEmpty) {
              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);
            } else {
              TopSlideNotification.show(context,
                  text: "当前暂无数据".tr,
                  textColor: themeController.currentColor.sc9);
            }
          },
          child: Row(
            children: [
              Text(
                '首页.报告详情'.tr,
                style: TextStyle(
                  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,
      child: SingleChildScrollView(
        controller: _scrollController,
        scrollDirection: Axis.horizontal,
        child: Row(
          children: widget.sleepDateWidgets
              .map((widget) => widget)
              .toList()
              .divide(SizedBox(width: 20.rpx)),
        ),
      ),
    );
  }

  Widget _buildSleepDataModuleWidgets() {
    if (widget.sleepDataModuleWidgets.isEmpty) {
      return Container(
        height: 200.rpx,
        alignment: Alignment.center,
        child: Text(
          '暂无数据'.tr,
          style: TextStyle(
            fontFamily: 'Inter',
            fontSize: 28.rpx,
            color: themeController.currentColor.sc4,
          ),
        ),
      );
    }

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

  void resetScroll() {
    _hasScrolled = false;
    if (_scrollController.hasClients) {
      _scrollController.jumpTo(0);
    }
  }
}
