import 'package:be_happy/core/result.dart';
import 'package:be_happy/domain/entities/point.dart';
import 'package:be_happy/domain/usecase/get_scooter_order_route_history_usecase.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

import '../event/route_event.dart';
import '../state/route_state.dart';

class RouteBloc extends Bloc<RouteEvent, RouteState> {
  final GetScooterOrderRouteHistoryUsecase getRouteUseCase;

  RouteBloc({required this.getRouteUseCase}) : super(RouteInitial()) {
    on<FetchRouteEvent>((event, emit) async {
      emit(RouteLoading());
      try {
        final result = await getRouteUseCase(event.orderId);

        if (result is Success<List<Point>>) {

          final List<Point> points = result.data ?? [];

          emit(RouteLoaded(points));

        }

      } catch (e) {
        emit(RouteError("Ошибка загрузки пути"));
      }
    });
  }
}