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

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

part 'bldg_assessment_offline_event.dart';
part 'bldg_assessment_offline_state.dart';

class BldgAssessmentOfflineBloc
    extends Bloc<BldgAssessmentOfflineEvent, BldgAssessmentOfflineState> {
  BldgAssessmentOfflineBloc() : super(BldgAssessmentOfflineInitial()) {
    List<PropertyAssessment> assessment = [];
    on<AddBldgAssessment>((event, emit) async {
      await SQLServices.instance.createBldgAssessment(PropertyAssessment(
          id: event.id,
          bldgapprDetailsId: event.bldgapprDetailsId,
          assessedById: event.assessedById,
          assessedByName: event.assessedByName,
          dateCreated: event.dateCreated,
          dateModified: event.dateModified,
          actualUse: event.actualUse,
          marketValue: event.marketValue,
          assessmentLevel: event.assessmentLevel,
          assessedValue: event.assessedValue,
          taxable: event.taxable,
          exempt: event.exempt,
          qtr: event.qtr,
          yr: event.yr.toString(),
          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,
          note: event.note,
          genCode: event.genCode,
          appraisedbyDesignation: event.appraisedbyDesignation,
          approvedbyDesignation: event.approvedbyDesignation,
          recommendapprDesignation: event.recommendapprDesignation));
    });
    on<FetchSingleBldgAssessment>((event, emit) async {
      List<Map<String, dynamic>> result =
          await SQLServices.instance.getBldgAssessment(event.id);

      if (result.isNotEmpty) {
        List<PropertyAssessment> assessmentList =
            result.map((map) => PropertyAssessment.fromJson2(map)).toList();

        // Choose a specific element from locationList
        PropertyAssessment firstAssessment = assessmentList
            .first; // You can change this to select a specific item

        print('assessment test result');
        print(firstAssessment.toJson());
        emit(SpecificBldgAssessmentLoaded(assessment: firstAssessment));
      } else {
        print('No data found.');
      }
    });
    on<UpdateBldgAssessment>((event, emit) async {
      await SQLServices.instance.updateAssessment(event.id, event.assessment);
    });
  }
}
