import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';

import '../../../../../model/passo/type_of_location.dart';
import '../../../../../sevices/offline/offline_passo/admin/sql_services/sql_services.dart';

part 'type_of_location_event.dart';
part 'type_of_location_state.dart';

class TypeOfLocationBloc
    extends Bloc<TypeOfLocationEvent, TypeOfLocationState> {
  TypeOfLocationBloc() : super(TypeOfLocationInitial()) {
    List<TypeOfLocation> typeOfLocation = [];
    on<LoadTypeOfLocation>((event, emit) async {
      typeOfLocation = await SQLServices.instance.readAllTypeOfLocation();

      emit(TypeOfLocationLoaded(typeOfLocation: typeOfLocation));
    });
    on<AddTypeOfLocation>((event, emit) async {
      await SQLServices.instance.createTypeOfLocation(TypeOfLocation(
          id: event.id,
          distanceKm: event.distanceKm,
          allRoadTypes: event.allRoadTypes,
          localTradingCenter: event.localTradingCenter));
    });
  }
}
