import 'dart:async';
import 'dart:math';

import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter/cupertino.dart';
import 'package:zhiying_base_widget/widgets/goods_details/slide_banner/bloc/goods_details_slide_banner_repository.dart';
import 'package:zhiying_base_widget/widgets/goods_details/slide_banner/model/goods_details_silde_banner_model.dart';
import 'package:zhiying_comm/util/empty_util.dart';

part 'goods_details_slide_banner_event.dart';

part 'goods_details_slide_banner_state.dart';

class GoodsDetailsSlideBannerBloc extends Bloc<GoodsDetailsSlideBannerEvent, GoodsDetailsSlideBannerState> {
  GoodsDetailsSlideBannerRepository repository;

  GoodsDetailsSlideBannerBloc({@required this.repository});

  @override
  GoodsDetailsSlideBannerState get initialState => GoodsDetailsSlideBannerInitial();

  @override
  Stream<GoodsDetailsSlideBannerState> mapEventToState(
    GoodsDetailsSlideBannerEvent event,
  ) async* {
    /// 初始化
    if (event is GoodsDetailsSlideBannerInitEvent) {
      yield* _mapInitEventToState(event);
    }
  }

  /// 初始化
  Stream<GoodsDetailsSlideBannerState> _mapInitEventToState(GoodsDetailsSlideBannerInitEvent event) async* {
    var parentData = await repository.fetchParentData(event.model);
    if (!EmptyUtil.isEmpty(parentData)) {
      yield GoodsDetailsSlideBannerLoadedState(model: parentData);
    }else{
      yield GoodsDetailsSlideBannerErrorState();
    }
    // var netData = await repository.fetchNetData(event.model);
    // if (!EmptyUtil.isEmpty(netData))
    //   yield GoodsDetailsSlideBannerLoadedState(model: parentData);
    // else
    //   yield GoodsDetailsSlideBannerErrorState();
  }
}
