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/bldg_loc.dart';
import 'package:unit2/model/passo/general_description.dart';
import 'package:unit2/model/passo/land_ref.dart';
import 'package:unit2/model/passo/property_info.dart';
import 'package:unit2/model/passo/structural_materials_ii.dart';
import 'package:unit2/sevices/passo/building/general_description_services.dart';
import 'package:unit2/sevices/passo/building/property_info_services.dart';
import 'package:http/http.dart' as http;

import 'package:unit2/sevices/passo/building/structural_material_services.dart';

part 'property_info_event.dart';
part 'property_info_state.dart';

class PropertyInfoBloc extends Bloc<PropertyInfoEvent, PropertyInfoState> {
  PropertyInfoBloc() : super(PropertyInfoLoading()) {
    on<LoadPropertyInfo>((event, emit) async {
      emit(PropertyInfoLoading());
      try {
        final property_info = await PropertyInfoService.instance.fetch();
        emit(PropertyInfoLoaded(property_info));
      } catch (e) {
        emit(PropertyInfoErrorState(e.toString()));
      }
    });
    on<AddPropertyInfo>((event, emit) async {
      final state = this.state;
      try {
        http.Response response =
            (await PropertyInfoService.instance.add(event.property_info))!;
        print(response.body);

        if (response.statusCode == 201) {
          var jsonResponse = jsonDecode(response.body);
          final tempID = await SharedPreferences.getInstance();
          print(jsonResponse['data']);
          await tempID.setInt('tempid', jsonResponse['data']['id'] + 1);
          final faas = await PropertyInfoService.instance.fetch();
          emit(PropertyInfoLoaded(faas));
        }
      } catch (e) {
        emit(PropertyInfoErrorState(e.toString()));
      }
    });
    on<UpdatPropertyInfo>(((event, emit) async {
      final state = this.state;
      try {
        http.Response response = (await PropertyInfoService.instance
            .update(event.property_info, event.property_info.id))!;
        print('property_info');
        print(response.body);
      } catch (e) {
        emit(PropertyInfoErrorState(e.toString()));
      }
    }));
    on<UpdateBldgLoc>(((event, emit) async {
      final state = this.state;
      try {
        http.Response response = (await PropertyInfoService.instance
            .updateBldg(event.bldg_loc, event.bldg_loc.id))!;
        print('bldgLoc');
        print(response.statusCode);
      } catch (e) {
        emit(PropertyInfoErrorState(e.toString()));
      }
    }));
    on<UpdateLandRef>(
      (event, emit) async {
        final state = this.state;
        try {
          http.Response response = (await PropertyInfoService.instance
              .updateLandRef(event.land_ref, event.land_ref.id))!;
          print('landref');
          print(response.body);
        } catch (e) {
          emit(PropertyInfoErrorState(e.toString()));
        }
      },
    );

    on<UpdateGeneralDesc>((event, emit) async {
      final state = this.state;
      try {
        http.Response response = (await PropertyInfoService.instance
            .updateGenDesc(event.gen_desc, event.gen_desc.id))!;
        print('genDesc');
        print(response.body);
      } catch (e) {
        emit(PropertyInfoErrorState(e.toString()));
      }
    });

    on<UpdateStrucMaterials>((event, emit) async {
      final state = this.state;
      try {
        final tempID = await SharedPreferences.getInstance();
        print(tempID.getInt('tempid')! - 1);
        http.Response response = (await StrucMaterialServices.instance
            .update(event.data, event.data.id))!;
        print('struc Material');
        print(response.body);
      } catch (e) {
        emit(PropertyInfoErrorState(e.toString()));
      }
    });
  }
}
