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

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

part 'land_property_boundaries_event.dart';
part 'land_property_boundaries_state.dart';

class LandPropertyBoundariesBloc
    extends Bloc<LandPropertyBoundariesEvent, LandPropertyBoundariesState> {
  LandPropertyBoundariesBloc() : super(LandPropertyBoundariesInitial()) {
    List<LandPropertyBoundaries> todos = [];
    on<AddLandPropertyBoundaries>((event, emit) async {
      await SQLServices.instance.createLandPropertyBoundaries(
          LandPropertyBoundaries(
              id: event.id,
              landapprDetailsId: event.landapprDetailsId,
              assessedById: event.assessedById,
              assessedByName: event.assessedByName,
              dateCreated: event.dateCreated,
              dateModified: event.dateModified,
              north: event.north,
              east: event.east,
              south: event.south,
              west: event.west,
              sketch: event.sketch));
    });
    on<FetchSingleLandPropertyBoundaries>((event, emit) async {
      List<Map<String, dynamic>> result =
          await SQLServices.instance.getLandPropertyBoundaries(event.id);

      if (result.isNotEmpty) {
        List<LandPropertyBoundaries> landpropertyboundariesList =
            result.map((map) => LandPropertyBoundaries.fromJson(map)).toList();

        // Choose a specific element from landpropertyboundariesList
        LandPropertyBoundaries firstLandPropertyBoundaries =
            landpropertyboundariesList
                .first; // You can change this to select a specific item

        print('landpropertyboundaries test result');
        print(firstLandPropertyBoundaries);
        emit(SpecificLandPropertyBoundariesLoaded(
            landpropertyboundaries: firstLandPropertyBoundaries));
      } else {
        print('No data found.');
      }
    });
    on<UpdateLandPropertyBoundaries>((event, emit) async {
      await SQLServices.instance
          .updateLandPropertyBoundaries(event.id, event.landPropertyBoundaries);
    });
  }
}
