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

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

part 'land_subclassification_event.dart';
part 'land_subclassification_state.dart';

class LandSubclassificationBloc
    extends Bloc<LandSubclassificationEvent, LandSubclassificationState> {
  LandSubclassificationBloc() : super(LandSubclassificationInitial()) {
    List<LandSubClassification> landSubClassification = [];
    on<LoadLandSubClassification>((event, emit) async {
      landSubClassification =
          await SQLServices.instance.readAllLandSubClassification();

      emit(LandSubClassificationLoaded(
          landSubClassification: landSubClassification));
    });
    on<LoadSpecificLandSubClassification>((event, emit) async {
      landSubClassification = await SQLServices.instance
          .readSpecificLandSubClassification(event.cityCode, event.classCode);

      emit(LandSubClassificationLoaded(
          landSubClassification: landSubClassification));
    });
    on<AddLandSubClassification>((event, emit) async {
      await SQLServices.instance.createLandSubClassification(
          LandSubClassification(
              id: event.id,
              classificationId: event.classificationId,
              cityCode: event.cityCode,
              subclassCode: event.subclassCode,
              subclassDescription: event.subclassDescription,
              baseUnitMarketval: event.baseUnitMarketval));
    });
  }
}
