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

import 'package:bloc/bloc.dart';
import 'package:flutter/cupertino.dart';
import 'package:zhiying_comm/util/empty_util.dart';

import './bloc.dart';

class CustomQuickEntryBloc extends Bloc<CustomQuickEntryEvent, CustomQuickEntryState> {
  CustomQuickEntryRepository repository;

  CustomQuickEntryBloc({@required this.repository});

  @override
  CustomQuickEntryState get initialState => InitialCustomQuickEntryState();

  @override
  Stream<CustomQuickEntryState> mapEventToState(CustomQuickEntryEvent event) async* {
    /// 初始化
    if (event is CustomQuickEntryInitEvent) {
      yield* _mapHomeQuickEntryInitToState(event);
    }
  }

  /// 初始化
  Stream<CustomQuickEntryState> _mapHomeQuickEntryInitToState(CustomQuickEntryInitEvent event) async* {
    /// 获取父页面传进来的数据
    var parentData = await repository.fetchPreantData(event: event);
    if (!EmptyUtil.isEmpty(parentData)) {
      yield CustomQuickEntryLoadedState(model: parentData);
    } else {
      yield CustomQuickEntryErrorState();
    }

    // /// 获取本地缓存数据
    // var cached = await repository.fetchCachedData(event: event);
    // if (!EmptyUtil.isEmpty(cached)) {
    //   yield CustomQuickEntryCachedState(model: cached);
    // }
    //
    // /// 获取网络的数据
    // var result = await repository.fetchData(event: event);
    // if (!EmptyUtil.isEmpty(result)) {
    //   yield CustomQuickEntryLoadedState(model: result);
    // } else {
    //   yield CustomQuickEntryErrorState();
    // }
  }
}
