import 'dart:async';

import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter/cupertino.dart';
import 'package:zhiying_base_widget/pages/goods_details_page/bloc/goods_details_page_repository.dart';
import 'package:zhiying_comm/util/empty_util.dart';

part 'goods_details_page_event.dart';

part 'goods_details_page_state.dart';

class GoodsDetailsPageBloc extends Bloc<GoodsDetailsPageEvent, GoodsDetailsPageState> {
  // GoodsDetailsPageBloc() : super(GoodsDetailsPageInitial());

  @override
  GoodsDetailsPageState get initialState => GoodsDetailsPageInitial();

  GoodsDetailsPageRepository repository;

  GoodsDetailsPageBloc({@required this.repository});

  @override
  Stream<GoodsDetailsPageState> mapEventToState(
    GoodsDetailsPageEvent event,
  ) async* {
    if (event is GoodsDetailsPageInitEvent) {
      yield* _mapInitEventToState(event);
    }
  }

  Stream<GoodsDetailsPageState> _mapInitEventToState(GoodsDetailsPageInitEvent event) async* {
    var result = await repository.fetchInitData(event.model);
    if (!EmptyUtil.isEmpty(result))
      yield GoodsDetailsPageLoadedState(model: result);
    else
      yield GoodsDetailsPageErrorState();
  }
}
