import 'package:farm_tpf/data/api/app_exception.dart';
import 'package:farm_tpf/data/repository/repository.dart';
import 'package:rxdart/rxdart.dart';

class GetPlotInfoBloc {
  final _repository = Repository();
  final _getPlotInfoFetcher = PublishSubject<dynamic>();

  Stream<dynamic> get actions => _getPlotInfoFetcher.stream;

  void getPlotInfo(
      int cropId, Function(dynamic) onSuccess, Function(String) onError) async {
    _repository.getPlotDetail(cropId).then((value) {
      onSuccess(value);
      _getPlotInfoFetcher.sink.add(value);
    }).catchError((onError) {
      onError(AppException.handleError(onError));
      _getPlotInfoFetcher.addError(AppException.handleError(onError));
    });
  }

  void dispose() async {
    await _getPlotInfoFetcher.drain();
    _getPlotInfoFetcher.close();
  }
}

final getPlotInfoBloc = GetPlotInfoBloc();
