import 'dart:async';

import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:farm_tpf/custom_model/Nursery.dart';
import 'package:farm_tpf/custom_model/Nursery.dart';
import 'package:farm_tpf/custom_model/NurseryDetail.dart';
import 'package:meta/meta.dart';

part 'expansion_list_event.dart';
part 'expansion_list_state.dart';

class ExpansionListBloc extends Bloc<ExpansionListEvent, ExpansionListState> {
  ExpansionListBloc() : super(ExpansionListInitial());

  @override
  Stream<ExpansionListState> mapEventToState(
    ExpansionListEvent event,
  ) async* {
    if (event is AddNew) {
      var items = <NurseryDetail>[];
      for (var i = 0; i < event.items.length; i++) {
        var current = event.items[i];
        items.add(NurseryDetail.clone(current));
      }
      yield ExpansionListSuccess(items: items);
    } else if (event is DeleteItem) {
      var items = <NurseryDetail>[];
      for (var i = 0; i < event.items.length; i++) {
        var current = event.items[i];
        if (i == event.index) {
          //ignore
        } else {
          items.add(NurseryDetail.clone(current));
        }
      }

      yield ExpansionListSuccess(items: items);
    } else if (event is Update) {
      var items = <NurseryDetail>[];
      for (var i = 0; i < event.items.length; i++) {
        var current = event.items[i];
        if (i == event.index) {
          items.add(NurseryDetail.clone(event.item));
        } else {
          items.add(NurseryDetail.clone(current));
        }
      }

      yield ExpansionListSuccess(items: items);
    }
  }
}
