import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';

import '../../../../../model/passo/land_property_owner.dart';
import '../../../../../sevices/offline/offline_passo/admin/sql_services/sql_services.dart';

part 'land_property_owner_event.dart';
part 'land_property_owner_state.dart';

class LandPropertyOwnerBloc
    extends Bloc<LandPropertyOwnerEvent, LandPropertyOwnerState> {
  LandPropertyOwnerBloc() : super(LandPropertyOwnerInitial()) {
    List<LandPropertyOwner> todos = [];
    on<AddLandPropertyOwner>((event, emit) async {
      try {
        await SQLServices.instance.createLandOwner(
          LandPropertyOwner(
              id: event.id,
              assessedById: event.assessedById,
              assessedByName: event.assessedByName,
              dateCreated: event.dateCreated,
              dateModified: event.dateModified,
              transCode: event.transCode,
              tdn: event.tdn,
              pin: event.pin,
              cloaNo: event.cloaNo,
              dated: event.dated,
              surveyNo: event.surveyNo,
              lotNo: event.lotNo,
              blkNo: event.blkNo,
              owner: event.owner,
              address: event.address,
              telno: event.telno,
              tin: event.tin,
              adminUser: event.adminUser,
              adminTelno: event.adminTelno,
              adminAddress: event.adminAddress,
              adminTin: event.adminTin,
              faasType: event.faasType),
        );
      } catch (e) {
        emit(LandPropertyOwnerInfoErrorState(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<UpdateLandPropertyOwnerInfo>((event, emit) async {
      await SQLServices.instance
          .updateLandPropertyOwner(event.id, event.landPropertyOwner);
    });

    on<LoadLandPropertyOwner>((event, emit) async {
      todos = await SQLServices.instance.getLandPropertyOwner();
      emit(LandPropertyOwnerLoaded(landPropertyOwner: todos));
    });

    // on<FetchSpecificTodo>((event, emit) async {
    //   Prop todo = await PropertyOwnerInfoServices.instance.readTodo(id: event.id);
    //   emit(DisplaySpecificTodo(todo: todo));
    // });

    on<DeleteLandPropertyOwner>((event, emit) async {
      await SQLServices.instance.deleteLandPropertyOwner(id: event.id);
      add(const LoadLandPropertyOwner());
    });
  }
}
