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> {
  OperationBloc() : super(OperationIdle()) {
    on<LoadOperation>(_loadOperation);
    on<FailOperation>(_failOperation);
    on<FinishOperation>(_finishOperation);
  }

  Future<void> _loadOperation(LoadOperation event, Emitter<OperationState> emit) async {
    emit(OperationLoading());
  }

  Future<void> _failOperation(FailOperation event, Emitter<OperationState> emit) async {
    emit(OperationFailed(message: event.message));

    await Future.delayed(Duration(seconds: 5));

    emit(OperationIdle());
  }

  Future<void> _finishOperation(FinishOperation event, Emitter<OperationState> emit) async {
    emit(OperationFinished(message: event.message));

    await Future.delayed(Duration(seconds: 5));

    emit(OperationIdle());
  }
}