import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:vnote_device_plugin/devices/urine.dart';
import 'package:vnote_device_plugin/models/exams/urine.dart';

class UrineCard extends StatefulWidget {
  final String mac;
  final String model;

  const UrineCard({super.key, required this.mac, required this.model});

  @override
  State<StatefulWidget> createState() => _UrineCardState();
}

class _UrineCardState extends State<UrineCard> {
  late final UrineDeviceWorker worker = UrineDeviceWorker(
    mac: widget.mac,
    model: widget.model,
  );
  UrineExamData? value;
  bool _working = false;

  @override
  void initState() {
    worker.successEvent.addListener(_onSuccess);
    worker.errorEvent.addListener(_onError);
    worker.noResultEvent.addListener(_onNoResult);
    super.initState();
  }

  @override
  void dispose() {
    worker.successEvent.removeListener(_onSuccess);
    worker.errorEvent.removeListener(_onError);
    worker.noResultEvent.removeListener(_onNoResult);
    worker.disconnect();

    super.dispose();
  }

  void _onSuccess(Object _, UrineExamData e) {
    setState(() {
      value = e;
    });
  }

  void _onError(_, String e) {
    setState(() {
      value = null;
    });
    Get.snackbar(
      "提示",
      "测量错误: $e",
      snackPosition: SnackPosition.TOP,
    );
  }

  void _onNoResult(_, void e) {
    Get.snackbar(
      "提示",
      "测量无结果",
      snackPosition: SnackPosition.TOP,
    );
  }

  @override
  Widget build(BuildContext context) {
    const textStyle = TextStyle(fontSize: 14);
    return Card(
      elevation: 4,
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
      child: Container(
        alignment: Alignment.center,
        width: 360,
        height: 160,
        child: SizedBox(
          height: 120,
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.end,
            children: [
              if (value != null) ...[
                Column(
                  mainAxisAlignment: MainAxisAlignment.start,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text("LEU: ${value!.leu}", style: textStyle),
                    Text("BLD: ${value!.bld}", style: textStyle),
                    Text("NIT: ${value!.nit}", style: textStyle),
                    Text("KET: ${value!.ket}", style: textStyle),
                    Text("UBG: ${value!.ubg}", style: textStyle),
                    Text("BIL: ${value!.bil}", style: textStyle),
                  ],
                ),
                const SizedBox(width: 8),
                Column(
                  mainAxisAlignment: MainAxisAlignment.start,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text("PRO: ${value!.pro}", style: textStyle),
                    Text("GLU: ${value!.glu}", style: textStyle),
                    Text("PH: ${value!.ph}", style: textStyle),
                    Text("SG: ${value!.sg}", style: textStyle),
                    Text("VC: ${value!.vc}", style: textStyle),
                  ],
                ),
              ],
              const SizedBox(width: 24),
              SizedBox(
                width: 60,
                height: 60,
                child: _working
                    ? OutlinedButton(
                        style: ButtonStyle(
                          backgroundColor: MaterialStatePropertyAll(
                            Colors.red.withOpacity(.08),
                          ),
                          shape: MaterialStatePropertyAll(
                            RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(30),
                            ),
                          ),
                        ),
                        onPressed: () async {
                          await worker.disconnect();
                          setState(() {
                            _working = false;
                          });
                        },
                        child: const Text(
                          "停止",
                          style: TextStyle(color: Colors.red),
                        ),
                      )
                    : OutlinedButton(
                        style: ButtonStyle(
                          backgroundColor: MaterialStatePropertyAll(
                            Theme.of(context).primaryColor.withOpacity(.08),
                          ),
                          shape: MaterialStatePropertyAll(
                            RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(30),
                            ),
                          ),
                        ),
                        onPressed: () async {
                          await worker.connect();
                          worker.autoTest();
                          setState(() {
                            _working = true;
                          });
                        },
                        child: const Text("开始"),
                      ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
