import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:unit2/model/passo/land_ext.dart';
import 'package:unit2/sevices/passo/land/land_ext.dart';
import 'package:http/http.dart' as http;
part 'land_ext_edit_event.dart';
part 'land_ext_edit_state.dart';

class LandExtEditBloc extends Bloc<LandExtEditEvent, LandExtEditState> {
  LandExtEditBloc() : super(LandExtEditInitial()) {
    on<LoadLandExtEdit>((event, emit) async {
      emit(LandExtEditLoading());
      try {
        final tempID = await SharedPreferences.getInstance();

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

        emit(LandExtEditLoaded(assessments));
      } catch (e) {
        print(e.toString());
        emit(LandExtEditErrorState(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<UpdateLandExtEdit>((event, emit) async {
      http.Response response = (await LandExtServices.instance
          .updateEdit(event.land_ext_edit, event.id))!;
      print('landloc edit');
      print(response.statusCode);
      print(response.body);
      // if (response.statusCode == 201) {
      //   final faas = await PropertyInfoRepository.getUsers();
      //   emit(FaasLoaded(faas));
      // }
    });
  }
}
