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

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

part 'trees_improvements_event.dart';
part 'trees_improvements_state.dart';

class TreesImprovementsBloc
    extends Bloc<TreesImprovementsEvent, TreesImprovementsState> {
  TreesImprovementsBloc() : super(TreesImprovementsInitial()) {
    List<TreesImprovements> treesImprovements = [];
    on<LoadTreesImprovements>((event, emit) async {
      treesImprovements = await SQLServices.instance.readAllTreesImprovements();

      emit(TreesImprovementsLoaded(treesImprovements: treesImprovements));
    });
    on<AddTreesImprovements>((event, emit) async {
      await SQLServices.instance.createTreesImprovements(TreesImprovements(
          id: event.id,
          improvement: event.improvement,
          pricePerTree: event.pricePerTree,
          subclassCode: event.subclassCode));
    });
  }
}
