import 'dart:async';

import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter/cupertino.dart';
import 'package:zhiying_base_widget/widgets/home/home_goods/models/home_goods_model.dart';
import 'package:zhiying_base_widget/widgets/search_result/goods_list/bloc/search_result_goods_list_repository.dart';
import 'package:zhiying_comm/zhiying_comm.dart';

part 'search_result_goods_list_event.dart';

part 'search_result_goods_list_state.dart';

class SearchResultGoodsListBloc extends Bloc<SearchResultGoodsListEvent, SearchResultGoodsListState> {
  // SearchResultGoodsListBloc() : super(SearchResultGoodsListInitial());

  SearchResultGoodsListRepository repository;

  SearchResultGoodsListBloc({@required this.repository});

  @override
  SearchResultGoodsListState get initialState => SearchResultGoodsListInitial();

  @override
  Stream<SearchResultGoodsListState> mapEventToState(
    SearchResultGoodsListEvent event,
  ) async* {
    /// 初始化
    if (event is SearchResultGoodsListInitEvent) {
      yield* _mapInitEventToState(event);
    }

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

    /// 上拉更多
    if (event is SearchResultGoodsListOnLoadEvent && state is SearchResultGoodsListLoadedState) {
      yield* _mapOnLoadToState(event, state);
    }

    /// 修改样式
    if (event is SearchResultGoodsListChangeStyleEvent) {
      yield* _mapChangeStyleToState();
    }

    /// 搜索
    if (event is SearchResultGoodsListSubmitEvent) {
      yield* _mapSubmitToState(event);
    }

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

    /// 综合
    if (event is SearchResultGoodsListZHSortEvent) {
      yield* _mapZhSortEventToState(event);
    }

    /// 补贴
    if (event is SearchResultGoodsListBtSortEvent) {
      yield* _mapBTSortEventToState(event);
    }

    /// 优惠券
    if (event is SearchResultGoodsListYHQSortEvent) {
      yield* _mapYHQSortEventToState(event);
    }

    /// 筛选
    if (event is SearchResultGoodsListSXSortEvent) {
      yield* _mapSXSortEventToState(event);
    }
  }

  /// 初始化事件
  Stream<SearchResultGoodsListState> _mapInitEventToState(SearchResultGoodsListInitEvent event) async* {
    var result = await repository.fetchInitData();
    if (!EmptyUtil.isEmpty(result)) {
      yield SearchResultGoodsListLoadedState(model: result);
    } else {
      yield SearchResultGoodsListErrorState();
    }
  }

  /// 下拉刷新
  Stream<SearchResultGoodsListState> _mapRefreshToState(SearchResultGoodsListOnRefreshEvent event) async* {
    var result = await repository.fetchRefreshData();
    if (!EmptyUtil.isEmpty(result)) {
      yield SearchResultGoodsListLoadedState(model: result);
    } else {
      yield SearchResultGoodsListErrorState();
    }
  }

  /// 上拉更多
  Stream<SearchResultGoodsListState> _mapOnLoadToState(SearchResultGoodsListOnLoadEvent event, SearchResultGoodsListLoadedState state) async* {
    var result = await repository.fetchLoadData();
    if (!EmptyUtil.isEmpty(result)) {
      yield SearchResultGoodsListLoadedState(model: result);
    } else {
      yield SearchResultGoodsListErrorState();
    }
  }

  /// 修改样式
  Stream<SearchResultGoodsListState> _mapChangeStyleToState() async* {
    var result = repository.fetchChangeStyle();
    if (!EmptyUtil.isEmpty(result)) {
      yield SearchResultGoodsListChangeStyleState(result['style'], result['datas']);
    } else {
      yield SearchResultGoodsListErrorState();
    }
  }

  /// 关键字搜索
  Stream<SearchResultGoodsListState> _mapSubmitToState(SearchResultGoodsListSubmitEvent event) async* {
    var result = await repository.fetchSearchSubmit(event.keywords);
    if (!EmptyUtil.isEmpty(result)) {
      yield SearchResultGoodsListLoadedState(model: result);
    } else {
      yield SearchResultGoodsListErrorState();
    }
  }

  /// 排序
  Stream<SearchResultGoodsListState> _mapSortEventToState(SearchResultGoodsListSortEvent event) async* {
    var result = await repository.fetchSort(event.args, event.id, event.remove);
    if (!EmptyUtil.isEmpty(result)) {
      yield SearchResultGoodsListLoadedState(model: result);
    } else {
      yield SearchResultGoodsListErrorState();
    }
  }

  /// 综合排序
  Stream<SearchResultGoodsListState> _mapZhSortEventToState(SearchResultGoodsListZHSortEvent event) async* {
    var result = await repository.fetchZhSort(event.args);
    if (!EmptyUtil.isEmpty(result)) {
      yield SearchResultGoodsListLoadedState(model: result);
    } else {
      yield SearchResultGoodsListErrorState();
    }
  }

  /// 补贴排序
  Stream<SearchResultGoodsListState> _mapBTSortEventToState(SearchResultGoodsListBtSortEvent event) async* {
    var result = await repository.fetchBt(event.args);
    if (!EmptyUtil.isEmpty(result)) {
      yield SearchResultGoodsListLoadedState(model: result);
    } else {
      yield SearchResultGoodsListErrorState();
    }
  }

  /// 优惠券排序
  Stream<SearchResultGoodsListState> _mapYHQSortEventToState(SearchResultGoodsListYHQSortEvent event) async* {
    var result = await repository.fetchYhq(event.args);
    if (!EmptyUtil.isEmpty(result)) {
      yield SearchResultGoodsListLoadedState(model: result);
    } else {
      yield SearchResultGoodsListErrorState();
    }
  }

  /// 筛选排序
  Stream<SearchResultGoodsListState> _mapSXSortEventToState(SearchResultGoodsListSXSortEvent event) async* {
    var result = await repository.fetchSX(event.args);
    if (!EmptyUtil.isEmpty(result)) {
      yield SearchResultGoodsListLoadedState(model: result);
    } else {
      yield SearchResultGoodsListErrorState();
    }
  }
}
