import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:vnote_device_plugin/consts/types.dart';
import 'package:vnote_device_plugin/devices/nibp.dart';
import 'package:vnote_device_plugin/models/exams/nibp.dart';

import 'package:vitalapp/components/alert_dialog.dart';
import 'package:vitalapp/managers/interfaces/models/device.dart';
import 'package:vitalapp/pages/check/widgets/device_controller.dart';
import 'package:vitalapp/pages/check/widgets/exam_configurable/exam_card.dart';
import 'package:vitalapp/managers/interfaces/permission.dart';
import 'package:vitalapp/pages/medical/models/item.dart';
import 'package:vitalapp/pages/medical/models/worker.dart';
import 'package:vitalapp/pages/medical/widgets/device_status.dart';
import 'package:vitalapp/pages/medical/widgets/switch_button.dart';

/// 小弹窗输入
class VDialogBloodPressure extends StatelessWidget {
  /// 标题
  final String? title;

  /// 描述
  final String? description;

  /// 输入占位符
  final String? placeholder;

  /// 初始值
  final List<String>? initialValue;

  const VDialogBloodPressure({
    super.key,
    this.title,
    this.description,
    this.placeholder,
    this.initialValue,
  });

  Future<String?> show<String>() => VAlertDialog.showDialog<String>(this);

  @override
  Widget build(BuildContext context) {
    final controller1 = TextEditingController(text: initialValue?.first);
    final controller2 = TextEditingController(text: initialValue?.last);
    return VAlertDialog(
      title: title,
      width: 440,
      contentPadding: const EdgeInsets.symmetric(vertical: 12, horizontal: 24),
      content: _buildContent(context, controller1, controller2),
      showCancel: true,
      onConfirm: () {
        Get.back(result: json.encode([controller1.text, controller2.text]));
      },
    );
  }

  Widget _buildContent(
    BuildContext context,
    TextEditingController controller1,
    TextEditingController controller2,
  ) {
    final children = <Widget>[];
    if (description != null) {
      children.add(
        Padding(
          padding: const EdgeInsets.symmetric(horizontal: 4),
          child: Text(
            description!,
            style: const TextStyle(color: Colors.black87, fontSize: 18),
          ),
        ),
      );
      children.add(const SizedBox(height: 8));
    } else {
      children.add(const SizedBox(height: 12));
    }
    children.add(_buildInputWidget(context, controller1, controller2));
    return SingleChildScrollView(
      child: Column(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: children,
      ),
    );
  }

  Widget _buildInputWidget(
    BuildContext context,
    TextEditingController controller1,
    TextEditingController controller2,
  ) {
    const fontSize = 20.0;
    return Row(
      children: [
        _buildItem(context, controller1, '高压'),
        const Text(
          '/',
          style: TextStyle(fontSize: fontSize),
        ),
        _buildItem(context, controller2, '低压'),
      ],
    );
  }

  Widget _buildItem(
    BuildContext context,
    TextEditingController controller,
    String hintText,
  ) {
    const fontSize = 20.0;
    const height = 56.0;
    return Expanded(
      child: SizedBox(
        height: height,
        child: TextField(
          controller: controller,
          readOnly: false,
          autofocus: true,
          keyboardType: const TextInputType.numberWithOptions(
              decimal: true), // 允许输入数字和小数点
          inputFormatters: [
            FilteringTextInputFormatter.allow(
              RegExp(r'^\d+\.?\d{0,2}'),
            ), // 只允许输入数字和小数点,俩位小数
          ],
          style: const TextStyle(fontSize: fontSize),
          decoration: InputDecoration(
            border: const UnderlineInputBorder(
              borderRadius: BorderRadius.zero,
              borderSide: BorderSide(),
            ),
            enabledBorder: const UnderlineInputBorder(
              borderRadius: BorderRadius.zero,
              borderSide: BorderSide(
                color: Colors.black54,
              ),
            ),
            focusedBorder: UnderlineInputBorder(
              borderRadius: BorderRadius.zero,
              borderSide: BorderSide(
                color: Theme.of(context).primaryColor.withOpacity(.4),
              ),
            ),
            filled: true,
            fillColor: Colors.white,
            contentPadding: const EdgeInsets.symmetric(
              vertical: (height - fontSize * 1.2) / 2,
              horizontal: 8,
            ),
            hintStyle: const TextStyle(fontSize: fontSize),
            labelStyle: const TextStyle(fontSize: fontSize),
            hintText: hintText,
            isCollapsed: true,
          ),
          onSubmitted: (value) {
            print(value);
            Get.back(result: value);
          },
        ),
      ),
    );
  }
}

