//实时数据卡片
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:get/get.dart';

import 'realController.dart';

class RealDataShow extends StatelessWidget {
  const RealDataShow({super.key});

  @override
  Widget build(BuildContext context) {
    final Size size = MediaQuery.of(context).size;
    final Orientation orientation =
        MediaQuery.of(context).orientation; // 获取屏幕方向
    // var isDataVisible = true.obs;
    final RealController controller1 = Get.find();
    bool isDarkMode = Theme.of(context).brightness == Brightness.dark;
    double width;
    double height;

    // 根据屏幕方向设置宽度和高度
    if (orientation == Orientation.portrait) {
      width = size.width / 5 * 2.4; // 纵向时的宽度
      height = size.height / 5 * 2; // 纵向时的高度
    } else {
      width = size.width / 3.7; // 横向时的宽度
      height = size.height / 1.5; // 横向时的高度
    }
    return Obx(() => Positioned(
        left: controller1.sightOffset1.value.dx,
        top: controller1.sightOffset1.value.dy,
        width: width,
        height: height,
        child: GestureDetector(
            onPanStart: (details) {
              // 记录初始偏移量
              controller1.sightInit1.value =
                  details.localPosition - controller1.sightOffset1.value;
            },
            onPanUpdate: (details) {
              // 更新卡片的位置
              controller1.sightOffset1.value =
                  details.localPosition - controller1.sightInit1.value;
            },
            child: Visibility(
                visible: controller1.isDataVisible.value,
                child: Container(
                  decoration: const BoxDecoration(
                      color: Color.fromARGB(0, 214, 133, 133)),
                  child: Card(
                    elevation: 5.0,
                    child: Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: Column(
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Text(
                                "实时数据",
                                style: TextStyle(
                                    fontSize: 19,
                                    fontWeight: FontWeight.bold,
                                    color: isDarkMode
                                        ? Colors.white
                                        : Colors.black),
                              ),
                              IconButton(
                                icon: const Icon(Icons.close),
                                onPressed: () {
                                  controller1.isDataVisible.value = false;
                                },
                              ),
                            ],
                          ),
                          ..._buildRealTimeDataList(context),
                        ],
                      ),
                    ),
                  ),
                )))));
  }

  Widget _buildCard(BuildContext context, double width, double height) {
    final RealController controller1 = Get.find();

    return Container(
      decoration: const BoxDecoration(color: Color.fromARGB(0, 214, 133, 133)),
      child: Card(
        elevation: 5.0,
        child: Padding(
          padding: const EdgeInsets.all(8.0),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  const Text("实时数据"),
                  IconButton(
                    icon: const Icon(Icons.close),
                    onPressed: () {
                      controller1.isDataVisible.value = false;
                    },
                  ),
                ],
              ),
              ..._buildRealTimeDataList(context),
            ],
          ),
        ),
      ),
    );
  }

  List<Widget> _buildRealTimeDataList(BuildContext context) {
    // 获取实时控制器
    final RealController realController = Get.find();
    bool isDarkMode = Theme.of(context).brightness == Brightness.dark;

    // final orientation = MediaQuery.of(context).orientation;
    // bool isPortrait = Orientation.portrait == orientation ? true : false;
    // 创建数据项列表
    List<DataItem> dataItems = [
      DataItem(
          "速度(m/min):",
          () => realController.speed.abs() >= 100
              ? realController.speed.toStringAsFixed(1)
              : realController.speed.toStringAsFixed(2)),
      DataItem("时间(min:s):", () => realController.time.value),
      DataItem("深度(m):", () => realController.depth.toStringAsFixed(2)),
      DataItem("1#瞬时流量(L/min):",
          () => realController.subtotalFlow1.toStringAsFixed(2)),
      DataItem("2#瞬时流量(L/min):",
          () => realController.subtotalFlow2.toStringAsFixed(2)),
      DataItem(
          "1#累计流量(L):", () => realController.totalFlow1.toStringAsFixed(2)),
      DataItem(
          "2#累计流量(L):", () => realController.totalFlow2.toStringAsFixed(2)),
    ];

    // 返回数据项的列表
    return dataItems.map((item) {
      return Padding(
        padding: const EdgeInsets.symmetric(vertical: 4.0),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.start,
          children: [
            Text(item.title,
                style: TextStyle(
                  fontSize: 13,
                  color: isDarkMode ? Colors.white : Colors.black,
                )),
            Obx(() => Text(item.value(),
                style: TextStyle(
                  fontSize: 18,
                  color: isDarkMode ? Colors.white : Colors.black,
                ))),
          ],
        ),
      );
    }).toList();
  }
}

class DataItem {
  final String title;
  final String Function() value;

  DataItem(this.title, this.value);
}