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/nibp.dart';
import 'package:vnote_device_plugin/vnote_device_plugin_platform_interface.dart';

/// 血压
class NibpDeviceWorker extends DeviceWorkerBase {
  NibpDeviceWorker({
    required super.model,
    required super.mac,
  }) : super(type: DeviceTypes.NIBP);

  /// 结果更新事件
  final resultUpdateEvent = FEventHandler<NibpExamValue>();

  /// 实时数据更新事件
  final liveUpdateEvent = FEventHandler<int>();

  /// 测量错误事件
  final errorEvent = FEventHandler<String>();

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

    // injectStatusCallback(
    //     VDeviceEventStatus.EXAM_NIBP_COMMAND_SUCCESS, _onValue);
    injectStatusCallback(VDeviceEventStatus.EXAM_NIBP_BUSY_ERROR, _onBusyError);
    // injectStatusCallback(VDeviceEventStatus.EXAM_NIBP_COMMAND_ERROR, _onValue);
    // injectStatusCallback(VDeviceEventStatus.EXAM_NIBP_GETSYS_SUCCESS, _onValue);

    injectStatusCallback(VDeviceEventStatus.EXAM_NIBP_LIVE, _onLive);
    injectStatusCallback(VDeviceEventStatus.EXAM_NIBP_RESULT, _onResult);
    injectStatusCallback(VDeviceEventStatus.EXAM_NIBP_ERROR, _onError);

    // injectStatusCallback(VDeviceEventStatus.EXAM_NIBP_DELETE_SUCCESS, _onValue);
    // injectStatusCallback(VDeviceEventStatus.EXAM_NIBP_SORTNO_EMPTY, _onValue);
    // injectStatusCallback(VDeviceEventStatus.EXAM_NIBP_SORTNO_RETURN, _onValue);

    injectStatusCallback(
        VDeviceEventStatus.EXAM_NIBP_NO_RESULT, _onNoResultError);
  }

  Future<void> queryAll() async {
    await VnoteDevicePluginPlatform.instance.callAction(
      "QUERY_USER_ALL",
      {
        "MODEL": model,
        "MAC": mac,
      },
    );
  }

  void _onLive(Map<String, dynamic>? data) {
    final NibpExamData examData = NibpExamData.fromEventData(data!);
    log("Value update: ${examData.pressure}");
    liveUpdateEvent.emit(this, examData.pressure);
  }

  void _onResult(Map<String, dynamic>? data) {
    final NibpExamData examData = NibpExamData.fromEventData(data!);
    log("Result; Sbp: ${examData.systolicPressure}, Dbp: ${examData.diastolicPressure}, Pulse: ${examData.pulse}");
    resultUpdateEvent.emit(this, examData);
  }

  void _onError(Map<String, dynamic>? data) {
    final String errMsg = data?["MSG"] ?? "";
    final String msg = "${VDeviceEventStatus.EXAM_NIBP_ERROR}|Error|$errMsg";
    log("Error: $msg");
    errorEvent.emit(this, msg);
  }

  void _onBusyError(Map<String, dynamic>? data) {
    const String msg =
        "${VDeviceEventStatus.EXAM_NIBP_BUSY_ERROR}|ProbeFalloff";
    log("Busy Error: $msg");
    errorEvent.emit(this, msg);
  }

  void _onNoResultError(Map<String, dynamic>? data) {
    const String msg = "${VDeviceEventStatus.EXAM_NIBP_NO_RESULT}|NoResult";
    log("NoResult Error: $msg");
    errorEvent.emit(this, msg);
  }
}
