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';

/// 体温
class TempDeviceWorker extends DeviceWorkerBase {
  TempDeviceWorker({
    required super.model,
    required super.mac,
  }) : super(type: DeviceTypes.TEMP);

  /// 测量成功事件
  final successEvent = FEventHandler<double>();

  /// 测量失败
  final errorEvent = FEventHandler<String>();

  /// 温度过低
  final tempTooLowEvent = FEventHandler<double>();

  /// 温度过高
  final tempTooHighEvent = FEventHandler<double>();

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

    injectStatusCallback(VDeviceEventStatus.EXAM_TEMP_SUCCESS, _onTempSuccess);
    injectStatusCallback(VDeviceEventStatus.EXAM_TEMP_ERROR, _onTempError);
    injectStatusCallback(VDeviceEventStatus.EXAM_TEMP_TOO_LOW, _onTempTooLow);
    injectStatusCallback(VDeviceEventStatus.EXAM_TEMP_TOO_HIGH, _onTempTooHigh);
  }

  void _onTempSuccess(Map<String, dynamic>? data) {
    final double value = data!["TEMP"].toDouble();
    log("Result; TEMP: $value");
    successEvent.emit(this, value);
  }

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

  void _onTempTooLow(Map<String, dynamic>? data) {
    final double value = data!["TEMP"].toDouble();
    log("Result|TooLow; TEMP: $value");
    tempTooLowEvent.emit(this, value);
  }

  void _onTempTooHigh(Map<String, dynamic>? data) {
    final double value = data!["TEMP"].toDouble();
    log("Result|TooHigh; TEMP: $value");
    tempTooHighEvent.emit(this, value);
  }
}
