import '../../models/ble_device.dart';

enum DeviceSyncFailureStage {
  permission,
  bluetooth,
  connection,
  measurement,
  upload,
}

class DeviceSyncFailure implements Exception {
  final DeviceSyncFailureStage stage;
  final String? detail;

  const DeviceSyncFailure(this.stage, [this.detail]);
}

String? deviceSyncAvailabilityLabel(BleDeviceType type) {
  return type == BleDeviceType.bloodPressure ? null : '暂不支持自动同步';
}

String deviceSyncErrorMessage(DeviceSyncFailure failure) {
  final detail = failure.detail?.trim();
  return switch (failure.stage) {
    DeviceSyncFailureStage.permission => '请开启蓝牙权限后再同步设备',
    DeviceSyncFailureStage.bluetooth => '蓝牙未开启，请开启后重试',
    DeviceSyncFailureStage.connection =>
      detail?.isNotEmpty == true ? '设备连接失败：$detail' : '设备连接失败，请靠近设备后重试',
    DeviceSyncFailureStage.measurement => '已连接设备，但本次未收到测量数据',
    DeviceSyncFailureStage.upload =>
      detail?.isNotEmpty == true ? '数据上传失败：$detail' : '数据已读取，但上传失败，请检查网络后重试',
  };
}
