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

import '../../../../../model/passo/land_ref.dart';
import '../../../../../model/passo/todo.dart';
import '../../../../../sevices/offline/offline_passo/building/property_owner_info_service.dart';

part 'landref_location_event.dart';
part 'landref_location_state.dart';

class LandrefLocationBloc
    extends Bloc<LandrefLocationEvent, LandrefLocationState> {
  LandrefLocationBloc() : super(LandrefInitial()) {
    List<LandRef> todos = [];
    on<AddLandRef>((event, emit) async {
      await SQLServices.instance.createBldgLandRef(
        LandRef(
            id: event.id,
            bldgapprDetailsId: event.bldgapprDetailsId,
            assessedById: event.assessedById,
            assessedByName: event.assessedByName,
            owner: event.owner,
            cloaNo: event.cloaNo,
            lotNo: event.lotNo,
            tdn: event.tdn,
            area: event.area,
            surveyNo: event.surveyNo,
            blkNo: event.blkNo,
            genCode: event.genCode),
      );
    });

    // on<UpdateTodo>((event, emit) async {
    //   await PropertyOwnerInfoServices.instance.update(
    //     todo: event.todo,
    //   );
    // });

    // on<FetchLanRef>((event, emit) async {
    //   landref = await PropertyOwnerInfoServices.instance.readAllTodos();
    //   emit(LandrefLoaded(landref: landref));
    // });

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

      if (result.isNotEmpty) {
        LandRef firstRow = LandRef(
          id: result[0]["id"],
          bldgapprDetailsId: result[0]["bldgapprDetailsId"],
          assessedById: result[0]["assessedById"],
          assessedByName: result[0]["assessedByName"],
          dateCreated: DateTime.now().toString(),
          dateModified: DateTime.now().toString(),
          owner: result[0]["owner"],
          cloaNo: result[0]["cloaNo"],
          lotNo: result[0]["lotNo"],
          tdn: result[0]["tdn"],
          area: result[0]["area"],
          surveyNo: result[0]["surveyNo"],
          blkNo: result[0]["blkNo"],
        );

        print('landref test result');
        print(firstRow);
        emit(SpecificLandrefLoaded(landref: firstRow));
      } else {
        print('No data found.');
      }
    });

    on<UpdateBldgLandRef>((event, emit) async {
      await SQLServices.instance.updateLandRef(event.id, event.landRef);
    });

    // on<DeleteTodo>((event, emit) async {
    //   await PropertyOwnerInfoServices.instance.delete(id: event.id);
    //   add(const FetchTodos());
    // });
  }
}
