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

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

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

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

class _ICReaderCardState extends State<ICReaderCard> {
  late final ICReaderDeviceWorker worker = ICReaderDeviceWorker(
    mac: widget.mac,
    model: widget.model,
  );
  ICReaderExamData? value;
  bool _working = false;

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

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

    super.dispose();
  }

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

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

  @override
  Widget build(BuildContext context) {
    return Card(
      elevation: 4,
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
      child: Container(
        alignment: Alignment.center,
        width: 360,
        height: 160,
        child: SizedBox(
          height: 160,
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.end,
            children: [
              if (value != null)
                SizedBox(
                  width: 280,
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      const SizedBox(height: 4),
                      Text(value!.name),
                      Text(value!.gender),
                      Text(value!.nation),
                      Text(value!.birthday),
                      Text(value!.cardNo),
                      Text(value!.address, overflow: TextOverflow.ellipsis),
                    ],
                  ),
                ),
              const SizedBox(width: 8),
              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(() {
                            value = null;
                            _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();
                          setState(() {
                            _working = true;
                          });
                        },
                        child: const Text("开始"),
                      ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
