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

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

part 'additional_items_offline_event.dart';
part 'additional_items_offline_state.dart';

class AdditionalItemsOfflineBloc
    extends Bloc<AdditionalItemsOfflineEvent, AdditionalItemsOfflineState> {
  AdditionalItemsOfflineBloc() : super(AdditionalItemsOfflineInitial()) {
    List<AdditionalItems> addItems = [];
    on<LoadAdditionalItems>((event, emit) async {
      emit(AdditionalItemsOfflineInitial());
      try {
        emit(AdditionalItemsLoaded(addItem: addItems));
      } catch (e) {
        emit(AdditionalItemsErrorState(e.toString()));
      }
    });
    on<LoadAdditionalItemsEdit>((event, emit) async {
      emit(AdditionalItemsOfflineInitial());
      List<Map<String, dynamic>> result =
          await SQLServices.instance.getAdditionalItems(event.id);

      if (result.isNotEmpty) {
        addItems = result.map((map) => AdditionalItems.fromJson(map)).toList();

        emit(AdditionalItemsLoaded(addItem: addItems));
      } else {
        print('No data found.');
      }
    });

    on<AddAdditionalItems>((event, emit) async {
      try {
        AdditionalItems item = await SQLServices.instance.createAdditionalItems(
            AdditionalItems(
                id: event.id,
                bldgapprDetailsId: event.bldgapprDetailsId,
                classId: event.classId,
                className: event.className,
                structType: event.structType,
                unitValue: event.unitValue,
                baseUnitValue: event.baseUnitValue,
                area: event.area,
                marketValue: event.marketValue,
                depreciationRate: event.depreciationRate,
                adjustedMarketVal: event.adjustedMarketVal,
                amtDepreciation: event.amtDepreciation,
                painted: event.painted == true ? '1' : '0',
                secondhand: event.secondhand == true ? '1' : '0',
                paintedUnitval: event.paintedUnitval,
                secondhandUnitval: event.secondhandUnitval,
                actualUse: event.actualUse,
                genCode: event.genCode));

        print(item.toJson());

        addItems.add(item);

        emit(AdditionalItemsLoaded(addItem: addItems));
      } catch (e) {
        print(e.toString());
      }
    });
    on<ShowAdditionalItems>((event, emit) async {
      emit(ShowAddItemsScreen());
    });
    on<FetchAdditionalItems>((event, emit) async {
      addItems = await SQLServices.instance.readAdditionalItems();
      emit(AdditionalItemsLoaded(addItem: addItems));
    });
    on<DeleteAdditionalItems>((event, emit) async {
      addItems
          .removeWhere(((AdditionalItems element) => element.id == event.id));
      await SQLServices.instance.deleteAdditionalItems(id: event.id);
      emit(const AdditionalItemsDeletedState(success: true));
    });
  }
}
