import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:equatable/equatable.dart'; import '../../../domain/entities/tour.dart'; import '../../../domain/entities/logistic_object.dart'; import '../../../domain/repositories/tour_repository.dart'; import '../../../core/errors/failures.dart'; part 'tour_event.dart'; part 'tour_state.dart'; class TourBloc extends Bloc { final TourRepository repository; TourBloc({required this.repository}) : super(TourInitial()) { on(_onLoadTours); on(_onSelectTour); on(_onCompleteTour); on(_onSyncData); on(_onRefreshTours); on(_onLoadTourDetails); } Future _onLoadTours(LoadTours event, Emitter emit) async { emit(TourLoading()); final result = await repository.getTours(); result.fold( (failure) => emit(TourError(message: _mapFailureToMessage(failure))), (tours) { final openTours = tours.where((t) => t.state < 2).toList(); final completedTours = tours.where((t) => t.state == 1).toList(); emit(ToursLoaded( tours: openTours, completedTours: completedTours, allTours: tours, )); }, ); } Future _onSelectTour(SelectTour event, Emitter emit) async { final currentState = state; if (currentState is ToursLoaded) { emit(currentState.copyWith(selectedTour: event.tour)); } } Future _onCompleteTour(CompleteTour event, Emitter emit) async { final currentState = state; if (currentState is ToursLoaded) { emit(TourLoading()); final result = await repository.completeTour(event.tourId); result.fold( (failure) => emit(TourError(message: _mapFailureToMessage(failure))), (_) => add(const LoadTours()), ); } } Future _onSyncData(SyncData event, Emitter emit) async { final currentState = state; if (currentState is ToursLoaded) { emit(currentState.copyWith(isSyncing: true)); final result = await repository.syncData(); result.fold( (failure) { emit(currentState.copyWith(isSyncing: false)); emit(SyncError(message: _mapFailureToMessage(failure))); }, (_) { emit(currentState.copyWith(isSyncing: false)); add(const LoadTours()); }, ); } } Future _onRefreshTours(RefreshTours event, Emitter emit) async { add(const LoadTours()); } Future _onLoadTourDetails(LoadTourDetails event, Emitter emit) async { final currentState = state; if (currentState is ToursLoaded) { emit(currentState.copyWith(isLoadingDetails: true)); final objectsResult = await repository.getObjectsByTour(event.tourId); final statsResult = await repository.getTourStatistics(event.tourId); objectsResult.fold( (failure) => emit(TourError(message: _mapFailureToMessage(failure))), (objects) { statsResult.fold( (failure) => emit(TourError(message: _mapFailureToMessage(failure))), (statistics) { emit(currentState.copyWith( selectedTourObjects: objects, selectedTourStats: statistics, isLoadingDetails: false, )); }, ); }, ); } } String _mapFailureToMessage(Failure failure) { return switch (failure) { ServerFailure _ => 'Serverfehler: ${failure.message}', NetworkFailure _ => 'Netzwerkfehler. Bitte überprüfen Sie Ihre Internetverbindung.', CacheFailure _ => 'Cachefehler: ${failure.message}', NotFoundFailure _ => 'Daten nicht gefunden', UnauthorizedFailure _ => 'Nicht autorisiert. Bitte melden Sie sich erneut an.', _ => 'Ein unerwarteter Fehler ist aufgetreten', }; } }