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

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

part 'land_property_location_event.dart';
part 'land_property_location_state.dart';

class LandPropertyLocationBloc
    extends Bloc<LandPropertyLocationEvent, LandPropertyLocationState> {
  LandPropertyLocationBloc() : super(LandPropertyLocationInitial()) {
    List<LandPropertyLoc> todos = [];
    on<AddLandPropertyLocation>((event, emit) async {
      await SQLServices.instance.createLandLocation(LandPropertyLoc(
        id: event.id,
        landapprDetailsId: event.landapprDetailsId,
        assessedById: event.assessedById,
        assessedByName: event.assessedByName,
        dateCreated: event.dateCreated,
        dateModified: event.dateModified,
        street: event.street,
        municipality: event.municipality,
        barangay: event.barangay,
        province: event.province,
      ));
    });
    on<LoadLandPropertyLocation>((event, emit) async {
      // todos = await SQLServices.instance.getLandPropertyOwner();
      // emit(LandPropertyOwnerLoaded(landPropertyOwner: todos));
    });

    on<FetchSingleLandPropertyLocation>((event, emit) async {
      List<Map<String, dynamic>> result =
          await SQLServices.instance.getLandPropertyLocation(event.id);

      if (result.isNotEmpty) {
        List<LandPropertyLoc> landpropertylocationList =
            result.map((map) => LandPropertyLoc.fromJson2(map)).toList();

        // Choose a specific element from landpropertylocationList
        LandPropertyLoc firstLandPropertyLocation = landpropertylocationList
            .first; // You can change this to select a specific item

        print('landpropertylocation test result');
        print(firstLandPropertyLocation);
        emit(SpecificLandPropertyLocationLoaded(
            landpropertylocation: firstLandPropertyLocation));
      } else {
        print('No data found.');
      }
    });
    on<UpdateLandPropertyLocation>((event, emit) async {
      await SQLServices.instance
          .updateLandPropertyLocation(event.id, event.landPropertyLocation);
    });
  }
}
