import 'package:vnote_device_plugin/consts/status.dart';
import 'package:vnote_device_plugin/consts/types.dart';
import 'package:vnote_device_plugin/devices/base.dart';
import 'package:vnote_device_plugin/events/event_type.dart';
import 'package:vnote_device_plugin/models/exams/ic_reader.dart';

/// 人证阅读器
class ICReaderDeviceWorker extends DeviceWorkerBase {
  ICReaderDeviceWorker({
    required super.model,
    required super.mac,
  }) : super(type: DeviceTypes.IC_READER);

  @override
  init() {
    super.init();

    injectStatusCallback(VDeviceEventStatus.EXAM_ICREADER_FAIL, _onFail);
    injectStatusCallback(VDeviceEventStatus.EXAM_ICREADER_SUCCESS, _onSuccess);
  }

  /// 读取成功事件
  final successEvent = FEventHandler<ICReaderExamData>();

  /// 读取错误事件
  final errorEvent = FEventHandler<String>();

  void _onSuccess(Map<String, dynamic>? data) {
    final ICReaderExamData examData = ICReaderExamData.fromEventData(data!);
    log("Result; CardNo: ${examData.cardNo}, Name: ${examData.name}");
    successEvent.emit(this, examData);
  }

  void _onFail(Map<String, dynamic>? data) {
    final String msg = data!["MSG"];
    log("Fail: $msg");
    errorEvent.emit(this, msg);
  }
}