/// TODO 优化血压组件，存储需要重新设计
// ignore: must_be_immutable
class ExamBloodPressure extends StatefulWidget {
  ExamBloodPressure({
    super.key,
    required this.currentValue,
    required this.bloodPressure,
  });
  Map currentValue;
  Function(Map) bloodPressure;
  @override
  State<ExamBloodPressure> createState() => _ExamBloodPressureState();
}

class _ExamBloodPressureState extends State<ExamBloodPressure> {
  var permissionManager = Get.find<IPermissionManager>();
  final controller = Get.find<DeviceController>();
  PressureStatus pressureStatus = PressureStatus.left;
  WorkerStatus connectStatus = WorkerStatus.connecting;
  late NibpDeviceWorker worker;

  late int liveValue = 0;
  NibpExamValue? value;
  NibpExamValue? leftNibpExamValue;
  NibpExamValue? rightNibpExamValue;
  String? right;
  String? left;
  bool isAccessTypeAuth = false;
  @override
  void initState() {
    getPermission();
    currentDevice();
    if (widget.currentValue['left'] != null) {
      leftNibpExamValue = NibpExamValue(
          systolicPressure: int.parse(
              jsonDecode(widget.currentValue['left']).first.toString()),
          diastolicPressure: int.parse(
              jsonDecode(widget.currentValue['left']).last.toString()),
          pulse: 0);
    }
    if (widget.currentValue['right'] != null) {
      rightNibpExamValue = NibpExamValue(
          systolicPressure: int.parse(
              jsonDecode(widget.currentValue['right']).first.toString()),
          diastolicPressure: int.parse(
              jsonDecode(widget.currentValue['right']).last.toString()),
          pulse: 0);
    }

    super.initState();
  }

  void loadListeners() {
    worker.liveUpdateEvent.addListener(_onLiveUpdate);
    worker.resultUpdateEvent.addListener(_onSuccess);
    worker.connectErrorEvent.addListener(_onConnectFail);
    worker.connectedEvent.addListener(_onConnectSuccess);
    worker.connect();
  }

  Future<void> disconnect() async {
    worker.connectErrorEvent.removeListener(_onConnectFail);
    worker.connectedEvent.removeListener(_onConnectSuccess);
    worker.liveUpdateEvent.removeListener(_onLiveUpdate);
    worker.resultUpdateEvent.removeListener(_onSuccess);
    worker.disconnectedEvent.removeListener(_onDisconnected);
    await worker.disconnect();
  }

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

  Future<void> currentDevice() async {
    DeviceModel? device = await controller.getDevice(DeviceTypes.NIBP);
    isAccessTypeAuth = await controller.isAccessTypeAuth(DeviceTypes.NIBP);
    if (!isAccessTypeAuth) {
      return;
    }
    if (device.isNull) {
      connectStatus = WorkerStatus.unboundDevice;
      setState(() {});
      return;
    }
    worker = NibpDeviceWorker(
      mac: device?.mac ?? '',
      model: device?.model ?? '',
    );

    loadListeners();
  }

  void _onLiveUpdate(_, int e) {
    setState(() {
      liveValue = e;
    });
  }

