import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:unit2/model/passo/land_property_boundaries.dart';
import 'package:http/http.dart' as http;
import 'package:unit2/sevices/passo/land/land_boundaries.dart';
part 'land_boundaries_edit_event.dart';
part 'land_boundaries_edit_state.dart';

class LandBoundariesEditBloc
    extends Bloc<LandBoundariesEditEvent, LandBoundariesEditState> {
  LandBoundariesEditBloc() : super(LandBoundariesEditInitial()) {
    on<LoadLandBoundariesEdit>((event, emit) async {
      emit(LandBoundariesEditLoading());
      try {
        final tempID = await SharedPreferences.getInstance();

        final assessments =
            await LandBoundariesService.instance.fetchEdit(event.id);

        emit(LandBoundariesEditLoaded(assessments));
      } catch (e) {
        emit(LandBoundariesEditErrorState(e.toString()));
      }
    });
    // on<AddLandLocationEdit>((event, emit) async {
    //   http.Response response = (await LandLocationService.instance
    //       .addEdit(event.land_loc_edit))!;
    //   print('Assessment');
    //   print(response.statusCode);
    //   print(response.body);
    //   if (response.statusCode == 201) {
    //     var jsonResponse = jsonDecode(response.body);
    //     LandPropertyLoc newAssessment =
    //         LandPropertyLoc.fromJson(jsonResponse['data']);

    //     globalLandLocationEdit = newAssessment;

    //     emit(LandLocationEditLoaded(globalLandBoundariesEdit));
    //   }
    // });
    on<UpdateLandBoundariesEdit>((event, emit) async {
      http.Response response = (await LandBoundariesService.instance
          .updateEdit(event.land_boundaries_edit, event.id))!;
      print('landloc edit');
      print(response.statusCode);
      print(response.body);
      // if (response.statusCode == 201) {
      //   final faas = await PropertyInfoRepository.getUsers();
      //   emit(FaasLoaded(faas));
      // }
    });
  }
}
