import 'dart:convert';

import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:unit2/model/passo/land_property_loc.dart';
import 'package:http/http.dart' as http;
import 'package:unit2/sevices/passo/land/land_location.dart';
part 'land_location_edit_event.dart';
part 'land_location_edit_state.dart';

class LandLocationEditBloc
    extends Bloc<LandLocationEditEvent, LandLocationEditState> {
  LandPropertyLoc globalLandLocationEdit;
  LandLocationEditBloc()
      : globalLandLocationEdit = LandPropertyLoc(),
        super(LandLocationEditInitial()) {
    on<LoadLandLocationEdit>((event, emit) async {
      emit(LandLocationEditLoading());
      try {
        final tempID = await SharedPreferences.getInstance();

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

        emit(LandLocationEditLoaded(assessments));
      } catch (e) {
        emit(LandLocationEditErrorState(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(globalLandLocationEdit));
    //   }
    // });
    on<UpdateLandLocationEdit>((event, emit) async {
      http.Response response = (await LandLocationService.instance
          .updateEdit(event.land_loc_edit, event.id))!;
      print('landloc edit');
      print(response.statusCode);
      print(response.body);
      // if (response.statusCode == 201) {
      //   final faas = await PropertyInfoRepository.getUsers();
      //   emit(FaasLoaded(faas));
      // }
    });
  }
}
