import 'dart:async';

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

part 'status_add_form_event.dart';
part 'status_add_form_state.dart';

class StatusAddFormBloc extends Bloc<StatusAddFormEvent, StatusAddFormState> {
  StatusAddFormBloc() : super(Initial());

  @override
  Stream<StatusAddFormState> mapEventToState(
    StatusAddFormEvent event,
  ) async* {
    if (event is Changed) {
      switch (event.status) {
        case CRUDStatus.edit:
          var index = event.index;
          var item = NurseryDetail.clone(event.nurseryDetail);
          var items = <NurseryDetail>[];
          for (var i = 0; i < event.items.length; i++) {
            var current = event.items[i];
            items.add(NurseryDetail.clone(current));
          }
          yield Edit(nurseryDetail: item, index: index, items: items);
          break;
        case CRUDStatus.delete:
          if (event.items.isEmpty) {
            break;
          }
          var index = event.index;
          var item = NurseryDetail.clone(event.nurseryDetail);
          var items = <NurseryDetail>[];
          for (var i = 0; i < event.items.length; i++) {
            var current = event.items[i];
            if (i == index) {
              //
            } else {
              items.add(NurseryDetail.clone(current));
            }
          }
          yield Delete(nurseryDetail: item, index: index, items: items);
          break;
        default:
          yield Initial();
      }
    } else if (event is Reset) {
      yield Initial();
    }
  }
}
