import 'package:app_gaslieferung/repository/tour_repository.dart';
import 'package:bloc/bloc.dart';
import 'package:flutter/cupertino.dart';

import 'tour_event.dart';
import 'tour_state.dart';

class TourBloc extends Bloc<TourEvent, TourState> {
  TourRepository tour;

  TourBloc({required this.tour}) : super(TourInitial()) {
    on<TourLoadEvent>(_onTourLoadEvent);
  }

  void _onTourLoadEvent(TourLoadEvent event, Emitter<TourState> emit) async {
    emit(TourLoading());
    try {
      final tour = await this.tour.getTour(event.sessionId, event.carId);
      emit(TourLoaded(tour: tour));
    } catch (e, st) {
      debugPrint(st.toString());
      emit(TourLoadingFailed(message: e.toString()));
    }
  }
}
