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

/// TODO 还未完成，先做调试
/// 心电
class TwelveHeartDeviceWorker extends DeviceWorkerBase {
  TwelveHeartDeviceWorker({
    required super.model,
    required super.mac,
  }) : super(type: DeviceTypes.TWELVEHEART);

  @override
  init() {
    super.init();
    injectStatusCallback(
        VDeviceEventStatus.EXAM_HEART_TWELVE_RESULT, _onResult);
    injectStatusCallback(
        VDeviceEventStatus.EXAM_HEART_TWELVE_START_SAVE_RESULT, _onSave);
    injectStatusCallback(
        VDeviceEventStatus.EXAM_HEART_TWELVE_START_SAVE_RESULT_SUCCESS,
        _onStartSaveSuccess);
    injectStatusCallback(
        VDeviceEventStatus.EXAM_HEART_TWELVE_START_SAVE_RESULT_PROGRESS,
        _onSaveProgress);
    injectStatusCallback(
        VDeviceEventStatus.EXAM_HEART_TWELVE_STABLE, _onStable);
    injectStatusCallback(
        VDeviceEventStatus.EXAM_HEART_TWELVE_CONCLUSION, _onConclusion);
  }

  // /// 心率数据更新事件
  // final hrValueUpdateEvent = FEventHandler<int>();

  // /// ECG数据更新事件
  // final ecgValueUpdateEvent = FEventHandler<List<int>>();

  /// 接收结果事件
  final resultReceivedEvent = FEventHandler<TwelveHeartExamData>();

  /// 开始保存心电测量值事件
  final startSaveResultEvent = FEventHandler<bool>();

  /// 保存心电测量值成功事件
  final startSaveSuccessEvent = FEventHandler<bool>();

  /// 保存心电测量值进度事件
  final saveProgressEvent = FEventHandler<int>();

  /// 心电测量稳定事件
  final ecgStableEvent = FEventHandler<bool>();

  /// 心电测量结论事件
  final ecgConclusionEvent = FEventHandler<TwelveHeartExamResult>();

  /// 连接设备 连接前为啥需要搜索设备
  @override
  Future<void> connect() async {
    log("12ecg Connect");
    await VnoteDevicePluginPlatform.instance.callAction(
      "BLUETOOTH_CONNECT",
      {
        "MODEL": model,
        "MAC": mac,
      },
    );
  }

  void _onResult(Map<String, dynamic>? data) {
    final TwelveHeartExamData examData =
        TwelveHeartExamData.fromEventData(data!);
    log("Result; HR: ${examData.heartRate}, Analysis: ");
    resultReceivedEvent.emit(this, examData);
  }

  void _onSave(Map<String, dynamic>? data) {
    startSaveResultEvent.emit(this, true);
  }

  void _onStartSaveSuccess(Map<String, dynamic>? data) {
    startSaveSuccessEvent.emit(this, true);
  }

  /// TODO 需要添加
  void _onSaveProgress(Map<String, dynamic>? data) {
    saveProgressEvent.emit(this, 1);
  }

  void _onStable(Map<String, dynamic>? data) {
    ecgStableEvent.emit(this, true);
  }

  void _onConclusion(Map<String, dynamic>? data) {
    final TwelveHeartExamResult examData =
        TwelveHeartExamResult.fromEventData(data!);
    log("Result; HR: ${examData.heartRate}, Analysis: ");
    ecgConclusionEvent.emit(this, examData);
  }
}