  void _onSuccess(_, NibpExamValue e) {
    setState(() {
      value = e;
      if (pressureStatus == PressureStatus.left) {
        leftNibpExamValue = value;
        left = jsonEncode([
          leftNibpExamValue!.systolicPressure,
          leftNibpExamValue!.diastolicPressure
        ]);
      } else {
        rightNibpExamValue = value;
        right = jsonEncode([
          rightNibpExamValue!.systolicPressure,
          rightNibpExamValue!.diastolicPressure
        ]);
      }
      widget.bloodPressure({
        'left': left,
        'right': right,
      });
    });
  }

  void _onConnectFail(sender, e) {
    print('连接设备失败');
    connectStatus = WorkerStatus.connectionFailed;
    worker.connect();
    setState(() {});
  }

  void _onConnectSuccess(sender, e) {
    connectStatus = WorkerStatus.connected;
    setState(() {});
  }

  void _onDisconnected(sender, e) {
    connectStatus = WorkerStatus.disconnected;
    print('设备连接中断');
    worker.connect();
    setState(() {});
  }

  Future<void> getPermission() async {
    await permissionManager.requestLocationPermission();
    await permissionManager.requestBluetoothConnectPermission();
    await permissionManager.requestBluetoothAdvertisePermission();
    await permissionManager.requestBluetoothScanPermission();
  }

  Widget _buildValue() {
    if (value != null) {
      return _buildResultWidget();
    }
    return _buildLiveWidget();
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        ExamCard(
          // clickCard: () {},
          content: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              _SideBar(
                title: '血压测量值',
                value: _buildValue(),
                unit: 'mmHg',
              ),
              const Divider(indent: 30),
              const SizedBox(
                height: 32,
              ),
              InkWell(
                child: _SideBar(
                  title: '左侧血压',
                  value: _buildResult(leftNibpExamValue),
                  unit: 'mmHg',
                ),
                onTap: () async {
                  String? result = await VDialogBloodPressure(
                    title: '左侧血压',
                    initialValue: [
                      leftNibpExamValue?.systolicPressure.toString() ?? '',
                      leftNibpExamValue?.diastolicPressure.toString() ?? '',
                    ],
                  ).show();

                  if (result != null) {
                    widget.bloodPressure({
                      'left': result,
                      'right': right,
                    });
                    leftNibpExamValue = NibpExamValue(
                      diastolicPressure: int.parse(jsonDecode(result).last),
                      systolicPressure: int.parse(jsonDecode(result).first),
                      pulse: 0,
                    );
                  }

                  setState(() {});
                },
              ),
              const Divider(indent: 30),
              const SizedBox(
                height: 32,
              ),
              InkWell(
                child: _SideBar(
                  title: '右侧血压',
                  value: _buildResult(rightNibpExamValue),
                  unit: 'mmHg',
                ),
                onTap: () async {
                  String? result = await VDialogBloodPressure(
                    title: '右侧血压',
                    initialValue: [
                      rightNibpExamValue?.systolicPressure.toString() ?? '',
                      rightNibpExamValue?.diastolicPressure.toString() ?? '',
                    ],
                  ).show();
                  if (result != null) {
                    widget.bloodPressure({
                      'left': left,
                      'right': result,
                    });
                    rightNibpExamValue = NibpExamValue(
                      diastolicPressure: int.parse(jsonDecode(result).last),
                      systolicPressure: int.parse(jsonDecode(result).first),
                      pulse: 0,
                    );
                  }

                  setState(() {});
                },
              ),
            ],
          ),
        ),
        if (isAccessTypeAuth) DeviceStatus(connectStatus: connectStatus),
      ],
    );
  }

  Widget _buildButtonGroup() {
    return AnimatedToggle(
      values: const ['左侧', '右侧'],
      onToggleCallback: (value) {
        setState(() {
          pressureStatus = value;
        });
      },
      statusValue: pressureStatus,
      buttonColor: Theme.of(context).primaryColor,
      backgroundColor: const Color(0xFFB5C1CC),
      textColor: const Color(0xFFFFFFFF),
    );
  }

  Widget _buildLiveWidget() {
    const textStyle = TextStyle(fontSize: 80, color: Colors.black);
    return Center(
      child: Row(
        children: [
          Text(
            liveValue.toString(),
            style: textStyle,
          ),
          _buildButtonGroup(),
        ],
      ),
    );
  }

  Widget _buildResult(NibpExamValue? nibpExamValue) {
    const textStyle = TextStyle(fontSize: 48, color: Colors.black);

    return Row(
      children: [
        Column(
          mainAxisAlignment: MainAxisAlignment.center,
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Align(
              alignment: Alignment.centerLeft,
              child: Text(
                nibpExamValue?.systolicPressure.toString() ?? '',
                style: textStyle,
              ),
            ),
            Align(
              alignment: Alignment.centerRight,
              child: Text(
                nibpExamValue?.diastolicPressure.toString() ?? '',
                style: textStyle,
              ),
            ),
          ],
        ),
      ],
    );
  }

  Widget _buildResultWidget() {
    const textStyle = TextStyle(fontSize: 48, color: Colors.black);
    return Row(
      children: [
        Column(
          mainAxisAlignment: MainAxisAlignment.center,
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Align(
              alignment: Alignment.centerLeft,
              child: Text(
                value!.systolicPressure.toString(),
                style: textStyle,
              ),
            ),
            Align(
              alignment: Alignment.centerRight,
              child: Text(
                value!.diastolicPressure.toString(),
                style: textStyle,
              ),
            ),
          ],
        ),
        _buildButtonGroup(),
      ],
    );
  }
}

