import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:unit2/model/passo/general_description.dart';
import 'package:unit2/sevices/offline/offline_passo/admin/sql_services/sql_services.dart';

import '../../../../../sevices/offline/offline_passo/building/property_owner_info_service.dart';

part 'general_description_event.dart';
part 'general_description_state.dart';

class GeneralDescriptionBloc
    extends Bloc<GeneralDescriptionEvent, GeneralDescriptionState> {
  GeneralDescriptionBloc() : super(GeneralDescriptionInitial()) {
    List<GeneralDesc> todos = [];
    on<AddGendesc>((event, emit) async {
      await SQLServices.instance.createBldgGeneralDescription(GeneralDesc(
          bldgapprDetailsId: event.bldgapprDetailsId,
          assessedById: event.assessedById,
          assessedByName: event.assessedByName,
          bldgKind: event.bldgKind,
          strucType: event.strucType,
          bldgPermit: event.bldgPermit,
          dateIssued: event.dateIssued.toString(),
          cct: event.cct,
          certCompletionIssued: event.certCompletionIssued.toString(),
          certOccupancyIssued: event.certOccupancyIssued.toString(),
          dateCompleted: event.dateCompleted.toString(),
          dateOccupied: event.dateOccupied.toString(),
          bldgAge: event.bldgAge,
          noStoreys: event.noStoreys,
          area1Stfloor: event.area1Stfloor,
          area2Ndfloor: event.area2Ndfloor,
          area3Rdfloor: event.area3Rdfloor,
          area4Thfloor: event.area4Thfloor,
          totalFloorArea: event.totalFloorArea,
          floorSketch: event.floorSketch,
          actualUse: event.actualUse,
          unitValue: event.unitValue,
          genCode: event.genCode));
    });
    on<FetchSingleGeneralDescription>((event, emit) async {
      List<Map<String, dynamic>> result =
          await SQLServices.instance.getGeneralDescription(event.id);

      if (result.isNotEmpty) {
        List<GeneralDesc> genDescList =
            result.map((map) => GeneralDesc.fromJson2(map)).toList();

        // Choose a specific element from locationList
        GeneralDesc firstGenDesc =
            genDescList.first; // You can change this to select a specific item

        print('location test result');
        print(firstGenDesc);
        emit(SpecificGeneralDescriptionLoaded(gendesc: firstGenDesc));
      } else {
        print('No data found.');
      }
    });
    on<UpdateGeneralDescription>((event, emit) async {
      await SQLServices.instance
          .updateGeneralDescription(event.id, event.gendesc);
    });
  }
}
