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

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

part 'class_components_admin_event.dart';
part 'class_components_admin_state.dart';

class ClassComponentsAdminBloc
    extends Bloc<ClassComponentsAdminEvent, ClassComponentsAdminState> {
  ClassComponentsAdminBloc() : super(ClassComponentsAdminInitial()) {
    List<ClassComponentsOffline> classes = [];
    on<LoadClassComponents>((event, emit) async {
      classes = await SQLServices.instance.readAllClassComponents();

      emit(ClassComponentsAdminLoaded(classes: classes));
    });
    on<AddClassComponents>((event, emit) async {
      await SQLServices.instance.createClassComponents(
        ClassComponentsOffline(
            componentName: event.componentName,
            minBaseUnitvalPercent: event.minBaseUnitvalPercent,
            maxBaseUnitvalPercent: event.maxBaseUnitvalPercent,
            minUnitvalSqrmtr: event.minUnitvalSqrmtr,
            maxUnitvalSqrmtr: event.maxUnitvalSqrmtr,
            minAddBaseunitval: event.minAddBaseunitval,
            maxAddBaseunitval: event.maxAddBaseunitval,
            minDeductBaserate: event.minDeductBaserate,
            maxDeductBaserate: event.maxDeductBaserate,
            minLinearMeter: event.minLinearMeter,
            maxLinearMeter: event.maxLinearMeter,
            minSpacing: event.minSpacing,
            maxSpacing: event.maxSpacing,
            roughFinish: event.roughFinish,
            highFinish: event.highFinish,
            withoutBucc: event.withoutBucc),
      );
    });
  }
}