class _SideBar extends StatelessWidget {
  final String title;
  final Widget value;
  final String unit;

  const _SideBar({
    required this.title,
    required this.value,
    required this.unit,
  });

  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Container(
          padding: const EdgeInsets.symmetric(horizontal: 30),
          child: Text(
            title,
            style: const TextStyle(
              fontSize: 25,
            ),
          ),
        ),
        Container(
          alignment: Alignment.bottomRight,
          padding: const EdgeInsets.only(
            bottom: 20,
            right: 30,
            left: 40,
          ),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.end,
            crossAxisAlignment: CrossAxisAlignment.end,
            children: [
              value,
            ],
          ),
        ),
      ],
    );
  }
}



// /// 血压组件
// class ExamBloodPressure extends StatelessWidget {
//   const ExamBloodPressure({
//     super.key,
//     required this.currentFormObject,
//     required this.currentBloodPressureValue,
//     required this.commonBloodPressure,
//   });
//   final FormObject currentFormObject;
//   final List<String> currentBloodPressureValue;
//   final Function commonBloodPressure;

//   String get getBloodPressure {
//     if (currentBloodPressureValue.first.isNotEmpty ||
//         currentBloodPressureValue.last.isNotEmpty) {
//       return '${currentBloodPressureValue.first} / ${currentBloodPressureValue.last}';
//     } else {
//       return '';
//     }
//   }

//   @override
//   Widget build(BuildContext context) {
//     return ExamCard(
//       title: currentFormObject.label ?? '',
//       clickCard: () => commonBloodPressure.call(),
//       content: Container(
//         alignment: Alignment.bottomRight,
//         padding: const EdgeInsets.only(bottom: 20, right: 30, left: 40),
//         constraints: const BoxConstraints(minHeight: 150),
//         child: FittedBox(
//           child: Row(
//             mainAxisAlignment: MainAxisAlignment.end,
//             crossAxisAlignment: CrossAxisAlignment.end,
//             children: [
//               RichText(
//                 text: TextSpan(
//                   text: getBloodPressure,
//                   style: const TextStyle(
//                     fontSize: 80,
//                     color: Colors.black,
//                   ),
//                   children: [
//                     TextSpan(
//                       text: currentFormObject.append ?? '',
//                       style: const TextStyle(fontSize: 25),
//                     )
//                   ],
//                 ),
//               ),
//             ],
//           ),
//         ),
//       ),
//     );
//   }
// }
