import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:unit2/model/passo/land_ext.dart';

import '../../../../../sevices/offline/offline_passo/admin/sql_services/sql_services.dart';

part 'land_property_signature_event.dart';
part 'land_property_signature_state.dart';

class LandPropertySignatureBloc
    extends Bloc<LandPropertySignatureEvent, LandPropertySignatureState> {
  LandPropertySignatureBloc() : super(LandPropertySignatureInitial()) {
    List<LandExt> landExt = [];
    on<AddLandPropertySignature>((event, emit) async {
      try {
        print(event.landapprDetailsId);
        await SQLServices.instance.createLandPropertySignatories(
          LandExt(
            id: event.id,
            landapprDetailsId: event.landapprDetailsId,
            assessedById: event.assessedById,
            assessedByName: event.assessedByName,
            dateCreated: event.dateCreated,
            dateModified: event.dateModified,
            taxable: event.taxable,
            exempt: event.exempt,
            qtr: event.qtr,
            yr: event.yr,
            appraisedbyName: event.appraisedbyName,
            appraisedbyDate: event.appraisedbyDate,
            recommendapprName: event.recommendapprName,
            recommendapprDate: event.recommendapprDate,
            approvedbyName: event.approvedbyName,
            approvedbyDate: event.approvedbyDate,
            memoranda: event.memoranda,
            swornstatementNo: event.swornstatementNo,
            dateReceived: event.dateReceived,
            entryDateAssessment: event.entryDateAssessment,
            entryDateBy: event.entryDateBy,
          ),
        );
      } catch (e) {
        emit(LandPropertySignatureInfoErrorState(errorMessage: 'error'));
        print('Error: $e');
        // You might want to throw or log the error, or take other appropriate actions

        // If you want to rethrow the error, uncomment the following line
        // throw e;
      }
    });

    on<UpdateLandPropertySignatureInfo>((event, emit) async {
      await SQLServices.instance
          .updateLandPropertySignature(event.id, event.landPropertySignature);
    });

    // on<LoadLandPropertySignature>((event, emit) async {
    //   todos = await SQLServices.instance.getLandPropertySignature();
    //   emit(LandPropertySignatureLoaded(landPropertySignature: todos));
    // });

    on<LoadSpecificLandPropertySignatureInfo>((event, emit) async {
      List<Map<String, dynamic>> result =
          await SQLServices.instance.getLandPropertySignature(event.id);

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

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

        print('location test result');
        print(firstLandExt);
        emit(LoadSpecificLandPropertySignature(
            landPropertySignature: firstLandExt));
      } else {
        print('No data found.');
      }
    });

    // on<DeleteLandPropertySignature>((event, emit) async {
    //   await SQLServices.instance.de(id: event.id);
    //   add(const LoadLandPropertySignature());
    // });
  }
}
