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

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

part 'barangay_admin_event.dart';
part 'barangay_admin_state.dart';

class BarangayAdminBloc extends Bloc<BarangayAdminEvent, BarangayAdminState> {
  BarangayAdminBloc() : super(BarangayAdminInitial()) {
    List<Brgy> brgy = [];
    on<LoadBarangay>((event, emit) async {
      brgy = await SQLServices.instance.readAllBarangay();

      emit(BarangayLoaded(brgy: brgy));
    });
    on<LoadBarangayInMunicipality>((event, emit) async {
      brgy = await SQLServices.instance
          .readBrgyInSelectedMunicipality(event.cityCode);

      emit(BarangayLoaded(brgy: brgy));
    });
    on<AddBarangay>((event, emit) async {
      await SQLServices.instance.createBarangay(
        Brgy(
            id: event.id,
            barangayId: event.barangayId,
            barangayCode: event.barangayCode,
            cityCode: event.cityCode,
            barangayDescription: event.barangayDescription),
      );
    });
  }
}
