import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:hl_lieferservice/widget/operations/bloc/operation_event.dart';
import 'package:hl_lieferservice/widget/operations/bloc/operation_state.dart';

class OperationBloc extends Bloc<OperationEvent, OperationState> {
  /// Counts how many in-flight mutations want to show the blocking overlay.
  /// Allows multiple parallel mutations without one prematurely closing the
  /// overlay before the others complete.
  int _inFlightCount = 0;

  /// When the current overlay session began (set when [_inFlightCount]
  /// transitions 0 → 1). Used to enforce [_minimumDisplayDuration].
  DateTime? _overlayStartedAt;

  /// Minimum time the overlay stays visible, even if the underlying request
  /// completes faster. Prevents a "did anything happen?" UX where a sub-100 ms
  /// roundtrip flashes the overlay for one frame.
  static const Duration _minimumDisplayDuration = Duration(milliseconds: 500);

  OperationBloc() : super(OperationIdle()) {
    on<StartOperation>(_startOperation);
    on<FailOperation>(_failOperation);
    on<FinishOperation>(_finishOperation);
  }

  Future<void> _startOperation(
    StartOperation event,
    Emitter<OperationState> emit,
  ) async {
    if (_inFlightCount == 0) {
      _overlayStartedAt = DateTime.now();
    }
    _inFlightCount += 1;
    emit(OperationInProgress(message: event.message));
  }

  Future<void> _finishOperation(
    FinishOperation event,
    Emitter<OperationState> emit,
  ) async {
    _inFlightCount = (_inFlightCount - 1).clamp(0, 1 << 30);

    // Spinner-Mindestdauer einhalten, BEVOR wir ihn schließen — auch wenn eine
    // Erfolgsmeldung folgt. Sonst blitzt der Spinner bei schnellen Requests nur
    // einen Frame lang auf (genau der Grund für den vorherigen „kein Spinner").
    if (_inFlightCount == 0) {
      await _awaitMinimumOverlayDuration();
    }

    if (event.message != null) {
      emit(OperationFinished(message: event.message));
      await Future.delayed(const Duration(seconds: 5));
    }

    if (_inFlightCount > 0) {
      emit(OperationInProgress());
    } else {
      _overlayStartedAt = null;
      emit(OperationIdle());
    }
  }

  Future<void> _failOperation(
    FailOperation event,
    Emitter<OperationState> emit,
  ) async {
    _inFlightCount = (_inFlightCount - 1).clamp(0, 1 << 30);

    // Auch im Fehlerfall den Spinner mindestens kurz zeigen, bevor die
    // Fehler-SnackBar erscheint.
    if (_inFlightCount == 0) {
      await _awaitMinimumOverlayDuration();
    }

    emit(OperationFailed(message: event.message));
    await Future.delayed(const Duration(seconds: 5));

    if (_inFlightCount > 0) {
      emit(OperationInProgress());
    } else {
      _overlayStartedAt = null;
      emit(OperationIdle());
    }
  }

  Future<void> _awaitMinimumOverlayDuration() async {
    final startedAt = _overlayStartedAt;
    if (startedAt == null) return;
    final elapsed = DateTime.now().difference(startedAt);
    if (elapsed < _minimumDisplayDuration) {
      await Future.delayed(_minimumDisplayDuration - elapsed);
    }
  }
}
