import 'dart:async';

import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter/cupertino.dart';
import 'package:zhiying_base_widget/widgets/team/fans_list/bloc/team_list_fans_repository.dart';
import 'package:zhiying_base_widget/widgets/team/fans_list/model/team_fans_list_model.dart';
import 'package:zhiying_comm/util/empty_util.dart';

part 'team_list_fans_event.dart';

part 'team_list_fans_state.dart';

class TeamListFansBloc extends Bloc<TeamListFansEvent, TeamListFansState> {

  TeamListFansRepository repository;

  TeamListFansBloc(this.repository);

  @override
  TeamListFansState get initialState => TeamListFansInitial();

  @override
  Stream<TeamListFansState> mapEventToState(
    TeamListFansEvent event,
  ) async* {
    final currentState = state;

    /// 初始化
    if (event is TeamListFansInitEvent) {
      yield* _mapInitEventToState(event);
    }

    /// 下拉刷新
    if (event is TeamListFansOnRefreshEvent) {
      yield* _mapRefreshToState(currentState, event);
    }

    /// 上拉更多
    if (event is TeamListFansOnLoadEevnt) {
      yield* _mapOnLoadToState(event);
    }

    /// 排序
    if (event is TeamListFansOnSortEvent) {
      yield* _mapSortEventToState(event);
    }

    /// 整体条件筛选
    if (event is TeamListFansFilterEvent) {
      yield* _mapFilterEventToState(event);
    }

    /// 搜索
    if(event is TeamListFansSearchEvent){
      yield* _mapSearchEventToState(event);
    }

  }

  /// 初始化
  Stream<TeamListFansState> _mapInitEventToState(TeamListFansInitEvent event) async* {
    var result = await repository.fetchInitData();
    if (!EmptyUtil.isEmpty(result)) {
      yield TeamListFansLoadedState(model: result);
    } else {
      yield TeamListFansErrorState();
    }
  }

  /// 下拉刷新
  Stream<TeamListFansState> _mapRefreshToState(TeamListFansState state, TeamListFansOnRefreshEvent event) async* {
    var result = await repository.fetchOnRefresh();
    if (!EmptyUtil.isEmpty(result)) {
      yield TeamListFansOnRefreshErrorState();
      yield TeamListFansLoadedState(model: result);
    } else {
      yield TeamListFansOnRefreshErrorState();
    }
  }

  /// 上拉更多
  Stream<TeamListFansState> _mapOnLoadToState(TeamListFansOnLoadEevnt event) async* {
    var result = await repository.fetchOnLoad();
    if (!EmptyUtil.isEmpty(result)) {
      yield TeamListFansOnLoadSuccessState();
      yield TeamListFansLoadedState(model: result);
    } else {
      yield TeamListFansOnLoadErrorState();
    }
  }

  /// 排序
  Stream<TeamListFansState> _mapSortEventToState(TeamListFansOnSortEvent event) async* {
    var result = await repository.fetchSort(event.key, event.args);
    if (!EmptyUtil.isEmpty(result)) {
      yield TeamListFansLoadedState(model: result);
    } else {
      yield TeamListFansErrorState();
    }
  }

  /// 整体筛选
  Stream<TeamListFansState> _mapFilterEventToState(TeamListFansFilterEvent event) async* {
    var result = await repository.fetchFilter(event.args);
    if (!EmptyUtil.isEmpty(result)) {
      yield TeamListFansLoadedState(model: result);
    } else {
      yield TeamListFansErrorState();
    }
  }

  /// 搜索
  Stream<TeamListFansState> _mapSearchEventToState(TeamListFansSearchEvent event) async*{
    var result = await repository.fetchSearchData(event?.keyword);
    if (!EmptyUtil.isEmpty(result)) {
      yield TeamListFansLoadedState(model: result);
    } else {
      yield TeamListFansErrorState();
    }
  }

}
